Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to parse 'Spread' syntax: Unexpected token ... #2034

Open
rob4629 opened this issue Sep 2, 2020 · 8 comments
Open

Fails to parse 'Spread' syntax: Unexpected token ... #2034

rob4629 opened this issue Sep 2, 2020 · 8 comments

Comments

@rob4629
Copy link

rob4629 commented Sep 2, 2020

Steps to reproduce

I'm using the ES2018 syntax in my code, and Esprima is failing to parse the "spread" syntax (...), for creating iterables and concatenating objects.

// Spread syntax:
const obj1 = { key: "value"};
const obj2 = { ...obj1, key2: "value"};

esprima.parse(obj2);

Expected output

I would expect the Spread syntax to be correctly parsed.

Actual output

Here's a demo link to the current issue being faced

Here's the full stack trace:
Unexpected token ...
      at ErrorHandler.constructError (node_modules/esprima/dist/esprima.js:5012:22)
      at ErrorHandler.createError (node_modules/esprima/dist/esprima.js:5028:27)
      at Parser.unexpectedTokenError (node_modules/esprima/dist/esprima.js:1985:39)
      at Parser.throwUnexpectedToken (node_modules/esprima/dist/esprima.js:1995:21)
      at Parser.parseObjectPropertyKey (node_modules/esprima/dist/esprima.js:2499:33)
      at Parser.parseObjectProperty (node_modules/esprima/dist/esprima.js:2534:25)
      at Parser.parseObjectInitializer (node_modules/esprima/dist/esprima.js:2602:35)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parsePrimaryExpression (node_modules/esprima/dist/esprima.js:2354:38)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseLeftHandSideExpressionAllowCall (node_modules/esprima/dist/esprima.js:2899:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseUpdateExpression (node_modules/esprima/dist/esprima.js:3004:26)
      at Parser.parseUnaryExpression (node_modules/esprima/dist/esprima.js:3048:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseExponentiationExpression (node_modules/esprima/dist/esprima.js:3054:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseBinaryExpression (node_modules/esprima/dist/esprima.js:3089:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseConditionalExpression (node_modules/esprima/dist/esprima.js:3141:26)
      at Parser.parseAssignmentExpression (node_modules/esprima/dist/esprima.js:3250:26)
      at Parser.isolateCoverGrammar (node_modules/esprima/dist/esprima.js:2269:37)
      at Parser.parseLexicalBinding (node_modules/esprima/dist/esprima.js:3425:34)
      at Parser.parseBindingList (node_modules/esprima/dist/esprima.js:3439:27)
      at Parser.parseLexicalDeclaration (node_modules/esprima/dist/esprima.js:3461:34)
      at Parser.parseStatementListItem (node_modules/esprima/dist/esprima.js:3376:39)
      at Parser.parseFunctionSourceElements (node_modules/esprima/dist/esprima.js:4195:29)
      at Parser.parseAssignmentExpression (node_modules/esprima/dist/esprima.js:3286:38)
      at Parser.isolateCoverGrammar (node_modules/esprima/dist/esprima.js:2269:37)
      at Parser.parseArguments (node_modules/esprima/dist/esprima.js:2807:27)
      at Parser.parseLeftHandSideExpressionAllowCall (node_modules/esprima/dist/esprima.js:2913:76)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseUpdateExpression (node_modules/esprima/dist/esprima.js:3004:26)
      at Parser.parseUnaryExpression (node_modules/esprima/dist/esprima.js:3048:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseExponentiationExpression (node_modules/esprima/dist/esprima.js:3054:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseBinaryExpression (node_modules/esprima/dist/esprima.js:3089:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseConditionalExpression (node_modules/esprima/dist/esprima.js:3141:26)
      at Parser.parseAssignmentExpression (node_modules/esprima/dist/esprima.js:3250:26)
      at Parser.isolateCoverGrammar (node_modules/esprima/dist/esprima.js:2269:37)
      at Parser.parseExpression (node_modules/esprima/dist/esprima.js:3341:26)
      at Parser.parseLabelledStatement (node_modules/esprima/dist/esprima.js:3996:26)
      at Parser.parseStatement (node_modules/esprima/dist/esprima.js:4122:97)
      at Parser.parseStatementListItem (node_modules/esprima/dist/esprima.js:3393:31)
      at Parser.parseFunctionSourceElements (node_modules/esprima/dist/esprima.js:4195:29)
      at Parser.parseFunctionExpression (node_modules/esprima/dist/esprima.js:4408:26)
      at Parser.parsePrimaryExpression (node_modules/esprima/dist/esprima.js:2380:38)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseLeftHandSideExpressionAllowCall (node_modules/esprima/dist/esprima.js:2899:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseUpdateExpression (node_modules/esprima/dist/esprima.js:3004:26)
      at Parser.parseUnaryExpression (node_modules/esprima/dist/esprima.js:3048:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseExponentiationExpression (node_modules/esprima/dist/esprima.js:3054:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseBinaryExpression (node_modules/esprima/dist/esprima.js:3089:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseConditionalExpression (node_modules/esprima/dist/esprima.js:3141:26)
      at Parser.parseAssignmentExpression (node_modules/esprima/dist/esprima.js:3250:26)
      at Parser.isolateCoverGrammar (node_modules/esprima/dist/esprima.js:2269:37)
      at Parser.parseArguments (node_modules/esprima/dist/esprima.js:2807:27)
      at Parser.parseLeftHandSideExpressionAllowCall (node_modules/esprima/dist/esprima.js:2913:76)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseUpdateExpression (node_modules/esprima/dist/esprima.js:3004:26)
      at Parser.parseUnaryExpression (node_modules/esprima/dist/esprima.js:3048:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseExponentiationExpression (node_modules/esprima/dist/esprima.js:3054:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseBinaryExpression (node_modules/esprima/dist/esprima.js:3089:26)
      at Parser.inheritCoverGrammar (node_modules/esprima/dist/esprima.js:2285:37)
      at Parser.parseConditionalExpression (node_modules/esprima/dist/esprima.js:3141:26)
      at Parser.parseAssignmentExpression (node_modules/esprima/dist/esprima.js:3250:26)
      at Parser.isolateCoverGrammar (node_modules/esprima/dist/esprima.js:2269:37)
      at Parser.parseExpression (node_modules/esprima/dist/esprima.js:3341:26)
      at Parser.parseLabelledStatement (node_modules/esprima/dist/esprima.js:3996:26)
      at Parser.parseStatement (node_modules/esprima/dist/esprima.js:4122:97)
      at Parser.parseStatementListItem (node_modules/esprima/dist/esprima.js:3393:31)
      at Parser.parseModule (node_modules/esprima/dist/esprima.js:4715:29)
      at parse (node_modules/esprima/dist/esprima.js:122:38)

Relevant references

You can see the Spread syntax here.

@fzxs8
Copy link

fzxs8 commented Jan 27, 2021

这么长时间了还没修复!

@Jamby93
Copy link

Jamby93 commented Apr 13, 2021

Actually, the spread operator seems to be correctly parsed in almost every case (function parameters, array initialization). It is even present in the parseObjectInitializer at line 936 so I think it should be working, but as OP found out, it actually breaks with Unexpected token inside parseObjectPropertyKey.
I solved by adding a single else inside the parseObjectPropertyKey, just before the last else:
else if (this.match('...')) { return this.parseSpreadElement(); }

I'm not entirely sure why the parseSpreadElement handled in parseObjectInitializer isn't triggering, but at least with this fix I was able to proceed with parsing of our files.

@0x00C5
Copy link

0x00C5 commented Nov 5, 2021

The solution from @Jamby93 worked for me. If you want to patch the node_modules source directly without having to compile the source, look for the following line (currently line 2511 in dist/esprima.js):

Parser.prototype.parseObjectProperty = function (hasProto) {

@GrayYoung
Copy link

这么长时间了还没修复!

I upgraded bootstrap to V5, suffering the same problem, feeling so bad.

@Meir017
Copy link
Contributor

Meir017 commented Dec 31, 2021

@jogibear9988 is this supported in https://github.com/node-projects/esprima-next ?

@jogibear9988
Copy link

don't know. test it, if it does not work, create an issue and I will fix

@jogibear9988
Copy link

@Meir017 @GrayYoung
I've tried, this works in my fork esprima-next

@mjaggard
Copy link

For anyone else struggling with this, one option is to run Babel on the offending file(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants