Skip to content

Commit e8c91e7

Browse files
Trottevanlucas
authored andcommitted
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: #7104 Fixes: #3611 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 1600966 commit e8c91e7

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/repl.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,11 @@ REPLServer.prototype.convertToContext = function(cmd) {
11541154
return cmd;
11551155
};
11561156

1157+
function bailOnIllegalToken(parser) {
1158+
return parser._literal === null &&
1159+
!parser.blockComment &&
1160+
!parser.regExpLiteral;
1161+
}
11571162

11581163
// If the error is that we've unexpectedly ended the input,
11591164
// then let the user try to recover by adding more input.
@@ -1166,9 +1171,16 @@ function isRecoverableError(e, self) {
11661171
return true;
11671172
}
11681173

1169-
return message.startsWith('Unexpected end of input') ||
1170-
message.startsWith('Unexpected token') ||
1171-
message.startsWith('missing ) after argument list');
1174+
if (message.startsWith('Unexpected end of input') ||
1175+
message.startsWith('missing ) after argument list'))
1176+
return true;
1177+
1178+
if (message.startsWith('Unexpected token')) {
1179+
if (message.includes('ILLEGAL') && bailOnIllegalToken(self.lineParser))
1180+
return false;
1181+
else
1182+
return true;
1183+
}
11721184
}
11731185
return false;
11741186
}

test/parallel/test-repl.js

+4
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ function error_test() {
324324
'undefined\n' + prompt_unix },
325325
{ client: client_unix, send: '{ var x = 4; }',
326326
expect: 'undefined\n' + prompt_unix },
327+
// Illegal token is not recoverable outside string literal, RegExp literal,
328+
// or block comment. https://github.com/nodejs/node/issues/3611
329+
{ client: client_unix, send: 'a = 3.5e',
330+
expect: /^SyntaxError: Unexpected token ILLEGAL/ },
327331
]);
328332
}
329333

0 commit comments

Comments
 (0)