Skip to content

Commit a38d6ad

Browse files
princejwesleyjasnell
authored andcommitted
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: #5576 PR-URL: #5581 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent 9e30129 commit a38d6ad

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/repl.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function REPLServer(prompt,
222222
eval_ = eval_ || defaultEval;
223223

224224
function defaultEval(code, context, file, cb) {
225-
var err, result, retry = false;
225+
var err, result, retry = false, input = code, wrappedErr;
226226
// first, create the Script object to check the syntax
227227
while (true) {
228228
try {
@@ -240,14 +240,23 @@ function REPLServer(prompt,
240240
debug('parse error %j', code, e);
241241
if (self.replMode === exports.REPL_MODE_MAGIC &&
242242
e.message === BLOCK_SCOPED_ERROR &&
243-
!retry) {
244-
retry = true;
243+
!retry || self.wrappedCmd) {
244+
if (self.wrappedCmd) {
245+
self.wrappedCmd = false;
246+
// unwrap and try again
247+
code = `${input.substring(1, input.length - 2)}\n`;
248+
wrappedErr = e;
249+
} else {
250+
retry = true;
251+
}
245252
continue;
246253
}
247-
if (isRecoverableError(e, self))
248-
err = new Recoverable(e);
254+
// preserve original error for wrapped command
255+
const error = wrappedErr || e;
256+
if (isRecoverableError(error, self))
257+
err = new Recoverable(error);
249258
else
250-
err = e;
259+
err = error;
251260
}
252261
break;
253262
}
@@ -420,6 +429,7 @@ function REPLServer(prompt,
420429
// to wrap it in parentheses, so that it will be interpreted as
421430
// an expression.
422431
evalCmd = '(' + evalCmd + ')\n';
432+
self.wrappedCmd = true;
423433
} else {
424434
// otherwise we just append a \n so that it will be either
425435
// terminated, or continued onto the next expression if it's an
@@ -437,6 +447,7 @@ function REPLServer(prompt,
437447
debug('finish', e, ret);
438448
self.memory(cmd);
439449

450+
self.wrappedCmd = false;
440451
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
441452
self.outputStream.write('npm should be run outside of the ' +
442453
'node repl, in your normal shell.\n' +

test/parallel/test-repl.js

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ function error_test() {
323323
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
324324
expect: prompt_multiline + prompt_multiline +
325325
'undefined\n' + prompt_unix },
326+
{ client: client_unix, send: '{ var x = 4; }',
327+
expect: 'undefined\n' + prompt_unix },
326328
]);
327329
}
328330

0 commit comments

Comments
 (0)