Repository: swc-project/swc (rust)

Issue:
Rest parameters undefined
### Describe the bug

When using rest parameters in a function declaration, the parameter is undefined.

### Input code

```typescript
function foo(...args) {
  console.log(args)
}

foo(1,2,3)
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true,
  "env": {
    "targets": "Edge >= 17"
  }
}
```


### Playground link

https://play.swc.rs/?version=1.2.129&code=H4sIAAAAAAAAA0srzUsuyczPU0jLz9fQ09NLLEov1lSo5lJQSM7PK87PSdXLyU%2FXAIty1XJxgVQZ6hjpGGsCAMQsaGQ5AAAA&config=H4sIAAAAAAAAA0WOTQrDIBCFrxLe2k0XbSHQ7rrsIcRMgiVRcUxpEO%2BeUVKyG97PNy%2FjwwZ9RtCRKdaLN5f0Dz3SFohNtCFBIbFIKa5U5NZxoiQJ4qtYs%2FdM6Ec9Myks1tlxqyDjlxCJ%2BbS0m%2BZ%2Fsgho8cNahdx%2BNeAN5WQcPcvvI1gHKJD7tk6bIXi8hom656O73FHKDhLiHCLTAAAA

### Expected behavior

`args` should be defined.

### Actual behavior

`args` is undefined, because it is declared with block-scope in a `for` loop (i.e. `let` instead of `var`):

```js
function foo() {
    for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
        args[_key] = arguments[_key];
    }
    console.log(args);
}
foo(1, 2, 3);
```

Changing the `let` to `var` fixes the issue. Alternatively, `args` and `_len` could be defined outside the loop:

```js
function foo() {
    let _len = arguments.length, args = new Array(_len)
    for(let _key = 0; _key < _len; _key++){
        args[_key] = arguments[_key];
    }
    console.log(args);
}
foo(1, 2, 3);
```

### Version

1.2.124

### Additional context

_No response_

Pull request description:
use var for _len and arg in rest parameters
**Description:**
```js
function foo(...args) {
  console.log(args)
}
function foo() {
    // use `let` will break code if block scoping is not enable
    for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
        args[_key] = arguments[_key];
    }
    console.log(args);
}
```

**Related issue (if exists):**
fix #3256

Patch:
diff --git a/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs b/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs
index c28afecf2b..8b1834949c 100644
--- a/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs
@@ -278,7 +278,7 @@ impl Params {
                     unpack_rest = Some(Stmt::For(ForStmt {
                         span,
                         init: Some(VarDeclOrExpr::VarDecl(VarDecl {
-                            kind: VarDeclKind::Let,
+                            kind: VarDeclKind::Var,
                             span,
                             decls: vec![
                                 // _len = arguments.length - i


Test patch:
diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs
index 9dffb91263..74f7335a75 100644
--- a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs
+++ b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs
@@ -1628,7 +1628,7 @@ function foo(...a) {
 const a = 'bar';
 
 function foo() {
-  for (let _len = arguments.length, a = new Array(_len), _key = 0; _key < _len; _key++) {
+  for (var _len = arguments.length, a = new Array(_len), _key = 0; _key < _len; _key++) {
     a[_key] = arguments[_key];
   }
 
@@ -1692,7 +1692,7 @@ test!(
     ",
     "
     const arrow = function() {
-        for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
+        for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
             args[_key] = arguments[_key];
         }
         console.log(args);
@@ -1711,7 +1711,7 @@ test!(
     ",
     "
     const arrow = ()=>function() {
-        for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
+        for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
             args[_key] = arguments[_key];
         }
         console.log(args);
@@ -1732,7 +1732,7 @@ test!(
     const arrow = ()=>{
         var _this = this;
         return function() {
-            for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; \
+            for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; \
      _key++){
                 args[_key] = arguments[_key];
             }
@@ -1755,7 +1755,7 @@ test!(
     const arrow = ()=>{
       var _this = this;
       return this, function() {
-          for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
+          for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
               args[_key] = arguments[_key];
           }
           console.log(_this, args);
@@ -1776,14 +1776,14 @@ test!(
     "
     var _this = this;
     const arrow = function() {
-        for(let _len1 = arguments.length, args = new Array(_len1), _key1 = 0; _key1 < _len1; \
+        for(var _len1 = arguments.length, args = new Array(_len1), _key1 = 0; _key1 < _len1; \
      _key1++){
             args[_key1] = arguments[_key1];
         }
         return _this, ()=>{
             var _this1 = _this;
             return function() {
-                for(let _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; \
+                for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; \
      _key++){
                     args[_key] = arguments[_key];
                 }
diff --git a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs
index a5731cf18d..8b70045c2a 100644
--- a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs
+++ b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs
@@ -221,7 +221,7 @@ async function s(x, ...args) {
 }
 function _s() {
     _s = _asyncToGenerator(function*(x) {
-        for(let _len1 = arguments.length, args = new Array(_len1 > 1 ? _len1 - 1 : 0), _key1 = 1; _key1 < _len1; _key1++){
+        for(var _len1 = arguments.length, args = new Array(_len1 > 1 ? _len1 - 1 : 0), _key1 = 1; _key1 < _len1; _key1++){
             args[_key1 - 1] = arguments[_key1];
         }
         var _this = this, _arguments = arguments;
@@ -230,7 +230,7 @@ function _s() {
                 var _this1 = _this, _arguments1 = _arguments;
                 let r = function() {
                     var _r = _asyncToGenerator(function*(z, b) {
-                        for(let _len = arguments.length, innerArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){
+                        for(var _len = arguments.length, innerArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){
                             innerArgs[_key - 2] = arguments[_key];
                         }
                         yield z;