Skip to content

Commit

Permalink
repl: handle quotes within regexp literal
Browse files Browse the repository at this point in the history
PR-URL: #5117
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
  • Loading branch information
princejwesley authored and rvagg committed Feb 9, 2016
1 parent 287bce7 commit c551da8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class LineParser {
this._literal = null;
this.shouldFail = false;
this.blockComment = false;
this.regExpLiteral = false;
}

parseLine(line) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,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 },
]);
}

Expand Down

0 comments on commit c551da8

Please sign in to comment.