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

ref is not defined when destructuring class method argument with certain browser targets #8366

Closed
abrenneke opened this issue Dec 1, 2023 · 1 comment · Fixed by #8371
Closed
Assignees
Labels
Milestone

Comments

@abrenneke
Copy link

abrenneke commented Dec 1, 2023

Describe the bug

When the following conditions are true:

  • You are exporting a class
  • It has a method with a destructured object, that has at least 2 properties
  • The argument has a default value (any)

A ref variable is referenced outside the class, and the variable does not exist at that point in the code execution.

This only happens with certain targets:

  • Chrome >= 46
  • Chrome >= 47
  • Chrome >= 48
  • Chrome >= 49
  • Chrome >= 50

Input code

export class F {
  a({ b, c } = undefined) {}
}

Bad output code

var _ref = ref === void 0 ? undefined : ref;
export class F {
    a(ref) {
        let b = _ref.b, c = _ref.c;
    }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    },
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true,
  "env": {
    "targets": "Chrome >= 50"
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.101-nightly-20231201.1&code=H4sIAAAAAAAAA0utKMgvKlFIzkksLlZwU6jmUlBI1KhWSNJRSFaoVbBVKM1LSU3LzEtN0VSoruWq5QIAmqfhMjAAAAA%3D&config=H4sIAAAAAAAAA2WOvQ7CMAyE9z5F5JmBBQYkWJh5iKi4JSh%2FslNEVPXdSdKmFLFY8Xfny42NEPDkFk5iTM%2B0eEmMtO6JcLRBvhOBED1yS8oH2FU1cJY6qRkLmmYFtHOMVVmYUVZ1cZvdOuMJmX%2BN2Sptr%2FE%2FuQww7j4UdSmdi%2BWCyEf4mupvazIovtXLQMPM0L42QZJ6DLkOXB%2FkDIrLWRz2JbSZPl6W03suAQAA

SWC Info output

No response

Expected behavior

Generates valid code that does not SyntaxError

Actual behavior

Generates code that is invalid to run

Version

1.3.100

Additional context

No response

@abrenneke abrenneke added the C-bug label Dec 1, 2023
@kdy1 kdy1 added this to the Planned milestone Dec 1, 2023
@kdy1 kdy1 self-assigned this Dec 1, 2023
@abrenneke abrenneke changed the title ref is not defined when destructuring class constructor object with certain browser targets ref is not defined when destructuring class method object with certain browser targets Dec 1, 2023
@abrenneke abrenneke changed the title ref is not defined when destructuring class method object with certain browser targets ref is not defined when destructuring class method argument with certain browser targets Dec 1, 2023
@kdy1 kdy1 closed this as completed in #8371 Dec 2, 2023
kdy1 pushed a commit that referenced this issue Dec 2, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.101 Dec 18, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Jan 17, 2024

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 Jan 17, 2024
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