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

{} + [] REPL bug? #33798

Closed
hemanth opened this issue Jun 8, 2020 · 11 comments
Closed

{} + [] REPL bug? #33798

hemanth opened this issue Jun 8, 2020 · 11 comments
Labels
repl Issues and PRs related to the REPL subsystem.

Comments

@hemanth
Copy link
Contributor

hemanth commented Jun 8, 2020

In the REPL, on 12+ version, I am seeing the below.

> {} + []
'[object Object]'

^ This should be 0

image

  • Version: 12+
  • Platform: 18.7.0 Darwin
@hemanth hemanth changed the title {} + [] REPL issue? {} + [] REPL bug? Jun 8, 2020
@ljharb
Copy link
Member

ljharb commented Jun 8, 2020

This also manifests when you do console.log({} + []) which prints '[object Object]' instead of 0, in many many node versions.

The direct repl behavior seems limited to 12+.

@ljharb
Copy link
Member

ljharb commented Jun 8, 2020

I've been informed that in fact this is correct; {} + [] in a statement context is "a block" and then +[], which is 0, but in an expression context (or as ({}) + [] is '[object Object]'.

@hemanth
Copy link
Contributor Author

hemanth commented Jun 8, 2020

Also:

> [{}+{}, {}+[]]
[ '[object Object][object Object]', '[object Object]' ]

@devsnek
Copy link
Member

devsnek commented Jun 8, 2020

This is not a bug. String({}) is '[object Object]' and String([]) is ''.

@hemanth
Copy link
Contributor Author

hemanth commented Jun 8, 2020

Nods, [{} + []] will be '[object Object]' but {} + [] in REPL should be 0?

@devsnek
Copy link
Member

devsnek commented Jun 8, 2020

REPL is an expression context, the input is actually ({} + []).

@BridgeAR
Copy link
Member

BridgeAR commented Jun 8, 2020

We handle REPL input as expression context as @devsnek pointed out. See

node/lib/repl.js

Lines 355 to 358 in 651088c

// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression. Note that if the above condition changes,

This also applies to e.g., { b: 1 }.b.

@hemanth
Copy link
Contributor Author

hemanth commented Jun 8, 2020

As @ljharb highlighted, on node <v12 {} + [] evaluates to 0 on the REPL [devtools might a different story]

@addaleax addaleax added the repl Issues and PRs related to the REPL subsystem. label Jun 8, 2020
@addaleax
Copy link
Member

addaleax commented Jun 8, 2020

As @devsnek and @BridgeAR pointed out, this is not a bug, but intentional – the REPL prefers to evaluate as expressions before evaluating as statements. So the current one is the expected behavior, and the pre-v12.x one is considered the buggy one.

I’ll close this as I don’t think there’s anything we can do here.

@addaleax addaleax closed this as completed Jun 8, 2020
@ljharb
Copy link
Member

ljharb commented Jun 8, 2020

hmm - i don't see this REPL change listed in the blog post: https://nodejs.org/en/blog/release/v12.0.0/

Clearly it's the right fix to make, and I assume its omission is an oversight - could someone link me to the PR that changed it?

@hemanth
Copy link
Contributor Author

hemanth commented May 26, 2023

Was there a PR for this? @ljharb :D

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

No branches or pull requests

5 participants