-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
repl: fix wrong parentheses #46987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repl: fix wrong parentheses #46987
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely a good thought, it may have false negatives/positives though: strings, regexp and comments could contain these brackets (probably a couple more). To solve this, we would have to use acorn to check for the right tokens after parsing the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's always going to be false negatives and false positives whatever checks we make (e.g. currently using { openingBracket: '{' }
as input interprets it as an object pattern, with this change it's going to interpret it as a code block), I don't know if it's worth the effort.
lib/internal/repl/utils.js
Outdated
function isValidParentheses(input) { | ||
const stack = []; | ||
const pairs = { | ||
'(': ')', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'(': ')', | |
__proto__: null, | |
'(': ')', |
lib/internal/repl/utils.js
Outdated
const char = input[i]; | ||
|
||
if (pairs[char]) { | ||
stack.push(char); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stack.push(char); | |
ArrayPrototypePush(stack, char); |
lib/internal/repl/utils.js
Outdated
if (pairs[char]) { | ||
stack.push(char); | ||
} else if (char === ')' || char === ']' || char === '}') { | ||
if (pairs[stack.pop()] !== char) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (pairs[stack.pop()] !== char) { | |
if (pairs[ArrayPrototypePop(stack)] !== char) { |
lib/internal/repl/utils.js
Outdated
for (let i = 0; i < input.length; i++) { | ||
const char = input[i]; | ||
|
||
if (pairs[char]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (pairs[char]) { | |
if (char in pairs) { |
Thank you for your suggestions. Let me try to use acorn. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for {'{':0}
and {[Symbol.for("{")]: 0 }
please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test for {},{}
and for {} //;
? If we end up using acorn anyway, I don’t think checking if the string ends with a semi is still very relevant.
And do we have a test for {throw 0}
already?
I think we can't do it because this case will fail: node/test/parallel/test-repl.js Line 470 in c733cc0
|
That doesn’t look like something we can’t workaround tbh, but it’s not a big deal. |
'{ [\x1B[32mSymbol({)\x1B[39m]: \x1B[33m0\x1B[39m }', | ||
], | ||
}, { | ||
input: '{},{}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I suggested the wrong test. We would want the following to report a syntax error:
input: '{},{}', | |
input: '{}),({}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 078088d
test/parallel/test-repl-preview.js
Outdated
'{}', | ||
], | ||
}, { | ||
input: '{} //;', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we try without the semi? After some more thoughts, I think that’s an even more interesting test case.
input: '{} //;', | |
input: '{} //', |
@jenthone do you mind to recover this change? I just found this PR and I believe it would actually be a great contribution! |
I don't work much on the nodejs anymore and also deleted my fork |
If the input isn't a valid syntax, don't wrap it
Fix #46877