Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: avoid parsing division operator as regex #10103

Merged
merged 1 commit into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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