esprima
esprima copied to clipboard
Issue with Spread Operator Inside JSON
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 ...
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
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
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
GKD!
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.
There's a solution here https://github.com/jquery/esprima/issues/2034#issuecomment-818542469