Skip to content
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

Fix parenthesization of sequence expressions in callee and function params #1566

Merged
merged 5 commits into from
Apr 12, 2021

Conversation

devongovett
Copy link
Contributor

This fixes an issue where parentheses would be removed around sequence expressions when they shouldn't, specifically in the case of a call expression callee, and a default value for a function param.

For example:

let x = ({}, () => 2)();

was compiling to:

let x = {
}, ()=>2
();

which is a syntax error.

A similar issue was seen with default assignment patterns in parameters. The fix was to ensure the correct context is set when visiting a sequence expression so that parentheses are added when needed. I checked a bunch of other places where commas are allowed and these were the ones I found, but there could be more.

@@ -389,6 +396,13 @@ impl VisitMut for Fixer<'_> {
}
}

fn visit_mut_stmt(&mut self, s: &mut Stmt) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes a broken test once I added Context::Callee to the list of contexts to wrap with parens around a sequence expression. I think a previous statement was setting the context and it wasn't being reset when moving on to the next statement.

Copy link
Member

@kdy1 kdy1 Apr 11, 2021

Choose a reason for hiding this comment

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

Seems like a reasonable fix to me.

@kdy1 kdy1 modified the milestones: v1.2.52, v1.2.53 Apr 11, 2021
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Great! Thanks!

@@ -389,6 +396,13 @@ impl VisitMut for Fixer<'_> {
}
}

fn visit_mut_stmt(&mut self, s: &mut Stmt) {
Copy link
Member

@kdy1 kdy1 Apr 11, 2021

Choose a reason for hiding this comment

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

Seems like a reasonable fix to me.

ecmascript/transforms/base/src/fixer.rs Show resolved Hide resolved
@kdy1 kdy1 enabled auto-merge (squash) April 12, 2021 05:21
@kdy1 kdy1 disabled auto-merge April 12, 2021 05:21
@kdy1 kdy1 enabled auto-merge (squash) April 12, 2021 05:22
@kdy1 kdy1 merged commit 14edb69 into swc-project:master Apr 12, 2021
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants