Skip to content

Commit c551da8

Browse files
princejwesleyrvagg
authored andcommittedFeb 9, 2016
repl: handle quotes within regexp literal
PR-URL: #5117 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 287bce7 commit c551da8

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed
 

‎lib/repl.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class LineParser {
8484
this._literal = null;
8585
this.shouldFail = false;
8686
this.blockComment = false;
87+
this.regExpLiteral = false;
8788
}
8889

8990
parseLine(line) {
@@ -99,6 +100,11 @@ class LineParser {
99100
}
100101

101102
if (!this._literal) {
103+
if (this.regExpLiteral && current === '/') {
104+
this.regExpLiteral = false;
105+
previous = null;
106+
continue;
107+
}
102108
if (previous === '*' && current === '/') {
103109
if (this.blockComment) {
104110
this.blockComment = false;
@@ -115,13 +121,17 @@ class LineParser {
115121
break;
116122
}
117123

118-
if (previous === '/' && current === '*') {
119-
this.blockComment = true;
124+
if (previous === '/') {
125+
if (current === '*') {
126+
this.blockComment = true;
127+
} else {
128+
this.regExpLiteral = true;
129+
}
120130
previous = null;
121131
}
122132
}
123133

124-
if (this.blockComment) continue;
134+
if (this.blockComment || this.regExpLiteral) continue;
125135

126136
if (current === this._literal) {
127137
this._literal = null;

‎test/parallel/test-repl.js

+13
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ function error_test() {
287287
// access to internal modules without the --expose_internals flag.
288288
{ client: client_unix, send: 'require("internal/repl")',
289289
expect: /^Error: Cannot find module 'internal\/repl'/ },
290+
// REPL should handle quotes within regexp literal in multiline mode
291+
{ client: client_unix, send: "function x(s) {\nreturn s.replace(/'/,'');\n}",
292+
expect: prompt_multiline + prompt_multiline +
293+
'undefined\n' + prompt_unix },
294+
{ client: client_unix, send: "function x(s) {\nreturn s.replace(/\'/,'');\n}",
295+
expect: prompt_multiline + prompt_multiline +
296+
'undefined\n' + prompt_unix },
297+
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/"/,"");\n}',
298+
expect: prompt_multiline + prompt_multiline +
299+
'undefined\n' + prompt_unix },
300+
{ client: client_unix, send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
301+
expect: prompt_multiline + prompt_multiline +
302+
'undefined\n' + prompt_unix },
290303
]);
291304
}
292305

0 commit comments

Comments
 (0)
Please sign in to comment.