Skip to content

Commit fcfd87d

Browse files
thefourtheyesilverwind
authored andcommitted
repl: backslash bug fix
The actual problem was with the line parsing logic for string literals. When we use backslash in the string literals, it used to remember the `\` as the previous character even after we parsed the character next to it. This leads to REPL thinking that the end of string literals is not reached. This patch replaces the previous character with `null`, so that it will properly skip the character next to it. Previous Discussion: #2952 Fixes: #2749 PR-URL: #2968 Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent 75fe773 commit fcfd87d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ function REPLServer(prompt,
288288

289289
for (var i = 0; i < line.length; i += 1) {
290290
if (previous === '\\') {
291-
// if it is a valid escaping, then skip processing
292-
previous = current;
291+
// if it is a valid escaping, then skip processing and the previous
292+
// character doesn't matter anymore.
293+
previous = null;
293294
continue;
294295
}
295296

test/parallel/test-repl.js

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ function error_test() {
242242
'RegExp.$6\nRegExp.$7\nRegExp.$8\nRegExp.$9\n',
243243
expect: ['\'1\'\n', '\'2\'\n', '\'3\'\n', '\'4\'\n', '\'5\'\n', '\'6\'\n',
244244
'\'7\'\n', '\'8\'\n', '\'9\'\n'].join(`${prompt_unix}`) },
245+
// regression tests for https://github.com/nodejs/node/issues/2749
246+
{ client: client_unix, send: 'function x() {\nreturn \'\\n\';\n }',
247+
expect: prompt_multiline + prompt_multiline +
248+
'undefined\n' + prompt_unix },
249+
{ client: client_unix, send: 'function x() {\nreturn \'\\\\\';\n }',
250+
expect: prompt_multiline + prompt_multiline +
251+
'undefined\n' + prompt_unix },
245252
]);
246253
}
247254

0 commit comments

Comments
 (0)