esprima icon indicating copy to clipboard operation
esprima copied to clipboard

Issue with Spread Operator Inside JSON

Open FlameWolf opened this issue 6 years ago • 6 comments

When trying to parse a JSON expression containing a spread operator, we get an error, even though the code is valid.

Steps to reproduce

Try to validate the below code:

var x = {
	a: "asdf",
	b: "qwerty",
	...(1 > 0 ? { c: "zxcv" } : ""),
	d: 1234
};

Expected output

A message saying "Code is syntactically valid."

Actual output

Error: Line 4: Unexpected token ...

FlameWolf avatar Apr 13 '18 12:04 FlameWolf

Closed #1949 and moved here

Steps to reproduce

When using recast to parse, it fails on a spread syntax object.

query = {
      ...query, // FAILS here, line 477
      $or: [
        {_id: { $in: req.jwt.var}},
        {owner: req.jwt.var2}
      ]
    };

Expected output

Program completes parsing normally.

Actual output

gitdir\node_modules\esprima\dist\esprima.js:1995
                throw this.unexpectedTokenError(token, message);
                ^
Error: Line 477: Unexpected token ...
    at ErrorHandler.constructError (gitdir\node_modules\esprima\dist\esprima.js:5012:22)
    at ErrorHandler.createError (gitdir\node_modules\esprima\dist\esprima.js:5028:27)
    at Parser.unexpectedTokenError (gitdir\node_modules\esprima\dist\esprima.js:1985:39)
    at Parser.throwUnexpectedToken (gitdir\node_modules\esprima\dist\esprima.js:1995:21)
    at Parser.parseObjectPropertyKey (gitdir\node_modules\esprima\dist\esprima.js:2499:33)
    at Parser.parseObjectProperty (gitdir\node_modules\esprima\dist\esprima.js:2534:25)
    at Parser.parseObjectInitializer (gitdir\node_modules\esprima\dist\esprima.js:2602:35)
    at Parser.inheritCoverGrammar (gitdir\node_modules\esprima\dist\esprima.js:2285:37)
    at Parser.parsePrimaryExpression (gitdir\node_modules\esprima\dist\esprima.js:2354:38)
    at Parser.inheritCoverGrammar (gitdir\node_modules\esprima\dist\esprima.js:2285:37)

Commenting out that line (477), and it ran successfully

dbryan0516 avatar Aug 13 '18 20:08 dbryan0516

Hi there ! Is there any chance this to be fixed ?:) according to the test/fixtures/es2018/spread-property it should work but does not for me

AlekseyChepurko avatar Sep 24 '18 08:09 AlekseyChepurko

Apparently this feature is supported in master, but not in the latest official release. It would be great it were on the official release too. I am writing a JavaScript visualization platform and some projects fail because of this :S

dorellang avatar Nov 05 '18 22:11 dorellang

GKD!

captainblue2013 avatar Jan 10 '19 10:01 captainblue2013

The object spread operator is valid syntax as of ECMAScript 2018. http://www.ecma-international.org/ecma-262/9.0/#sec-object-initializer It is a shame that the released version of esprima doesn't support this yet.

fr0 avatar Apr 25 '19 19:04 fr0

There's a solution here https://github.com/jquery/esprima/issues/2034#issuecomment-818542469

0x00C5 avatar Nov 05 '21 17:11 0x00C5