From 427173204e3b06484b3348fdb0b01b95cea59a5e Mon Sep 17 00:00:00 2001 From: Prince J Wesley Date: Mon, 7 Mar 2016 10:31:33 +0530 Subject: [PATCH] repl: support standalone blocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Evan Lucas --- lib/repl.js | 23 +++++++++++++++++------ test/parallel/test-repl.js | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 35cde4c1df0ec0..7b3b7d11739add 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 }, ]); }