Skip to content

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

Closed as not planned
@cola119

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

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