Skip to content

Commit 30d239b

Browse files
improved validation logic for dot operator commands
1 parent 4fa9ef6 commit 30d239b

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

lib/repl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,12 +815,12 @@ class REPLServer extends Interface {
815815
const dotIndex = StringPrototypeIndexOf(trimmedCmd, '.');
816816
const isDotCommandAfterWhitespace = dotIndex > 0 &&
817817
StringPrototypeCharAt(trimmedCmd, dotIndex + 1) !== '.';
818-
819-
if ((isDotCommandAtStart || isDotCommandAfterWhitespace) &&
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) &&
820823
NumberIsNaN(NumberParseFloat(trimmedCmd))) {
821-
const matches = RegExpPrototypeExec(/(?:^|\s)\.([^\s]+)\s*(.*)$/, trimmedCmd);
822-
const keyword = matches?.[1];
823-
const rest = matches?.[2];
824824
if (FunctionPrototypeCall(_parseREPLKeyword, self, keyword, rest) === true) {
825825
return;
826826
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ function runDotCommand(command, validate) {
1313

1414
replServer.on('exit', common.mustCall());
1515
replServer.write('function a() {\n');
16+
replServer.write('console.log("logging");\n');
1617
replServer.write(`${command}\n`);
1718
validate(replServer, output);
1819
replServer.write('arr = [1,\n');
20+
replServer.write('console.log("logging");\n');
1921
replServer.write(`${command}\n`);
2022
validate(replServer, output);
2123
replServer.close();

0 commit comments

Comments
 (0)