Skip to content

Conversation

jenthone
Copy link
Contributor

@jenthone jenthone commented Mar 7, 2023

If the input isn't a valid syntax, don't wrap it

Fix #46877

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem. labels Mar 7, 2023
@jenthone jenthone changed the title Fix wrong parentheses repl: fix wrong parentheses Mar 7, 2023
@jenthone jenthone marked this pull request as ready for review March 7, 2023 05:48
Copy link
Member

@BridgeAR BridgeAR left a 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.

Copy link
Contributor

@aduh95 aduh95 left a 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.

function isValidParentheses(input) {
const stack = [];
const pairs = {
'(': ')',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'(': ')',
__proto__: null,
'(': ')',

const char = input[i];

if (pairs[char]) {
stack.push(char);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
stack.push(char);
ArrayPrototypePush(stack, char);

if (pairs[char]) {
stack.push(char);
} else if (char === ')' || char === ']' || char === '}') {
if (pairs[stack.pop()] !== char) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (pairs[stack.pop()] !== char) {
if (pairs[ArrayPrototypePop(stack)] !== char) {

for (let i = 0; i < input.length; i++) {
const char = input[i];

if (pairs[char]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (pairs[char]) {
if (char in pairs) {

@jenthone
Copy link
Contributor Author

jenthone commented Mar 8, 2023

Thank you for your suggestions. Let me try to use acorn.

@jenthone jenthone requested review from BridgeAR and aduh95 and removed request for BridgeAR March 8, 2023 13:56
Copy link
Contributor

@aduh95 aduh95 left a 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?

Copy link
Contributor

@aduh95 aduh95 left a 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?

@jenthone
Copy link
Contributor Author

jenthone commented Mar 9, 2023

I don’t think checking if the string ends with a semi is still very relevant.

I think we can't do it because this case will fail:

send: '{ a: 1 }.a;', // { a: 1 }.a;

@aduh95
Copy link
Contributor

aduh95 commented Mar 9, 2023

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: '{},{}',
Copy link
Contributor

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:

Suggested change
input: '{},{}',
input: '{}),({}',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 078088d

'{}',
],
}, {
input: '{} //;',
Copy link
Contributor

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.

Suggested change
input: '{} //;',
input: '{} //',

@jenthone jenthone requested a review from aduh95 March 14, 2023 02:56
@jenthone jenthone closed this by deleting the head repository Jan 9, 2024
@BridgeAR
Copy link
Member

BridgeAR commented Apr 3, 2025

@jenthone do you mind to recover this change? I just found this PR and I believe it would actually be a great contribution!
I am very sorry that this slipped through. It is sometimes hard to review all PRs properly.

@jenthone
Copy link
Contributor Author

jenthone commented Apr 23, 2025

@jenthone do you mind to recover this change? I just found this PR and I believe it would actually be a great contribution! I am very sorry that this slipped through. It is sometimes hard to review all PRs properly.

I don't work much on the nodejs anymore and also deleted my fork
It seems this PR can't be restored. I think someone can pick my commits and create a new PRs ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong error report message for expression {})

4 participants