Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect scope of declarations surrounding using declarations #8020

Closed
tjjfvi opened this issue Sep 27, 2023 · 1 comment · Fixed by #8044
Closed

incorrect scope of declarations surrounding using declarations #8020

tjjfvi opened this issue Sep 27, 2023 · 1 comment · Fixed by #8044
Assignees
Labels
Milestone

Comments

@tjjfvi
Copy link

tjjfvi commented Sep 27, 2023

Describe the bug

SWC incorrectly handles the scope of declarations surrounding using declarations.

Input code

using foo = null

const bar = 1

console.log(baz())

function baz() {
  return bar
}

Config

{
  "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.

@swc-bot
Copy link
Collaborator

swc-bot commented Nov 4, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants