Skip to content

Standalone blocks don't work in node REPL console #5576

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
polybuildr opened this issue Mar 6, 2016 · 3 comments
Closed

Standalone blocks don't work in node REPL console #5576

polybuildr opened this issue Mar 6, 2016 · 3 comments
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.

Comments

@polybuildr
Copy link

Standalone blocks don't play well with the node console.

{ var x = 4; console.log(x); }

works fine when put in a file and run, but when run in the console,

SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:24:10)
    at REPLServer.defaultEval (repl.js:137:25)
    at bound (domain.js:250:14)
    at REPLServer.runBound [as eval] (domain.js:263:12)
    at REPLServer.<anonymous> (repl.js:393:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)
  • Version: v4.1.1
  • Platform: Linux matrix 3.13.0-79-generic #123-Ubuntu SMP Fri Feb 19 14:27:58 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
@mscdex mscdex added repl Issues and PRs related to the REPL subsystem. confirmed-bug Issues with confirmed bugs. labels Mar 6, 2016
@bnoordhuis
Copy link
Member

The REPL wraps input in parentheses, otherwise {} would be interpreted as an empty statement instead of an object literal (which is what most people expect - the evaluator was changed years ago after receiving many, many bug reports about that.)

You can see it for yourself when you run the REPL with NODE_DEBUG=repl set in the environment:

$ env NODE_DEBUG=repl out/Release/node 
> { x: 42 }
REPL 5133: line "{ x: 42 }"
REPL 5133: eval "({ x: 42 })\n"
REPL 5133: finish undefined { x: 42 }
{ x: 42 }

> { var x = 4; console.log(x); }
REPL 5133: line "{ var x = 4; console.log(x); }"
REPL 5133: eval "({ var x = 4; console.log(x); })\n"
REPL 5133: parse error "({ var x = 4; console.log(x); })\n" SyntaxError: Unexpected identifier
<elided>

A possible solution is to retry without parens on SyntaxError but there are some inputs that are ambiguous either way, e.g., { x } can either be a block statement evaluating to x or an ES6 object literal with an x property.

@princejwesley
Copy link
Contributor

Same behaviour in chrome console (wrapped with parenthesis).
image

@princejwesley
Copy link
Contributor

@bnoordhuis see #5581

evanlucas pushed a commit that referenced this issue Mar 30, 2016
Enable support for standalone block statements.

```js
node 🙈 ₹ git:(upstream ⚡ bare-block) ./node
> { var x = 3; console.log(x); }
3
undefined
> {}
{}
> { x:1, y:"why not", z: function() {} }
{ x: 1, y: 'why not', z: [Function] }
>
```
For the ambiguous inputs like `{ x }`, the existing REPL
behaviour (ES6 literal shorthand) is preserved (prefers
expression over statement).

Fixes: #5576
PR-URL: #5581
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
evanlucas pushed a commit that referenced this issue Mar 31, 2016
Enable support for standalone block statements.

```js
node 🙈 ₹ git:(upstream ⚡ bare-block) ./node
> { var x = 3; console.log(x); }
3
undefined
> {}
{}
> { x:1, y:"why not", z: function() {} }
{ x: 1, y: 'why not', z: [Function] }
>
```
For the ambiguous inputs like `{ x }`, the existing REPL
behaviour (ES6 literal shorthand) is preserved (prefers
expression over statement).

Fixes: #5576
PR-URL: #5581
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants