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(es/utils): Fix regression of RefRewriter #8153

Merged
merged 10 commits into from
Oct 20, 2023
Merged

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Oct 19, 2023

@kdy1 kdy1 added this to the Planned milestone Oct 19, 2023
@kdy1 kdy1 self-assigned this Oct 19, 2023
@kdy1
Copy link
Member Author

kdy1 commented Oct 19, 2023

cc @magic-akari Can you take a look?

Copy link
Member Author

@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.

swc-bump:

  • swc_ecma_utils

@kdy1
Copy link
Member Author

kdy1 commented Oct 19, 2023

cc @magic-akari Is this the correct fix?

@kdy1 kdy1 marked this pull request as ready for review October 19, 2023 23:10
@kdy1 kdy1 changed the title fix(es/modules): Fix regression of commonjs pass fix(es/utils): Fix regression of RefRewriter Oct 19, 2023
@magic-akari
Copy link
Member

magic-akari commented Oct 20, 2023

fn visit_mut_pat(&mut self, n: &mut Pat) {
match n {
Pat::Ident(BindingIdent { id, .. }) => {
if let Some(expr) = self.query.query_lhs(id) {
*n = Pat::Expr(Box::new(expr));
}
}
_ => n.visit_mut_children_with(self),
}
}

The key issue here is that we hope that when we access Pat::Ident, we get the Ident ref, not the ident decl

var a = "value-a", b = "value-b"; // var declaration
//  ^^^ decl       ^^^ decl

var { [a]: d, c } = { "value-a": 1, b }; // var declaration
//     |   |  ^^^ decl              ^^^ ref
//     |   |- decl
//     |
//     |-- ref

var [e = a] =     [a];       // var declaration
//   |   ^^^ ref   ^^^ ref
//   |- decl

  [b] = [1], { [a]:    d } = { "value-a": 1 }; // assignment expression
// ^^^ ref      ^^ref  ^^ref

I think that the Pat in a declaration name and the Pat in an assignment expression are different.
In an assignment expression, Pat::Ident can be safely replaced with Pat::Expr,
but you can't do that in a declaration name.

Maybe we should wrap all ident ref cases in an IdentRef struct #8074.

@kdy1 kdy1 enabled auto-merge (squash) October 20, 2023 05:28
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit 08cf1f7 into swc-project:main Oct 20, 2023
143 checks passed
@kdy1 kdy1 deleted the es-reg branch October 20, 2023 06:07
@kdy1 kdy1 modified the milestones: Planned, v1.3.94 Oct 21, 2023
sokra pushed a commit to vercel/turbo that referenced this pull request Oct 23, 2023
### Description

Update SWC crates. This PR fixes a regression of `swc_core`.
The important PR: swc-project/swc#8153

### Testing Instructions

See vercel/next.js#57121

Closes WEB-1812
sokra added a commit to vercel/next.js that referenced this pull request Oct 23, 2023
### What?

Update SWC crates. This PR fixes a regression of `swc_core`.
The important PR: swc-project/swc#8153

### Why?

There was a regression in `swc_core`.

### How?

-  Fixes #56408

Closes WEB-1811

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 20, 2023
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.

None yet

3 participants