repl: better REPL commands detection#2254
Conversation
|
Right now I am thinking about using a different character instead of |
|
|
|
@Fishrock123 You mean like |
|
Doh, true, |
|
I think starting with EDIT: Oh, and the modulus operator |
|
Oh yeah. That is also correct. |
|
I don't think changing characters is necessarily the best idea. We've already committed to |
|
Hmmm, true. So, breakage is unavoidable, no matter what we actually choose as the REPL command identifier. |
|
@cjihrig We cannot just ignore REPL commands in an expression, especially .break that is here to get out of an unfinished expression AFAIK |
|
I think you can, you just need the correct context, much like you must be able to determine the context of the |
|
If course, but don't we need to introduce some kind of JS parsing to do that ? |
|
I don't think we need a full blown parser, grammar and all. I think we just need to maintain a bit more state (like the existing |
|
I can take that up, but I am not sure if it is really necessary. As of now, I am just happy with this RegEx (though I have a feeling that this might break if the filename has |
Sounds like a test case! |
|
@Fishrock123 Oh yeah :-) I'll include that, but is it okay to land this fix with that known limitation? |
Fixes the problem shown in nodejs#2246 The REPL module, treats any line which starts with a dot character as a REPL command and this limits the ability to use function calls in the next lines to improve readability. This patch checks if the current line starts with `.` and then a series of lowercase letters and then any characters. For example: > process.version 'v2.4.0' > function sum(arr) { ... return arr ... .reduce(function(c, x) { return x + c; }); Invalid REPL keyword undefined but then, this patches allows this: > process.version 'v2.4.1-pre' > function sum(arr) { ... return arr ... .reduce(function(c, x) { return x + c; }); ... } undefined
553fd51 to
893ab37
Compare
|
Rebased and incorporated the test case suggested by @Fishrock123 :-) CI Run: node-test-pull-request/70 |
|
And the CI is green (failing cases are not related to this change) :-) |
|
Bump! |
|
@Fishrock123 Ping! |
There was a problem hiding this comment.
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() === '')
There was a problem hiding this comment.
/^\.[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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Hmmm, looks like this would be very tricky to fix and will still break from time to time :'(
Fixes the problem shown in #2246
The REPL module, treats any line which starts with a dot character as
a REPL command and this limits the ability to use function calls in
the next lines to improve readability.
This patch checks if the current line starts with
.and then a seriesof lowercase letters and then any characters.
For example:
but then, this patches allows this: