Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function REPLServer(prompt,

// Check to see if a REPL keyword was used. If it returns true,
// display next prompt and return.
if (cmd && cmd.charAt(0) === '.' && isNaN(parseFloat(cmd))) {
if (cmd && isNaN(parseFloat(cmd)) && /^\.[a-z]+\s*[^(]*$/.test(cmd)) {
var matches = cmd.match(/^\.([^\s]+)\s*(.*)$/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parse regex is far too lenient to begin with, and that we could probably only use one regex. Furthermore, why don't we just put this within if (!wasWithinStrLiteral && !isWithinStrLiteral) { above? That way we can catch it before anything is trimmed.

I'm suggesting something like:

if (!wasWithinStrLiteral && !isWithinStrLiteral) {
  if (cmd && isNaN(parseFloat(cmd)) {
    const matches = /^\.[a-z-_]+\s*[^(){}[];=|&]*$/.exec(cmd);
    ...
  }
  cmd = cmd.trim();
}

as an aside, we should probably just check before everything if (\^\s*$\.test(cmd)) (or (cmd.trim() === '')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/^\.[a-z-_]+\s*[^(){}[];=|&]*$/ doesn't allow file names with the special characters^(){}[];=|&. I can live that. Also _ and - are not necessary in it, because the REPL commands don't have them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also _ and - are not necessary in it, because the REPL commands don't have them.

The defaults don't currently, correct. But they could eventually. Also I think the repl can be extended to add more commands? If so, those characters seem like likely candidates to exist in a custom command. While we are at it we should add A-Z to the match too.

Given the above, it's possible this would technically be a breaking change then? I'm not so sure, or concerned either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looks like this would be very tricky to fix and will still break from time to time :'(

var keyword = matches && matches[1];
var rest = matches && matches[2];
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ function error_test() {
'RegExp.$6\nRegExp.$7\nRegExp.$8\nRegExp.$9\n',
expect: ['\'1\'\n', '\'2\'\n', '\'3\'\n', '\'4\'\n', '\'5\'\n', '\'6\'\n',
'\'7\'\n', '\'8\'\n', '\'9\'\n'].join(`${prompt_unix}`) },
// making sure that the function calls on objects will not be treated as
// REPL commands
{ client: client_unix, send: 'function sum(arr) {\nreturn arr\n\t' +
'.reduce(function(a, x) { return a + x })\n}',
expect: prompt_multiline + prompt_multiline + prompt_multiline +
'undefined\n' + prompt_unix },
// ... even if the function calls have whitespaces between the function
// name and the parameters.
{ client: client_unix, send: 'function sum(arr) {\nreturn arr\n\t' +
'.reduce (\tfunction(a, x) { return a + x }' +
')\n}',
expect: prompt_multiline + prompt_multiline + prompt_multiline +
'undefined\n' + prompt_unix },
// but the parsing will misinterpret `(` if it is used somewhere in a valid
// command, and it will NOT be considered as a REPL command.
{ client: client_unix,
send: '.load file_name_with_opening_paren_().js\n.clear',
expect: prompt_multiline + prompt_unix },
]);
}

Expand Down