From e7f45f0a17ab0f98b1e1e1c928108c31fe47edec Mon Sep 17 00:00:00 2001 From: Prince J Wesley Date: Sat, 6 Feb 2016 12:14:27 +0530 Subject: [PATCH] repl: handle quotes within regexp literal PR-URL: https://github.com/nodejs/node/pull/5117 Reviewed-By: Roman Reiss Reviewed-By: James M Snell Reviewed-By: Minwoo Jung --- lib/repl.js | 16 +++++++++++++--- test/parallel/test-repl.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 5d13e74026f65a..6375ab2d2481fe 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -84,6 +84,7 @@ class LineParser { this._literal = null; this.shouldFail = false; this.blockComment = false; + this.regExpLiteral = false; } parseLine(line) { @@ -99,6 +100,11 @@ class LineParser { } if (!this._literal) { + if (this.regExpLiteral && current === '/') { + this.regExpLiteral = false; + previous = null; + continue; + } if (previous === '*' && current === '/') { if (this.blockComment) { this.blockComment = false; @@ -115,13 +121,17 @@ class LineParser { break; } - if (previous === '/' && current === '*') { - this.blockComment = true; + if (previous === '/') { + if (current === '*') { + this.blockComment = true; + } else { + this.regExpLiteral = true; + } previous = null; } } - if (this.blockComment) continue; + if (this.blockComment || this.regExpLiteral) continue; if (current === this._literal) { this._literal = null; diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 5a13597e56966e..2d2fdc97bab31a 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -282,6 +282,19 @@ function error_test() { // access to internal modules without the --expose_internals flag. { client: client_unix, send: 'require("internal/repl")', expect: /^Error: Cannot find module 'internal\/repl'/ }, + // REPL should handle quotes within regexp literal in multiline mode + { client: client_unix, send: "function x(s) {\nreturn s.replace(/'/,'');\n}", + expect: prompt_multiline + prompt_multiline + + 'undefined\n' + prompt_unix }, + { client: client_unix, send: "function x(s) {\nreturn s.replace(/\'/,'');\n}", + expect: prompt_multiline + prompt_multiline + + 'undefined\n' + prompt_unix }, + { client: client_unix, send: 'function x(s) {\nreturn s.replace(/"/,"");\n}', + expect: prompt_multiline + prompt_multiline + + 'undefined\n' + prompt_unix }, + { client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}', + expect: prompt_multiline + prompt_multiline + + 'undefined\n' + prompt_unix }, ]); }