Browse Source

repl: support standalone blocks

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: https://github.com/nodejs/node/issues/5576
PR-URL: https://github.com/nodejs/node/pull/5581
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
process-exit-stdio-flushing
Prince J Wesley 9 years ago
committed by James M Snell
parent
commit
a38d6ad4be
  1. 23
      lib/repl.js
  2. 2
      test/parallel/test-repl.js

23
lib/repl.js

@ -222,7 +222,7 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval; eval_ = eval_ || defaultEval;
function defaultEval(code, context, file, cb) { function defaultEval(code, context, file, cb) {
var err, result, retry = false; var err, result, retry = false, input = code, wrappedErr;
// first, create the Script object to check the syntax // first, create the Script object to check the syntax
while (true) { while (true) {
try { try {
@ -240,14 +240,23 @@ function REPLServer(prompt,
debug('parse error %j', code, e); debug('parse error %j', code, e);
if (self.replMode === exports.REPL_MODE_MAGIC && if (self.replMode === exports.REPL_MODE_MAGIC &&
e.message === BLOCK_SCOPED_ERROR && e.message === BLOCK_SCOPED_ERROR &&
!retry) { !retry || self.wrappedCmd) {
retry = true; if (self.wrappedCmd) {
self.wrappedCmd = false;
// unwrap and try again
code = `${input.substring(1, input.length - 2)}\n`;
wrappedErr = e;
} else {
retry = true;
}
continue; continue;
} }
if (isRecoverableError(e, self)) // preserve original error for wrapped command
err = new Recoverable(e); const error = wrappedErr || e;
if (isRecoverableError(error, self))
err = new Recoverable(error);
else else
err = e; err = error;
} }
break; break;
} }
@ -420,6 +429,7 @@ function REPLServer(prompt,
// to wrap it in parentheses, so that it will be interpreted as // to wrap it in parentheses, so that it will be interpreted as
// an expression. // an expression.
evalCmd = '(' + evalCmd + ')\n'; evalCmd = '(' + evalCmd + ')\n';
self.wrappedCmd = true;
} else { } else {
// otherwise we just append a \n so that it will be either // otherwise we just append a \n so that it will be either
// terminated, or continued onto the next expression if it's an // terminated, or continued onto the next expression if it's an
@ -437,6 +447,7 @@ function REPLServer(prompt,
debug('finish', e, ret); debug('finish', e, ret);
self.memory(cmd); self.memory(cmd);
self.wrappedCmd = false;
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) { if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
self.outputStream.write('npm should be run outside of the ' + self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' + 'node repl, in your normal shell.\n' +

2
test/parallel/test-repl.js

@ -323,6 +323,8 @@ function error_test() {
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}', { client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
expect: prompt_multiline + prompt_multiline + expect: prompt_multiline + prompt_multiline +
'undefined\n' + prompt_unix }, 'undefined\n' + prompt_unix },
{ client: client_unix, send: '{ var x = 4; }',
expect: 'undefined\n' + prompt_unix },
]); ]);
} }

Loading…
Cancel
Save