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

Block-scoping bug #3235

Closed
seansonb opened this issue Jan 11, 2022 · 2 comments · Fixed by #3826
Closed

Block-scoping bug #3235

seansonb opened this issue Jan 11, 2022 · 2 comments · Fixed by #3826
Milestone

Comments

@seansonb
Copy link

seansonb commented Jan 11, 2022

I have the following script:

let res = [];

let a = 2;
res.push(a === 2);

{
let b = 1;
}
res.push(typeof b === "undefined");

if(true) {
let b = 0;
}
res.push(typeof b === "undefined");

for(let b = 0; b < 10; b++) {
}
res.push(typeof b === "undefined");

function test() {
let b = 7;
}
res.push(typeof b === "undefined");

console.log(res);

I expect this to evaluate to [true, true, true, true, true], but am getting [true, false, false, false, false] because the first block-scoped variable b is not renamed or prepended with an underscore. Here's the link to the swc-playground transpilation. In babel the transpilation looks like the following with all block-scoped let assignments prepended with underscores and incremented:

var res = [];
var a = 2;
res.push(a === 2);
{
var _b = 1;
}
res.push(typeof b === "undefined");

if (true) {
var _b2 = 0;
}

res.push(typeof b === "undefined");

for (var _b3 = 0; _b3 < 10; _b3++) {}

res.push(typeof b === "undefined");

function test() {
var b = 7;
}

res.push(typeof b === "undefined");
res;

Config:

{
"jsc": {
"target": "es5",
"parser": {
"syntax": "ecmascript"
}
}
"module": {
"type": "commonjs",
"strict": true
"strictMode": true
},
"sourceMaps": true
}

@seansonb seansonb changed the title Block-scoping bug? Block-scoping bug Jan 11, 2022
@kdy1 kdy1 added this to the v1.2.129 milestone Jan 11, 2022
@kdy1 kdy1 modified the milestones: v1.2.129, v1.2.130, v1.2.131, v1.2.132 Jan 13, 2022
@kdy1 kdy1 modified the milestones: v1.2.132, v1.2.133, v1.2.134, v1.2.135 Jan 20, 2022
@kdy1 kdy1 modified the milestones: v1.2.135, v1.2.136, v1.2.137 Jan 27, 2022
@kdy1 kdy1 modified the milestones: v1.2.137, v1.2.138, v1.2.139, v1.2.140 Feb 7, 2022
@kdy1 kdy1 modified the milestones: v1.2.140, v1.2.141, v1.2.142 Feb 15, 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
None yet
4 participants