swc icon indicating copy to clipboard operation
swc copied to clipboard

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

Open qzix opened this issue 1 year ago • 1 comments

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 avatar Jul 10 '24 14:07 qzix

related issue:

  • #8527

magic-akari avatar Jul 12 '24 03:07 magic-akari