Skip to content

Wrong interpretation of { a: 1 }['a'] in the REPL (BlockStatement v.s. ExpressionStatement) #45964

Description

@cola119

Version

v19.2.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

Evaluate { a: 1 }['a'] in the REPL

$ node
> { a: 1 }['a']

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

$ node -p "{ a: 1 }['a']"
[ 'a' ]
$ node
> { a: 1 }['a']
[ 'a' ]

What do you see instead?

$ node -p "{ a: 1 }['a']"
[ 'a' ]
$ node
> { a: 1 }['a']
1

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 to 1. 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:

  • ChromeDevTools: ['a']
  • Safari WebInspector: ['a']
  • Deno REPL: 1

Activity

  1. added
    replIssues and PRs related to the REPL subsystem.
    on Dec 24, 2022
  2. aduh95 commented on Dec 25, 2022

    @aduh95
    Contributor

    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?

  3. addaleax commented on Dec 25, 2022

    @addaleax
    Member

    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.

  4. Dsstefanov commented on Dec 26, 2022

    @Dsstefanov

    I would say the expected behaviour is to get converted in ExpressionStatement rather than a BlockStatement for a couple of reasons:

    1. Current developers are already familiar with the functionality of it being parsed as ExpressionStatement.
    2. If you use EOL operator it will get parsed according to your expectation to BlockStatement
    3. Like @addaleax mentioned if a valid expression is inserted it is intuitive it will get parsed as ExpressionStatement.
  5. bnoordhuis commented on Dec 27, 2022

    @bnoordhuis
    Member

    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.

  6. bnoordhuis commented on Jan 1, 2023

    @bnoordhuis
    Member

    Closing. The consensus is we're not going to change that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions