diff --git a/lib/repl.js b/lib/repl.js index ab9f1bf56dce1e..290cecd69c3a9a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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 { @@ -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; } @@ -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 @@ -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' + diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 7ec9f1c3637349..ab7df802ada8d5 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -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 }, ]); }