swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.89k stars 1.2k forks source link

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

Open qzix opened 1 month ago

qzix commented 1 month ago

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

magic-akari commented 1 month ago

related issue: