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

Node REPL says "SyntaxError: unexpected symbol :" when parsing multi-line string-keyed objects (e.g. JSON) #21657

Closed
alexch opened this issue Jul 4, 2018 · 8 comments
Labels
regression Issues related to regressions. repl Issues and PRs related to the REPL subsystem.

Comments

@alexch
Copy link

alexch commented Jul 4, 2018

  • Version: 10.3.0 and 10.5.0
  • Platform: MacOS
  • Subsystem: CLI

When entering (or pasting) a multi-line object with string keys -- e.g. any JSON object -- the Node CLI correctly asks for more input with a ... prompt, but then incorrectly fails with a SyntaxError: Unexpected token :.

Example:

$ node --version
v10.3.0
$ node
> {"foo":false}
{ foo: false }
> {
... "foo":false
"foo":false
     ^

SyntaxError: Unexpected token :

This problem only occurs with string keys; bare keys work fine:

> {
... foo:false
... }
{ foo: false }

reproduced on 10.5.0 as well

This is an important issue because JSON objects are all string-keyed, and this parsing bug makes it impossible to copy and paste valid, pretty-printed JSON into the console.

@addaleax
Copy link
Member

addaleax commented Jul 4, 2018

I’m not sure what changed in Node (v8.x doesn’t have this issue), but I think I can answer why quoted strings vs unquoted keys matter: In the case of un-quoted “keys”, JS code like { foo: false } is not, like one would expect, parsed as an object.

Instead, it’s parsed as a block, and the foo: bit is parsed as a label for the statement false.

This doesn’t happen with single-line statements because Node.js hacks around that and first tries to wrap the code in parentheses, i.e. ({ foo: false }), which would be parsed differently (as an object).

This is an important issue because JSON objects are all string-keyed, and this parsing bug makes it impossible to copy and paste valid, pretty-printed JSON into the console.

Yes, I think that’s an use case we should definitely support 👍

@addaleax addaleax added repl Issues and PRs related to the REPL subsystem. regression Issues related to regressions. labels Jul 4, 2018
@alexch
Copy link
Author

alexch commented Jul 4, 2018

Good to know. Thanks to your explanation, I found a workaround: precede the pasted content with x=:

> x={
... "foo" : false
... }
{ foo: false }

@alexch alexch changed the title Node CLI says "SyntaxError: unexpected symbol :" when parsing multi-line string-keyed objects (e.g. JSON) Node REPL says "SyntaxError: unexpected symbol :" when parsing multi-line string-keyed objects (e.g. JSON) Jul 4, 2018
@vsemozhetbyt
Copy link
Contributor

@rubys
Copy link
Member

rubys commented Jul 7, 2018

The regression was indeed caused by ebfa8b1#diff-b13d72249263845d8e8341db0426f9d3

Attempting to fix this by partially reverting the change by adding in:

if (message.startsWith('Unexpected token')) return true;

does indeed fix this problem, but causes other problems. In particular, the following test will fail:

ebfa8b1#diff-293fefe850253c8c28e2481c0b7f76e2L647

@vsemozhetbyt
Copy link
Contributor

cc @nodejs/repl

@antsmartian
Copy link
Contributor

Ok looks like we need this feature, which is breaking now. I'm not sure about the following test case and its importance over here: https://github.com/nodejs/node/blob/master/test/parallel/test-repl.js#L166. But adding the change like @rubys pointed will break the above test case. Not sure, we can handle both the cases in code, because the above test case expects the expression shouldn't cross the lines(which is what we need). May be we need to take a call on the use cases and features?

@rubys
Copy link
Member

rubys commented Jul 11, 2018

Looking at the code, I don't think it needs to be an either/or. If the use case is to ensure that an incomplete JSON object literal is to be treated as "recoverable", we would need to either extend the isCodeRecoverable function (or create a new function specific to JSON) that understands JSON syntax.

Just so it is clear: JSON syntax is supported when used in the context of a statement (say, an assignment or if statement) is fully supported, the only thing that is not currently supported is input that starts with (or consists exclusively of) a naked JSON object literal.

@BridgeAR
Copy link
Member

Fixed by #21805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Issues related to regressions. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
6 participants