You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Standalone blocks don't work in node REPL console #5576
Standalone blocks don't play well with the node console.
{varx=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
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.
Activity
bnoordhuis commentedon Mar 6, 2016
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:
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 toxor an ES6 object literal with anxproperty.princejwesley commentedon Mar 7, 2016
Same behaviour in chrome console (wrapped with parenthesis).

princejwesley commentedon Mar 7, 2016
@bnoordhuis see #5581
repl: support standalone blocks
repl: support standalone blocks
repl: support standalone blocks