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

Wrong Conversion of Let and Const Variables Defined in Block #2627

Closed
clhiker opened this issue Nov 2, 2021 · 1 comment · Fixed by #3826
Closed

Wrong Conversion of Let and Const Variables Defined in Block #2627

clhiker opened this issue Nov 2, 2021 · 1 comment · Fixed by #3826
Labels
Milestone

Comments

@clhiker
Copy link

clhiker commented Nov 2, 2021

Describe the bug

Const and let only exist in the blocks they are defined in, but after swc conversion, they all become var variables and can print value out of the block.

Input code

{
  let a = 1;
  const b = 1;
  var c = 1;
}
console.log(a); 
console.log(b); 
console.log(c);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false,
      "dynamicImport": false,
      "privateMethod": false,
      "functionBind": false,
      "exportDefaultFrom": false,
      "exportNamespaceFrom": false,
      "decorators": false,
      "decoratorsBeforeExport": false,
      "topLevelAwait": false,
      "importMeta": false
    },
    "transform": null,
    "target": "es5",
    "loose": false,
    "externalHelpers": false
  }
}

Playground link

https://play.swc.rs/?version=1.2.106&code=H4sIAAAAAAAAA6vm5VJQyEktUUhUsFUwtAbxkvPziksUkuD8ssQihWQor5aXCySdn5Oql5OfrpGoaa2AKpKEIZIMFAEAhYlu3mYAAAA%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

Expected behavior

const and let only exist in the blocks they are defined in. After transforming, they should like this

// const and let only exist in the blocks they are defined in.
{
  var _a = 1;
  var _b = 1;
  var c = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
console.log(c); // Prints 1

or like this
// const and let only exist in the blocks they are defined in.

{
  var a$__0 = 1;
  var b$__1 = 1;
  var c = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
console.log(c); // Prints 1

Version

@swc/cli: 0.1.51 @swc/core: 1.2.106

Additional context

I run swc use cmd as follows

npx swc --config-file=.swcrc poc.js -o poc.js

The environment is ubuntu 20.04 amd64.

@clhiker clhiker added the C-bug label Nov 2, 2021
@kdy1 kdy1 modified the milestones: v1.2.107, v1.2.108 Nov 3, 2021
@kdy1 kdy1 modified the milestones: v1.2.108, v1.2.109, v1.2.110, v1.2.111 Nov 11, 2021
@kdy1 kdy1 modified the milestones: v1.2.111, v1.2.112, v1.2.113 Nov 19, 2021
@kdy1 kdy1 modified the milestones: v1.2.118, v1.2.119, v1.2.120 Dec 5, 2021
@kdy1 kdy1 modified the milestones: v1.2.120, v1.2.121 Dec 13, 2021
@kdy1 kdy1 modified the milestones: v1.2.136, v1.2.137, v1.2.138, v1.2.139 Feb 3, 2022
@kdy1 kdy1 modified the milestones: v1.2.143, v1.2.144, v1.2.145, v1.2.146 Feb 19, 2022
@kdy1 kdy1 modified the milestones: v1.2.146, v1.2.147, v1.2.148 Feb 27, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Mar 3, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Mar 3, 2022
@kdy1 kdy1 closed this as completed in #3826 Mar 3, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 18, 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 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 participants