Skip to content

Commit 39afff7

Browse files
updated logic for dot command evaluation
1 parent fa811ff commit 39afff7

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

lib/repl.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ const {
7676
StringPrototypeCharAt,
7777
StringPrototypeEndsWith,
7878
StringPrototypeIncludes,
79-
StringPrototypeIndexOf,
8079
StringPrototypeRepeat,
8180
StringPrototypeSlice,
81+
StringPrototypeSplit,
8282
StringPrototypeStartsWith,
8383
StringPrototypeTrim,
8484
Symbol,
@@ -803,24 +803,19 @@ class REPLServer extends Interface {
803803
}
804804

805805
// Check REPL keywords and empty lines against a trimmed line input.
806-
const trimmedCmd = StringPrototypeTrim(cmd);
806+
let trimmedCmd = StringPrototypeTrim(cmd);
807807

808808
// Check to see if a REPL keyword was used. If it returns true,
809809
// display next prompt and return.
810810
if (trimmedCmd) {
811-
// If condition validates for dot commands at the beginning of the line,
812-
// or dot commands after some whitespace.
813-
const isDotCommandAtStart = StringPrototypeCharAt(trimmedCmd, 0) === '.' &&
814-
StringPrototypeCharAt(trimmedCmd, 1) !== '.';
815-
const dotIndex = StringPrototypeIndexOf(trimmedCmd, '.');
816-
const isDotCommandAfterWhitespace = dotIndex > 0 &&
817-
StringPrototypeCharAt(trimmedCmd, dotIndex + 1) !== '.';
818-
const matches = RegExpPrototypeExec(/(?:^|\s)\.([^\s]+)\s*(.*)$/, trimmedCmd);
819-
const keyword = matches?.[1];
820-
const rest = matches?.[2];
821-
const isValidKeyword = keyword && ObjectKeys(self.commands).includes(keyword);
822-
if ((isDotCommandAtStart || isDotCommandAfterWhitespace && isValidKeyword) &&
823-
NumberIsNaN(NumberParseFloat(trimmedCmd))) {
811+
const splitString = StringPrototypeSplit(trimmedCmd, '\r');
812+
trimmedCmd = splitString[splitString.length - 1];
813+
if (StringPrototypeCharAt(trimmedCmd, 0) === '.' &&
814+
StringPrototypeCharAt(trimmedCmd, 1) !== '.' &&
815+
NumberIsNaN(NumberParseFloat(trimmedCmd))) {
816+
const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd);
817+
const keyword = matches?.[1];
818+
const rest = matches?.[2];
824819
if (FunctionPrototypeCall(_parseREPLKeyword, self, keyword, rest) === true) {
825820
return;
826821
}

test/parallel/test-repl-multiline-dot-commands-execution.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ function runDotCommand(command, validate) {
1212
const { replServer, output } = startNewREPLServer();
1313

1414
replServer.on('exit', common.mustCall());
15+
replServer.write(`${command}\n`);
16+
replServer.write('let testObj = {\n');
17+
replServer.write(`${command}:"dummy-value"\n`);
18+
replServer.write(`}`);
1519
replServer.write('function a() {\n');
1620
replServer.write('console.log("logging");\n');
21+
replServer.write(`let value = testObj ${command} \n`);
22+
replServer.write('console.log(value) \n');
1723
replServer.write(`${command}\n`);
1824
validate(replServer, output);
1925
replServer.write('arr = [1,\n');
20-
replServer.write('console.log("logging");\n');
21-
replServer.write(`${command}\n`);
26+
replServer.write('consdole.log("logging");\n');
2227
validate(replServer, output);
2328
replServer.close();
2429
}

0 commit comments

Comments
 (0)