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

Function parameter's default value incorrectly captures other destructured parameter using spread operator #9198

Open
qzix opened this issue Jul 10, 2024 · 1 comment
Labels
Milestone

Comments

@qzix
Copy link

qzix commented Jul 10, 2024

Describe the bug

Destructured variable is referenced as a default value before being declared

Input code

function foo({ a, ...rest }: {}, foo: unknown = rest): unknown  {

}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.6.7&code=H4sIAAAAAAAAA0srzUsuyczPU0jLz9eoVkjUUdDT0ytKLS5RqLVSqK7VAYlbKZTmZefll%2Bcp2CqApDQRAgrVXFy1ClwA73sdnEYAAAA%3D&config=H4sIAAAAAAAAA1VPSQ7CMAy85xWRzxwACQ78gUdEwa2Csil2JaKqfydpk0Jv9iye8SykhDdpeMi5jGWJKhGmfS8IZc%2FqUxDgHJF0MpHh1FmmSg3KEq7QsjHAKo3I1YV0PV9uzQE2BMLuaJgz3gz5P1MHFxMSHYVVqvxo8ZgoWiq48JpWsv1S%2B24N7vAT9bD9MBh6dienCcXyBbnuEC8XAQAA

SWC Info output

No response

Expected behavior

SWC should output code for parameter default value after the destructuring code

Actual behavior

Targeting ES2015 and below, transpiled code is incorrect as it references undeclared variable, pay attention to foo1 = rest:

function foo(_param, foo1 = rest) {
    var { a } = _param, rest = _object_without_properties(_param, [
        "a"
    ]);
}

For modern ES that supports object spreading output is correct:

function foo({ a, ...rest }, foo1 = rest) {}

Version

1.6.7

Additional context

No response

@qzix qzix added the C-bug label Jul 10, 2024
@kdy1 kdy1 added this to the Planned milestone Jul 11, 2024
@kdy1 kdy1 self-assigned this Jul 11, 2024
@magic-akari
Copy link
Member

magic-akari commented Jul 12, 2024

@kdy1 kdy1 removed their assignment Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants