Skip to content

Commit 4271732

Browse files
princejwesleyevanlucas
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 <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent 0d0c57f commit 4271732

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/repl.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function REPLServer(prompt,
220220
eval_ = eval_ || defaultEval;
221221

222222
function defaultEval(code, context, file, cb) {
223-
var err, result, retry = false;
223+
var err, result, retry = false, input = code, wrappedErr;
224224
// first, create the Script object to check the syntax
225225
while (true) {
226226
try {
@@ -238,14 +238,23 @@ function REPLServer(prompt,
238238
debug('parse error %j', code, e);
239239
if (self.replMode === exports.REPL_MODE_MAGIC &&
240240
e.message === BLOCK_SCOPED_ERROR &&
241-
!retry) {
242-
retry = true;
241+
!retry || self.wrappedCmd) {
242+
if (self.wrappedCmd) {
243+
self.wrappedCmd = false;
244+
// unwrap and try again
245+
code = `${input.substring(1, input.length - 2)}\n`;
246+
wrappedErr = e;
247+
} else {
248+
retry = true;
249+
}
243250
continue;
244251
}
245-
if (isRecoverableError(e, self))
246-
err = new Recoverable(e);
252+
// preserve original error for wrapped command
253+
const error = wrappedErr || e;
254+
if (isRecoverableError(error, self))
255+
err = new Recoverable(error);
247256
else
248-
err = e;
257+
err = error;
249258
}
250259
break;
251260
}
@@ -418,6 +427,7 @@ function REPLServer(prompt,
418427
// to wrap it in parentheses, so that it will be interpreted as
419428
// an expression.
420429
evalCmd = '(' + evalCmd + ')\n';
430+
self.wrappedCmd = true;
421431
} else {
422432
// otherwise we just append a \n so that it will be either
423433
// terminated, or continued onto the next expression if it's an
@@ -435,6 +445,7 @@ function REPLServer(prompt,
435445
debug('finish', e, ret);
436446
self.memory(cmd);
437447

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

test/parallel/test-repl.js

Lines changed: 2 additions & 0 deletions
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)