diff --git a/lib/repl.js b/lib/repl.js index 17aab1c409beca..524307accad327 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -78,6 +78,7 @@ const { StringPrototypeIncludes, StringPrototypeRepeat, StringPrototypeSlice, + StringPrototypeSplit, StringPrototypeStartsWith, StringPrototypeTrim, Symbol, @@ -802,11 +803,13 @@ class REPLServer extends Interface { } // Check REPL keywords and empty lines against a trimmed line input. - const trimmedCmd = StringPrototypeTrim(cmd); + let trimmedCmd = StringPrototypeTrim(cmd); // Check to see if a REPL keyword was used. If it returns true, // display next prompt and return. if (trimmedCmd) { + const splitString = StringPrototypeSplit(trimmedCmd, '\r'); + trimmedCmd = splitString[splitString.length - 1]; if (StringPrototypeCharAt(trimmedCmd, 0) === '.' && StringPrototypeCharAt(trimmedCmd, 1) !== '.' && NumberIsNaN(NumberParseFloat(trimmedCmd))) { diff --git a/test/parallel/test-repl-multiline-dot-commands-execution.js b/test/parallel/test-repl-multiline-dot-commands-execution.js new file mode 100644 index 00000000000000..c333707c7405ad --- /dev/null +++ b/test/parallel/test-repl-multiline-dot-commands-execution.js @@ -0,0 +1,47 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { startNewREPLServer } = require('../common/repl'); + + +const dotCommandSyntaxError = + /Uncaught SyntaxError: Unexpected token '\.'/; + + +function runDotCommand(command, validate) { + const { replServer, output } = startNewREPLServer(); + + replServer.on('exit', common.mustCall()); + replServer.write(`${command}\n`); + replServer.write('let testObj = {\n'); + replServer.write(`${command}:"dummy-value"\n`); + replServer.write(`}`); + replServer.write('function a() {\n'); + replServer.write('console.log("logging");\n'); + replServer.write(`let value = testObj ${command} \n`); + replServer.write('console.log(value) \n'); + replServer.write(`${command}\n`); + validate(replServer, output); + replServer.write('arr = [1,\n'); + replServer.write('consdole.log("logging");\n'); + validate(replServer, output); + replServer.close(); +} + +runDotCommand('.break', common.mustCall((replServer, output) => { + replServer.write('1 + 1\n'); + assert.doesNotMatch(output.accumulator, dotCommandSyntaxError); + assert.match(output.accumulator, /2\n/); +}, 2)); + +runDotCommand('.clear', common.mustCall((replServer, output) => { + replServer.write('1 + 1\n'); + assert.doesNotMatch(output.accumulator, dotCommandSyntaxError); + assert.match(output.accumulator, /2\n/); +}, 2)); + +runDotCommand('.help', common.mustCall((replServer, output) => { + replServer.write('1 + 1\n'); + assert.doesNotMatch(output.accumulator, dotCommandSyntaxError); + assert.match(output.accumulator, /2\n/); +}, 2));