From b5175003bca45e23bd20d9559096dafda3e0ad7c Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 6 Jun 2014 23:00:55 -0700 Subject: [PATCH] repl: fix multi-line input The refactor in 3ae0b17c broke the multiline input's visual appearence. While actually switching to this mode, the `...` prefix is not displayed. Additionally, account only SyntaxErrors that are happening at the parse time, everything else should not be switching repl to the multiline mode. Signed-off-by: Fedor Indutny --- lib/repl.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index b458d1f..6d49bda 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -120,8 +120,11 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { displayErrors: false }); } catch (e) { - err = e; debug('parse error %j', code, e); + if (isRecoverableError(e)) + err = new Recoverable(e); + else + err = e; } if (!err) { @@ -294,7 +297,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) { // If error was SyntaxError and not JSON.parse error if (e) { - if (isRecoverableError(e)) { + if (e instanceof Recoverable) { // Start buffering data like that: // { // ... x: 1 @@ -386,15 +389,16 @@ REPLServer.prototype.resetContext = function() { }; REPLServer.prototype.displayPrompt = function(preserveCursor) { - var prompt = this._prompt; + var initial = this._prompt; + var prompt = initial; if (this.bufferedCommand.length) { prompt = '...'; var levelInd = new Array(this.lines.level.length).join('..'); prompt += levelInd + ' '; - } else { - this.setPrompt(prompt); } + this.setPrompt(prompt); this.prompt(preserveCursor); + this.setPrompt(initial); }; // A stream to push an array into a REPL @@ -940,5 +944,10 @@ REPLServer.prototype.convertToContext = function(cmd) { function isRecoverableError(e) { return e && e.name === 'SyntaxError' && - /^Unexpected end of input/.test(e.message); + /^(Unexpected end of input|Unexpected token :)/.test(e.message); +} + +function Recoverable(err) { + this.err = err; } +inherits(Recoverable, SyntaxError); -- 2.7.4