danielstjules / jsinspect

Detect copy-pasted and structurally similar code
MIT License
3.56k stars 128 forks source link

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode #54

Closed williamku closed 7 years ago

williamku commented 7 years ago

Hi, Daniel,

I have just tried this on 2 projects, got identical error block like this:

/usr/local/lib/node_modules/jsinspect/lib/inspector.js:10 class Inspector extends EventEmitter { ^^^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object. (/usr/local/lib/node_modules/jsinspect/bin/jsinspect:9:17) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10)

Not sure if this rule is over-enforced here. I have worked on 2 large existing ES6 projects. ES6 modules and classes are strict by default. Any chance you can take a look at this? Do you have a configuration entry that will suppress this error? Or a workaround?

Thanks! William

danielstjules commented 7 years ago

Please try upgrading to Node 6 :)

williamku commented 7 years ago

thanks for the tip! I switched to node version 6.10.1 and now i see a different error: (023_Sprint58_duplicate_scan)$ jsinspect -I -L -t 20 ./core_modules { SyntaxError: Unexpected token (8:10) at Parser.pp$5.raise (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:4373:13) at Parser.pp.unexpected (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:1716:8) at Parser.pp$3.parseParenAndDistinguishExpression (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3792:10) at Parser.pp$3.parseExprAtom (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3642:19) at Parser.parseExprAtom (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:7016:22) at Parser.pp$3.parseExprSubscripts (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3427:19) at Parser.pp$3.parseMaybeUnary (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3407:19) at Parser.pp$3.parseExprOps (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3337:19) at Parser.pp$3.parseMaybeConditional (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3314:19) at Parser.pp$3.parseMaybeAssign (/usr/local/lib/node_modules/jsinspect/node_modules/babylon/lib/index.js:3277:19) pos: 146, loc: Position { line: 8, column: 10 } }

I then reinstall jsinspect and tried again - got the same error as the above.

danielstjules commented 7 years ago

I just published 0.12 which includes a --debug option. Could you upgrade and try running jsinspect with that flag? You'll see some output like the following:

$ jsinspect --debug
parsing ./index.js
parsing ./lib/debug.js
parsing ./lib/inspector.js
parsing ./lib/match.js
...

The last file it prints before the exception is the one that's causing the error. Could you check the JS in that file and perhaps include a snippet here if you know what code is causing the issue? Does the file parse correctly on https://babeljs.io/repl/ ?

Thanks!

danielstjules commented 7 years ago

Looking at the stacktrace, it also includes the position of the error:

Position { line: 8, column: 10 }
danielstjules commented 7 years ago

Closing for now, but feel free to re-open if you can provide some additional information!

matthargett commented 7 years ago

Just FYI , I was getting a similar error and it turned out that jsinspect was running on my source map output. I used the newly added debug flag to help point me to the file with the parse issue.

danielstjules commented 7 years ago

@matthargett ah, that's helpful. I'll have to push an update that includes a more helpful error message on parsing errors.

danielstjules commented 7 years ago

Improved error handling in: https://github.com/danielstjules/jsinspect/commit/9c21549ac3aed3398024ec6bec2052d879a7e494

E.g.

$ cat ~/Desktop/bad.js
[_, = [1, 2, 3];

$ jsinspect ~/Desktop/bad.js
Error: Couldn't parse /Users/danielstjules/Desktop/bad.js: Unexpected token (1:4)
[_, = [1, 2, 3];
    ^
    at exports.parse (/Users/danielstjules/git/jsinspect/lib/parser.js:21:13)
    at _filePaths.forEach (/Users/danielstjules/git/jsinspect/lib/inspector.js:58:24)
    at Array.forEach (native)
    at Inspector.run (/Users/danielstjules/git/jsinspect/lib/inspector.js:55:21)
    at Object.<anonymous> (/Users/danielstjules/git/jsinspect/bin/jsinspect:110:13)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)

Thanks again!