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

When let => var, a scope error can cause the variable to be incorrectly overwritten #2998

Closed
meathill opened this issue Dec 9, 2021 · 1 comment · Fixed by #3058
Closed
Labels
Milestone

Comments

@meathill
Copy link

meathill commented Dec 9, 2021

Describe the bug

var in block statement will overwrite the variable which has a same name,
defined in identifier label. Like the sample code below. But let won't.
So when convert code like that, please change the variable name to make them
separated.

Input code

let a = 10;
for (let b = 0; b < a; b++) {
    let c = 0, b = 10, d = 100;
    console.log(b);
}

Config

{
  "jsc": {
    "loose": false,
    "externalHelpers": true,
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link

https://play.swc.rs/?version=1.2.118&code=H4sIAAAAAAAAA8tJLVFIVLBVMDSw5uVKyy9S0MgBiiQBRQysgZSNQiKQ0tbWVKjm5VIAApBsMkhWB6zIEEingGmQfpCC5Py84vycVL2c%2FHSNJE2gYC0vFwApdniYZQAAAA%3D%3D&config=H4sIAAAAAAAAA0WNOw4CMQxE7%2BI6LRQ5AQ2HsIJ3FZSfbK%2BUKNq7k0CAdmbemw5PcWA7hJyFwG4YhAxQVeKE4UahEAtY5WPEBVmI51xaUqxggVxEceyLghmuuhSnAUXeSedELqOLPvmtTdblWJhEfm8R0x6%2B5%2BdAY34cM%2BigrdBbcYXz71icl%2FsafsgXAm4Ch80AAAA%3D

Expected behavior

var a = 10;
for(var b = 0; b < a; b++){
    var c = 0, b1 = 10, d = 100;
    console.log(b1);
}

Version

1.2.118

Additional context

No response

@meathill meathill added the C-bug label Dec 9, 2021
@kdy1 kdy1 modified the milestones: v1.2.119, v1.2.120, v1.2.121 Dec 10, 2021
kwonoj added a commit to kwonoj/swc that referenced this issue Dec 17, 2021
kdy1 pushed a commit that referenced this issue Dec 17, 2021
swc_ecma_transforms_compat:
 - `block_scoping`: Consider the location of variable declarations. (Closes #2998)
nicholasxjy pushed a commit to nicholasxjy/swc that referenced this issue Dec 18, 2021
swc_ecma_transforms_compat:
 - `block_scoping`: Consider the location of variable declarations. (Closes swc-project#2998)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 20, 2022

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 Oct 20, 2022
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