Browse Source

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
v6.x
Teddy Katz 8 years ago
committed by Myles Borins
parent
commit
fac61118f9
  1. 9
      lib/repl.js
  2. 10
      test/parallel/test-repl.js

9
lib/repl.js

@ -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;
}

10
test/parallel/test-repl.js

@ -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…
Cancel
Save