repl: avoid parsing division operator as regex
This improves the heuristic used in multiline-prompt mode to determine whether a given slash character is at the beginning of a regular expression. PR-URL: https://github.com/nodejs/node/pull/10103 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: James M Snell <jasnell@keybase.io> Fixes: https://github.com/nodejs/node/issues/9300
This commit is contained in:
parent
7a23c0a800
commit
9d493d0064
@ -93,6 +93,7 @@ class LineParser {
|
||||
this.shouldFail = false;
|
||||
this.blockComment = false;
|
||||
this.regExpLiteral = false;
|
||||
this.prevTokenChar = null;
|
||||
}
|
||||
|
||||
parseLine(line) {
|
||||
@ -132,7 +133,11 @@ class LineParser {
|
||||
if (previous === '/') {
|
||||
if (current === '*') {
|
||||
this.blockComment = true;
|
||||
} else {
|
||||
} else if (
|
||||
// Distinguish between a division operator and the start of a regex
|
||||
// by examining the non-whitespace character that precedes the /
|
||||
[null, '(', '[', '{', '}', ';'].includes(this.prevTokenChar)
|
||||
) {
|
||||
this.regExpLiteral = true;
|
||||
}
|
||||
previous = null;
|
||||
@ -147,6 +152,8 @@ class LineParser {
|
||||
this._literal = this._literal || current;
|
||||
}
|
||||
|
||||
if (current.trim() && current !== '/') this.prevTokenChar = current;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
|
@ -352,6 +352,16 @@ function error_test() {
|
||||
|
||||
{ client: client_unix, send: 'function * foo() {}; foo().next()',
|
||||
expect: '{ value: undefined, done: true }' },
|
||||
|
||||
// https://github.com/nodejs/node/issues/9300
|
||||
{ client: client_unix, send: 'function foo() {\nvar bar = 1 / 1; // "/"\n}',
|
||||
expect: prompt_multiline + prompt_multiline + 'undefined\n' + prompt_unix },
|
||||
|
||||
{ client: client_unix, send: '(function() {\nreturn /foo/ / /bar/;\n}())',
|
||||
expect: prompt_multiline + prompt_multiline + 'NaN\n' + prompt_unix },
|
||||
|
||||
{ client: client_unix, send: '(function() {\nif (false) {} /bar"/;\n}())',
|
||||
expect: prompt_multiline + prompt_multiline + 'undefined\n' + prompt_unix }
|
||||
]);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user