diff --git a/lib/repl.js b/lib/repl.js index 9c58105655b811..b3be81ddcfcdce 100644 --- a/lib/repl.js +++ b/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; } diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 4342910d5dac03..70aac915f6b594 100644 --- a/test/parallel/test-repl.js +++ b/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 } ]); }