[英]why esprima works fine in online browser demo, but failed locally
When I try to parse a snippet of code with the following script, it failed (using esprima@4.0.1).当我尝试使用以下脚本解析一段代码时,它失败了(使用 esprima@4.0.1)。 However it works fine in the esprima online browser demo, shown in the screenshot below.
然而,它在 esprima 在线浏览器演示中运行良好,如下面的屏幕截图所示。
var esprima = require('esprima');
var fs = require('fs');
const raw_string = String.raw`export { AppCache , addOrReplaceCacheableGroup , findCacheableGroup , CacheTimeoutData } from 's' ; export { AppCacheAction , AppCacheActionType , CacheGroupAction , AddToCacheAction } from 's' ; export { cacheGroup , addToCache } from 's' ; export { appCacheReducer } from 's' ;`;
ast_string = JSON.stringify(esprima.parseScript(raw_string, { range: true }), null, 2);
fs.writeFile('./ast_data/test.json', ast_string, () => {});
The output error message is as follows: output错误信息如下:
/usr/lib/nodejs/esprima/dist/error-handler.js:17
throw error;
^
Error: Line 1: Unexpected token
at ErrorHandler.constructError (/usr/lib/nodejs/esprima/dist/error-handler.js:21:21)
at ErrorHandler.createError (/usr/lib/nodejs/esprima/dist/error-handler.js:37:26)
at Parser.unexpectedTokenError (/usr/lib/nodejs/esprima/dist/parser.js:162:38)
at Parser.tolerateUnexpectedToken (/usr/lib/nodejs/esprima/dist/parser.js:175:41)
at Parser.parseStatementListItem (/usr/lib/nodejs/esprima/dist/parser.js:1542:30)
at Parser.parseScript (/usr/lib/nodejs/esprima/dist/parser.js:2900:28)
at parse (/usr/lib/nodejs/esprima/dist/esprima.js:63:60)
at Object.parseScript (/usr/lib/nodejs/esprima/dist/esprima.js:86:12)
at Object.<anonymous> (/home/bob/Desktop/js_ast/esprima-ast-visitor-1.0.0/ast_gen.js:6:37)
at Module._compile (internal/modules/cjs/loader.js:778:30)
I solved my problem by changing esprima.parseScript to esprima.parseModule我通过将 esprima.parseScript 更改为 esprima.parseModule 解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.