运行 Solidity 测试时出现意外标记“[" [英] Unexpected token '[' when running Solidity test

查看:72
本文介绍了运行 Solidity 测试时出现意外标记“["的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行我的 Solidity 合约的 Mocha 测试,但它引发了与编译器代码相关的错误.

I'm currently running a Mocha test of my Solidity contract but it throws error which is related to compiler code.

C:\eth\compile.js:8
modules.exports = solc.compile(source).[];

SyntaxError: Unexpected token '['
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\eth\test\inbox.test.js:5:31)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.exports.requireOrImport (C:\eth\node_modules\mocha\lib\esm-utils.js:42:12)
    at Object.exports.loadFilesAsync (C:\eth\node_modules\mocha\lib\esm-utils.js:55:34)
    at Mocha.loadFilesAsync (C:\eth\node_modules\mocha\lib\mocha.js:473:19)
    at singleRun (C:\eth\node_modules\mocha\lib\cli\run-helpers.js:125:15)
    at exports.runMocha (C:\eth\node_modules\mocha\lib\cli\run-helpers.js:190:10)
    at Object.exports.handler (C:\eth\node_modules\mocha\lib\cli\run.js:362:11)
    at C:\eth\node_modules\mocha\node_modules\yargs\build\index.cjs:443:71

编译器本身是:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts', 'inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

modules.exports = solc.compile(source).[:Inbox];

推荐答案

  1. 您需要传递 JSON 字符串化选项对象(到 compile() 函数),而不仅仅是文本源.请参阅自述文件中的示例.
  2. 您的 solc.compile(source).[:Inbox] 代码有语法错误(将访问属性与 . 和数组与 [ 组合在一起)>) 和逻辑错误(未定义的 :Inbox,编译结果的错误路径,尝试将 JSON 字符串作为对象访问).
  1. You need to pass the JSON-stringified options object (to the compile() function), not just the text source. See the example in the readme.
  2. Your solc.compile(source).[:Inbox] code has syntax errors (combining access to property with . and array with [) and logical errors (undefined :Inbox, incorrect path to the compiled result, trying to access a JSON string as an object).

假设 inbox.sol 包含 contract Inbox 这是您要编译的主要合同,这是您问题中最后一行的有效代码替换:

Asssuming that inbox.sol contains contract Inbox that is the main contract you want to compile, this is a working code replacement for the last line in your question:

const options = {
  language: 'Solidity',
  sources: {
    'inbox.sol': {
      content: source
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};
const compiledRaw = solc.compile(JSON.stringify(options));
const compiledObj = JSON.parse(compiledRaw);
const compiledInboxContract = compiledObj.contracts['inbox.sol']['Inbox'];
//console.log(compiledInboxContract)
module.exports = compiledInboxContract;

这篇关于运行 Solidity 测试时出现意外标记“["的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆