-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
Wrong interpretation of { a: 1 }['a']
in the REPL (BlockStatement v.s. ExpressionStatement)
#45964
Comments
Maybe it should print the technically correct answer and also print out a warning that the user may want to wrap into parentheses to get what they actually want? |
I think the current behavior is fine -- the REPL is a human-facing function of Node.js, and should do what users expect it to do most of the time, not necessarily what is technically correct. And I think when a user inputs something that is a valid expression, it's reasonable to assume that they also expect it to be evaluated as an expression, not a full JS program. |
I would say the expected behaviour is to get converted in ExpressionStatement rather than a BlockStatement for a couple of reasons:
|
For historic perspective: the current behavior was deliberately introduced 12 years ago in commit b45698e because the default behavior kept confusing new users again and again and again. |
Closing. The consensus is we're not going to change that. |
Version
v19.2.0
Platform
No response
Subsystem
No response
What steps will reproduce the bug?
Evaluate
{ a: 1 }['a']
in the REPLHow often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
What do you see instead?
Additional information
The REPL tries to wrap the code that starts with
{
in parentheses eagerly ('eager wrapping' was introduced at #31943) and{ a: 1 }['a']
is converted into an ExpressionStatement({ a: 1 }['a'])
that is evaluated to1
. However, technically speaking,{ a: 1 }['a']
should be parsed as the combination of a BlockStatement{a:1}
and an ExpressionStatement['a']
, yielding a result of['a']
.The results of
{ a: 1 }['a']
in other runtimes:['a']
['a']
1
The text was updated successfully, but these errors were encountered: