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. PR-URL: https://github.com/nodejs/node/pull/7104 Fixes: https://github.com/nodejs/node/issues/3611 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
ac0665c908
commit
de0aa23ad7
18
lib/repl.js
18
lib/repl.js
@ -1154,6 +1154,11 @@ REPLServer.prototype.convertToContext = function(cmd) {
|
||||
return cmd;
|
||||
};
|
||||
|
||||
function bailOnIllegalToken(parser) {
|
||||
return parser._literal === null &&
|
||||
!parser.blockComment &&
|
||||
!parser.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 +1171,16 @@ 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') ||
|
||||
message.startsWith('missing ) after argument list'))
|
||||
return true;
|
||||
|
||||
if (message.startsWith('Unexpected token')) {
|
||||
if (message.includes('ILLEGAL') && bailOnIllegalToken(self.lineParser))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -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/ },
|
||||
]);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user