Repository: swc-project/swc (rust)

Issue:
incorrect scope of declarations surrounding `using` declarations
### Describe the bug

SWC incorrectly handles the scope of declarations surrounding `using` declarations.

### Input code

```typescript
using foo = null

const bar = 1

console.log(baz())

function baz() {
  return bar
}
```


### Config

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


### Playground link

https://play.swc.rs/?version=1.3.90&code=H4sIAAAAAAAAAyXIPQrAIAwG0D2n%2BEZdCj1AD6OiUggJRLO09O79G9%2FzsUtHU8UGcWaiojImcrJn1p%2FKdWHtIacjxEjUXMrcVfAFTgKsTrc3jK4b1lYZUlQAAAA%3D&config=H4sIAAAAAAAAA22PPQ7CMAyFd04ReWZAGRi4A4eIglsF5U%2B2K1FVvTtJ2hSQ2Oz3PdvPy0kpeLKFm1pKWZpsiJGOvig8RzGvooDMGdmSywLnToUrGoxnbNK6EfApMXaya8FFN8zfu20KmZD511itJo4e%2F24WQyNKzYOsL1q3LA1CSI%2BpTe3P1MCb8QofU09xXATH9z4pNBVtfQNyL9zVGQEAAA%3D%3D

### SWC Info output

(N/A; reproduced using Deno / SWC playground)

### Expected behavior

The transpiled program prints `1`.

### Actual behavior

The `bar` constant is defined inside the `try` block, rendering it inaccessible to the function `baz`, which is defined outside of the `try` block. This results in a `ReferenceError` at runtime.

### Version

1.3.90

### Additional context

When `target` is `ES5`, this actually works correctly, due to the `const` being transformed to a `var`.

As a point of comparison, TSC transpiles this with a `var bar;` statement outside of the `try`, even when `target` is set above `ES5`.

Pull request:
fix(es/compat): Fix scoping of `explicit-resource-management`
**Description:**


**Related issue:**

 - Closes #8020

Patch:
diff --git a/.github/workflows/publish-extra-bindings.yml b/.github/workflows/publish-extra-bindings.yml
index 1e3986ecb0..70b8eaeb32 100644
--- a/.github/workflows/publish-extra-bindings.yml
+++ b/.github/workflows/publish-extra-bindings.yml
@@ -23,9 +23,6 @@ on:
           - minifier
           - html
           - xml
-      version:
-        description: Must be a valid semver version, and should not start with `v`.
-        required: true
 
 jobs:
   build-node:
@@ -104,7 +101,7 @@ jobs:
               export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256
               export CARGO_PROFILE_RELEASE_LTO=false
               (cd packages/${{ github.event.inputs.package }} && corepack yarn build --target aarch64-pc-windows-msvc)
-    name: "Build: ${{ matrix.package }} - ${{ matrix.settings.target }}"
+    name: "Build: ${{ github.event.inputs.package }} - ${{ matrix.settings.target }}"
     runs-on: ${{ matrix.settings.host }}
     steps:
       - uses: actions/checkout@v3
@@ -385,9 +382,6 @@ jobs:
         with:
           node-version: 18
 
-      - name: Set release name
-        run: echo "RELEASE_VERSION=${{ github.event.inputs.package }}@v${{ github.event.inputs.version }}" >> $GITHUB_ENV
-
       - name: Install dependencies
         working-directory: "./packages/${{ github.event.inputs.package }}"
         run: corepack yarn
diff --git a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs
index e860053898..5f81501f0d 100644
--- a/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs
+++ b/crates/swc_ecma_transforms_proposal/src/explicit_resource_management.rs
@@ -87,6 +87,10 @@ impl ExplicitResourceManagement {
                 Ok(stmt @ Stmt::Decl(Decl::Fn(..))) => {
                     new.push(T::from_stmt(stmt));
                 }
+                Ok(Stmt::Decl(Decl::Var(mut var))) => {
+                    var.kind = VarDeclKind::Var;
+                    try_body.push(Stmt::Decl(Decl::Var(var)));
+                }
                 Ok(stmt) => try_body.push(stmt),
                 Err(stmt) => match stmt.try_into_module_decl() {
                     Ok(ModuleDecl::ExportDefaultDecl(decl)) => {
diff --git a/packages/minifier/package.json b/packages/minifier/package.json
index 84f50f388f..9d2f68b51e 100644
--- a/packages/minifier/package.json
+++ b/packages/minifier/package.json
@@ -1,6 +1,6 @@
 {
     "name": "@swc/minifier",
-    "version": "1.3.90",
+    "version": "1.3.91",
     "description": "Super-fast alternative for terser",
     "homepage": "https://swc.rs",
     "main": "./index.js",


Test patch:
diff --git a/crates/swc/tests/fixture/issues-8xxx/8020/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8020/input/.swcrc
new file mode 100644
index 0000000000..7395508a93
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8020/input/.swcrc
@@ -0,0 +1,19 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "typescript",
+            "tsx": false
+        },
+        "loose": false,
+        "minify": {
+            "compress": false,
+            "mangle": false
+        },
+        "target": "es2022",
+    },
+    "module": {
+        "type": "es6"
+    },
+    "minify": false,
+    "isModule": true,
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-8xxx/8020/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8020/input/1.js
new file mode 100644
index 0000000000..a100d25cea
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8020/input/1.js
@@ -0,0 +1,9 @@
+using foo = null
+
+const bar = 1
+
+console.log(baz())
+
+function baz() {
+    return bar
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js
new file mode 100644
index 0000000000..acc0c8214e
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8020/output/1.js
@@ -0,0 +1,16 @@
+import { _ as _dispose } from "@swc/helpers/_/_dispose";
+import { _ as _using } from "@swc/helpers/_/_using";
+function baz() {
+    return bar;
+}
+try {
+    var _stack = [];
+    var foo = _using(_stack, null);
+    var bar = 1;
+    console.log(baz());
+} catch (_) {
+    var _error = _;
+    var _hasError = true;
+} finally{
+    _dispose(_stack, _error, _hasError);
+}
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/mixed/output.mjs b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/mixed/output.mjs
index cdfaa6e1cc..6f2b7f9374 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/mixed/output.mjs
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/mixed/output.mjs
@@ -1,9 +1,9 @@
 {
     try {
         var _stack = [];
-        const a = _using(_stack, 1);
-        const b = _using(_stack, 2, true);
-        const c = _using(_stack, 3);
+        var a = _using(_stack, 1);
+        var b = _using(_stack, 2, true);
+        var c = _using(_stack, 3);
     } catch (_) {
         var _error = _;
         var _hasError = true;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/only-using-await/output.mjs b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/only-using-await/output.mjs
index 9324159310..5d4390b9ff 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/only-using-await/output.mjs
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-await/only-using-await/output.mjs
@@ -1,9 +1,9 @@
 {
     try {
         var _stack = [];
-        const x = _using(_stack, obj, true);
+        var x = _using(_stack, obj, true);
         stmt;
-        const y = _using(_stack, obj, true), z = _using(_stack, obj, true);
+        var y = _using(_stack, obj, true), z = _using(_stack, obj, true);
         doSomethingWith(x, y);
     } catch (_) {
         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/bare-block/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/bare-block/output.js
index c145abe089..6982b489b6 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/bare-block/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/bare-block/output.js
@@ -1,7 +1,7 @@
 {
     try {
         var _stack = [];
-        const x = _using(_stack, obj);
+        var x = _using(_stack, obj);
         doSomethingWith(x);
     } catch (_) {
         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/function-body/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/function-body/output.js
index 06519087aa..4330fb187b 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/function-body/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/function-body/output.js
@@ -1,7 +1,7 @@
 function fn() {
     try {
         var _stack = [];
-        const x = _using(_stack, obj);
+        var x = _using(_stack, obj);
         return doSomethingWith(x);
     } catch (_) {
         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/if-body/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/if-body/output.js
index d419059d66..41666e989f 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/if-body/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/if-body/output.js
@@ -1,7 +1,7 @@
 if (test) {
     try {
         var _stack = [];
-        const x = _using(_stack, obj);
+        var x = _using(_stack, obj);
         doSomethingWith(x);
     } catch (_) {
         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-nested/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-nested/output.js
index 9dece6c4f6..84cc09b386 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-nested/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-nested/output.js
@@ -1,14 +1,14 @@
 {
     try {
         var _stack = [];
-        const x = _using(_stack, obj);
+        var x = _using(_stack, obj);
         {
             try {
                 var _stack1 = [];
-                const y = _using(_stack1, call(()=>{
+                var y = _using(_stack1, call(()=>{
                     try {
                         var _stack = [];
-                        const z = _using(_stack, obj);
+                        var z = _using(_stack, obj);
                         return z;
                     } catch (_) {
                         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-same-level/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-same-level/output.js
index a82d21da8b..8ac8945904 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-same-level/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/multiple-same-level/output.js
@@ -2,11 +2,11 @@
     try {
         var _stack = [];
         stmt;
-        const x = _using(_stack, obj);
+        var x = _using(_stack, obj);
         stmt;
-        const y = _using(_stack, obj), z = _using(_stack, obj);
+        var y = _using(_stack, obj), z = _using(_stack, obj);
         stmt;
-        const w = _using(_stack, obj);
+        var w = _using(_stack, obj);
         doSomethingWith(x, z);
     } catch (_) {
         var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/static-block/output.js b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/static-block/output.js
index 87287b0f55..cec9c08fbe 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/static-block/output.js
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-sync/static-block/output.js
@@ -2,7 +2,7 @@ class A {
     static{
         try {
             var _stack = [];
-            const x = _using(_stack, y);
+            var x = _using(_stack, y);
             doSomethingWith(x);
         } catch (_) {
             var _error = _;
diff --git a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs
index 28a2a262dc..d37423ec05 100644
--- a/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs
+++ b/crates/swc_ecma_transforms_proposal/tests/explicit-resource-management/transform-top-level/hoisting/output.mjs
@@ -19,7 +19,7 @@ export class B {
 try {
     var _stack = [];
     doSomething();
-    let c = 2;
+    var c = 2;
     class A {
     }
     var x = _using(_stack, null);