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 of const is broken when transpiling to ES5 #3006

Closed
damiantew opened this issue Dec 10, 2021 · 3 comments · Fixed by #4029
Closed

Block scoping of const is broken when transpiling to ES5 #3006

damiantew opened this issue Dec 10, 2021 · 3 comments · Fixed by #4029
Assignees
Labels
Milestone

Comments

@damiantew
Copy link

Describe the bug

Attached code incorrectly transpiles in SWC target ES5:
6 is logged 7 times instead of each number logged once as per const scoping behaviors in ES6.
Wrapping with the async function appears to be necessary to trigger the error

Input code

async function a() {
    for (const number_in_a_sequence of Array.from(new Array(7), (_,i) => i)) {
        setTimeout(() => console.log(number_in_a_sequence), 10)
    }
}
a()

Config

default playground configuration

Playground link

https://play.swc.rs/?version=1.2.119&code=H4sIAAAAAAAAA22OzQrCMBCE74W%2Bwxw3UIqePCn4Dt5DDBsJtLuYH0oR393UgieHuQwD34zLq3iEKr5EFTgyePUdmoImkFfJBVLnOycbxTqb%2BVlZPEMDrim5dQxJZxJe9kgnM4DsEA3OF0Tz423KXG5xZq2F6NtvfJ14nPRB%2F1Ya63gwO%2BDdd83t4Qe5CcJstAAAAA%3D%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

Expected behavior

each number from 0 to 6 should be logged once as per const binding behaviors in ES6.

Version

1.2.119

Additional context

No response

@kdy1 kdy1 modified the milestones: v1.2.120, v1.2.121 Dec 11, 2021
@kwonoj
Copy link
Member

kwonoj commented Dec 17, 2021

master seems to be fixed, need double confirm once new version's published.

@kdy1 kdy1 modified the milestones: v1.2.145, v1.2.146, v1.2.147, v1.2.148 Feb 24, 2022
@kdy1 kdy1 modified the milestones: v1.2.148, v1.2.149, v1.2.150 Mar 4, 2022
@kdy1 kdy1 added this to the Planned milestone Mar 12, 2022
@kdy1
Copy link
Member

kdy1 commented Mar 15, 2022

I investigated this, and the cause was that regenerator pass not considering block scoping of const number_in_a_sequence.

image

@kdy1 kdy1 self-assigned this Mar 15, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.156 Mar 15, 2022
kdy1 added a commit that referenced this issue Mar 15, 2022
**Description:**

We should apply `regenerator` after `block_scoping` because `regenerator` does not know how to handle `const`s.

**Related issue:**

 - Closes #3006
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 17, 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 17, 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.

4 participants