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
transform-regenerator incorrectly hoists loop variable declaration #12806
Comments
Hey @djsweet! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite." |
This is quite hard to fix in Would you like to open a PR adding generators to
At some point we'll need a proper fix, but we can at least "hide" the bug for now. |
Fixing this will also fix #12784 |
I'm uncertain of this but would this resolve an issue I've been finding in my code, where "const" variable declarations outside of a for loop inside a generator function have their value unexpectedly changed? Prettifying the code shows the variables have been reused by local variables inside the for loop, I'm currently working around this by inserting fake references to the variables late in the function to keep them "alive". |
* fix: enable transform-block-scoping with generators feature (#12806) * Update Babel 8 fixtures * Fix test difference in Babel 7 and 8 --------- Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
It has been fixed. |
Bug Report
Current behavior
Variable declaration scope is not respected directly by
@babel/transform-regenerator
. In this example,capture
is supposed to be scoped to thefor ... of
loop, but instead is hoisted outside of it. This breaks the expectation of the arrow function passed tosetTimeout
thatcapture
won't change: because it is scoped outside of thefor ... of
loop, it does change on every iteration, meaning that the callback is always givencapture = 4
.I get this output from
npx babel --plugins=@babel/transform-regenerator
:And the resulting console output is
Input Code
Expected behavior
Correct code is generated by
@babel/transform-regenerator
if preceded by@babel/transform-block-scoping
, by runningnpx babel --plugins=@babel/transform-block-scoping,@babel/transform-regenerator
.When executed,
capture
is always the value ofoffset
at the triggering iteration, so the resulting console output isBabel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
Environment
Possible Solution
If this is intentional behavior,
packages/babel-compat-data/data/plugins.json
should be updated such thattransform-block-scoping
is always triggered for browser versions which also triggertransform-regenerator
, e.g.as particular filters in
browserslist
can result in this plugin configuration.Additional context
I discovered the issue with the following configuration in my
browserslist
when using@babel/preset-env
:Removing
not ie <= 11
corrected the issue, as did addingnot chrome <= 49
. It seems thattransform-regenerator
has been depending on a precedingtransform-block-scoping
, which is always the case when supporting IE, but is not the case otherwise. Settingnot chrome <= 49
results inpreset-env
skipping overtransform-regenerator
entirely, avoiding the bug.The text was updated successfully, but these errors were encountered: