Skip to content

Commit

Permalink
repl: support standalone blocks
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
princejwesley authored and evanlucas committed Mar 31, 2016
1 parent 0d0c57f commit 4271732
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval;

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
while (true) {
try {
Expand All @@ -238,14 +238,23 @@ function REPLServer(prompt,
debug('parse error %j', code, e);
if (self.replMode === exports.REPL_MODE_MAGIC &&
e.message === BLOCK_SCOPED_ERROR &&
!retry) {
retry = true;
!retry || self.wrappedCmd) {
if (self.wrappedCmd) {
self.wrappedCmd = false;
// unwrap and try again
code = `${input.substring(1, input.length - 2)}\n`;
wrappedErr = e;
} else {
retry = true;
}
continue;
}
if (isRecoverableError(e, self))
err = new Recoverable(e);
// preserve original error for wrapped command
const error = wrappedErr || e;
if (isRecoverableError(error, self))
err = new Recoverable(error);
else
err = e;
err = error;
}
break;
}
Expand Down Expand Up @@ -418,6 +427,7 @@ function REPLServer(prompt,
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
evalCmd = '(' + evalCmd + ')\n';
self.wrappedCmd = true;
} else {
// otherwise we just append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
Expand All @@ -435,6 +445,7 @@ function REPLServer(prompt,
debug('finish', e, ret);
self.memory(cmd);

self.wrappedCmd = false;
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' +
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ function error_test() {
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
expect: prompt_multiline + prompt_multiline +
'undefined\n' + prompt_unix },
{ client: client_unix, send: '{ var x = 4; }',
expect: 'undefined\n' + prompt_unix },
]);
}

Expand Down

0 comments on commit 4271732

Please sign in to comment.