From 320e3a143b97ab53d41e175d18ce22f2de6b59d2 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 1 Jun 2016 23:13:45 -0700 Subject: [PATCH 1/2] repl: refine handling of illegal tokens Illegal tokens are only recoverable in string literals, RegExp literals, and block comments. If not in one of these constructs, immediately return an error rather than giving the user false hope by giving them a chance to try to recover. Fixes: https://github.com/nodejs/node/issues/3611 --- lib/repl.js | 18 +++++++++++++++--- test/parallel/test-repl.js | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index b58cf499cda226..0a1a247f0eeeff 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1154,6 +1154,9 @@ REPLServer.prototype.convertToContext = function(cmd) { return cmd; }; +function bailOnIllegalToken(lp) { + return lp._literal === null && !lp.blockComment && !lp.regExpLiteral; +} // If the error is that we've unexpectedly ended the input, // then let the user try to recover by adding more input. @@ -1166,9 +1169,18 @@ function isRecoverableError(e, self) { return true; } - return message.startsWith('Unexpected end of input') || - message.startsWith('Unexpected token') || - message.startsWith('missing ) after argument list'); + if (message.startsWith('Unexpected end of input')) + return true; + + if (message.startsWith('Unexpected token')) { + if (message.includes('ILLEGAL') && bailOnIllegalToken(self.lineParser)) + return false; + else + return true; + } + + if (message.startsWith('missing ) after argument list')) + return true; } return false; } diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 2ec996897a1cae..fe4bcd32deda12 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -324,6 +324,10 @@ function error_test() { 'undefined\n' + prompt_unix }, { client: client_unix, send: '{ var x = 4; }', expect: 'undefined\n' + prompt_unix }, + // Illegal token is not recoverable outside string literal, RegExp literal, + // or block comment. https://github.com/nodejs/node/issues/3611 + { client: client_unix, send: 'a = 3.5e', + expect: /^SyntaxError: Unexpected token ILLEGAL/ }, ]); } From 75e4759d2617a59cecc2896e7c79aa807b69d68d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 3 Jun 2016 21:25:37 -0700 Subject: [PATCH 2/2] squash: nits --- lib/repl.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 0a1a247f0eeeff..387e3b5446f678 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -1154,8 +1154,10 @@ REPLServer.prototype.convertToContext = function(cmd) { return cmd; }; -function bailOnIllegalToken(lp) { - return lp._literal === null && !lp.blockComment && !lp.regExpLiteral; +function bailOnIllegalToken(parser) { + return parser._literal === null && + !parser.blockComment && + !parser.regExpLiteral; } // If the error is that we've unexpectedly ended the input, @@ -1169,7 +1171,8 @@ function isRecoverableError(e, self) { return true; } - if (message.startsWith('Unexpected end of input')) + if (message.startsWith('Unexpected end of input') || + message.startsWith('missing ) after argument list')) return true; if (message.startsWith('Unexpected token')) { @@ -1178,9 +1181,6 @@ function isRecoverableError(e, self) { else return true; } - - if (message.startsWith('missing ) after argument list')) - return true; } return false; }