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

ReferenceError after minification due to mangled variable name not applied to shorthand property #2051

Closed
marvinhagemeister opened this issue Aug 10, 2021 · 1 comment · Fixed by #2052
Assignees
Labels
Milestone

Comments

@marvinhagemeister
Copy link
Contributor

marvinhagemeister commented Aug 10, 2021

Describe the bug

After minifying the code with swc the code contains a reference error. The variables inside the destructured assignmend are correctly renamed, but not all references are updated accordingly. In the snippet below the url variable will be renamed, but the value of the shorthand property in the returned object won't be updated and still points to url.

Input code

export function LocationProvider() {
  const [url] = ["https://example.com"];
  return () => ({ url });
}

Incorrectly generated output:

export function LocationProvider(){const[a]=["https://example.com"];return()=>({url})}

Note, that the property in the returned object isn't updated.

Config

{
  "jsc": {
    "target": "es2021",
    "minify": {
      "mangle": true
    }
  },
  "minify": true
}

Expected behavior
I expect all references of a variable to be renamed. This includes updating references to shorthand properties in objects, whilst keeping the property name intact. The expected output would be:

export function LocationProvider(){const[a]=["https://example.com"];return()=>({url:a})}

Version
The version of @swc/core: 1.2.76

Additional context
Add any other context about the problem here.

@marvinhagemeister marvinhagemeister changed the title Missing variable after minification ReferenceError after minification due to mangled variable name not applied to shorthand property Aug 10, 2021
@kdy1 kdy1 added this to the v1.2.77 milestone Aug 10, 2021
@kdy1 kdy1 self-assigned this Aug 11, 2021
kdy1 added a commit that referenced this issue Aug 11, 2021
swc_ecma_minifier:
 - Test mangler using execution test suite.
 - `mangler`: Preserve `arguments`.
 - `mangler`: Handle shorthand. (#2051)
 - `mangler`: Handle object pattern properties.
 - `precompress`: Don't drop function declarations if the variable with same name is in different scope. (#2011)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 22, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants