Skip to content

Commit

Permalink
repl: avoid parsing division operator as regex
Browse files Browse the repository at this point in the history
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: nodejs#10103
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: James M Snell <jasnell@keybase.io>
Fixes: nodejs#9300
  • Loading branch information
not-an-aardvark authored and Jonathan Darling committed Dec 8, 2016
1 parent ec0cc9b commit 8b38047
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class LineParser {
this.shouldFail = false;
this.blockComment = false;
this.regExpLiteral = false;
this.prevTokenChar = null;
}

parseLine(line) {
Expand Down Expand Up @@ -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;
Expand All @@ -147,6 +152,8 @@ class LineParser {
this._literal = this._literal || current;
}

if (current.trim() && current !== '/') this.prevTokenChar = current;

previous = current;
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]);
}

Expand Down

0 comments on commit 8b38047

Please sign in to comment.