Repository: swc-project/swc (rust)

Issue:
[TypeScript] Invalid code generated of `var` declaration which shares the name of exported binding
### Describe the bug

The `Pat` in declaration name should be skipped in `RefRewriter`.

### Input code

```typescript
namespace Foo {
    export var a = 1;
    for (var a; a < 5; a++) {}
}
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2022",
  },
  "module": {
    "type": "commonjs"
  },
  "isModule": true
}
```


### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.92&code=H4sIAAAAAAAAA8tLzE0tLkhMTlVwy89XqOZSAILUioL8ohKFssQihUQFWwVDa7BoWn6RggZYzBoobKNgCqS0tTUVqmu5arkAbZ%2FyMEcAAAA%3D&config=H4sIAAAAAAAAA0WMSwqAMAxE9z1FydqFdOkdPESpQRT7IYmgiHe3rVh3mTcvcymtYWUHg77ymUOyxEgtZ8JnEHtkAnImZEdLEqjl3b0%2FYmlGKQay6Y2BwmsJPk77hv98mSiii97HsDI0c%2BHxc4V2VPcDPflySJwAAAA%3D

### SWC Info output

_No response_

### Expected behavior

```JavaScript 
"use strict";
var Foo;
(function(Foo) {
    Foo.a = 1;
    for(var a; Foo.a < 5; Foo.a++){}
})(Foo || (Foo = {}));
```

### Actual behavior

```JavaScript 
"use strict";
var Foo;
(function(Foo) {
    Foo.a = 1;
    for(var Foo.a; Foo.a < 5; Foo.a++){}
})(Foo || (Foo = {}));
```

### Version

1.3.92

### Additional context

_No response_

Pull request description:
fix(es/utils): Skip var declarator name in `RefRewriter`
**Related issue:**
 - Closes #8124 

Patch:
diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs
index a63155ac78..d11260ccc0 100644
--- a/crates/swc_ecma_utils/src/lib.rs
+++ b/crates/swc_ecma_utils/src/lib.rs
@@ -3062,6 +3062,11 @@ where
         }
     }
 
+    fn visit_mut_var_declarator(&mut self, n: &mut VarDeclarator) {
+        // skip var declarator name
+        n.init.visit_mut_with(self);
+    }
+
     fn visit_mut_pat(&mut self, n: &mut Pat) {
         match n {
             Pat::Ident(BindingIdent { id, .. }) => {


Test patch:
diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/input.ts
new file mode 100644
index 0000000000..65c9594d6d
--- /dev/null
+++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/input.ts
@@ -0,0 +1,9 @@
+namespace Foo {
+    export var a = 1;
+    for (var a; a < 5; a++) {}
+}
+
+namespace Bar {
+    export var b = 2;
+    var b = 3;
+}
diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/output.js
new file mode 100644
index 0000000000..17dbfedca3
--- /dev/null
+++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8124/output.js
@@ -0,0 +1,10 @@
+var Foo;
+(function(Foo) {
+    Foo.a = 1;
+    for(var a; Foo.a < 5; Foo.a++){}
+})(Foo || (Foo = {}));
+var Bar;
+(function(Bar) {
+    Bar.b = 2;
+    var b = 3;
+})(Bar || (Bar = {}));