-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
repl: support mult-line string-keyed objects #21775
Conversation
lib/repl.js
Outdated
@@ -1521,12 +1521,161 @@ function isRecoverableError(e, code) { | |||
return pos > 0 && frames[pos - 1].length === frames[pos].length; | |||
} | |||
|
|||
if (message.startsWith('Unexpected token')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In version v8.11.3, without this particular check, things works. I'm wondering, now with this code in place, we are sort of re-doing the check (may be?). May be I'm wrong here.
Actually as discussed in the issue, this change broken the partial JSON stuff. The diff is over here:
ebfa8b1#diff-293fefe850253c8c28e2481c0b7f76e2L647
As seen in the comments of PR, looks like partial template literal also broken in latest version. For example:
`io.js ${a
... }`
works in v8/9. But broken in v10x.
@nodejs/repl |
@rubys In general, the idea is great. Because this is going in a direction where the code is starting to be properly parsed. I am not sure though if this is the right approach in the current state of the repl. The reason is that the repl itself has so many hacks that it's a nightmare. It would be really awesome if you would be able to use acorn instead. I am not sure if that would help in this specific moment but I'll have a look into that tomorrow and maybe you could also check that? |
@BridegeAR: you are being too kind. It clearly is a hack. Ideally, V8 would be implemented as a streaming parser, but that isn't required for the browser. As Acorn isn't a streaming parser, it isn't likely to be able to help us here. The root problem is that the JavaScript grammar is ambiguous (at least at the tokenization level), and backtracking is sometimes needed. Examples of valid JavaScript: a=1
{a*2}
{a, a}
{a, a*2}
label: {a, a} Only one of the lines above is an object literal. |
@rubys btw we're working on rewriting the REPL from scratch: https://github.com/nodejs/repl it might be a good place to experiment with different ways of consuming input, as its still fairly small. |
I may have been too quick to discount acorn; it will probably need a patch to be landed to support this use case, but it looks doable. I'm traveling for the next four days, so it may not be until next week that I can take a look at this. |
lib/repl.js
Outdated
// Check whether a code snippet is a partially complete JSON file. | ||
// Consume the code provided using http://json.org/ until there is | ||
// either an error or nothing left. | ||
function isPartialJSON(code) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we split this out into a separate file in lib/internal/repl/
?
Update: played with acorn on the first flight leg on this trip. Everything needed can be done with a plugin. Will create a separate pull request either later today or this weekend. |
See #21805 for an acorn based implementation. |
fixes #21657
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes