You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current Behavior/Expected Behavior/Potential Solution
I am developing a custom plugin. The details of the plugin are irrelevant to this bug.
Given this code:
constfoo='1'constmain=()=>{constfoo='2'}
If the inner foo declaration is removed via path.remove(), then the binding for foo in the inner scope should be removed, and the binding for foo in the outer scope should not be removed.
Instead, both the inner and outer bindings are removed.
Here is a codesandbox that demonstrates the issue (open devtools to see the logs)
Changing removeBinding to removeOwnBinding seems to fix this use case, but I'm unsure if that would cause problems in other use cases. Any other ideas? Is there good test coverage on scope tracking?
The text was updated successfully, but these errors were encountered:
Hey @calebeby! 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."
Changing removeBinding to removeOwnBinding seems to fix this use case, but I'm unsure if that would cause problems in other use cases. Any other ideas? Is there good test coverage on scope tracking?
Since _removeFromScope uses the exact scope that contains the binding declaration, I cannot think about any possible negative side effect 👍
For some reason all of the scopes in this demo are empty/have no bindings. I assume it is something related to codesandbox. Maybe someone knows how to get it working?
If I add a debugger; statement to the plugin I see the bindings 🤷
I'm assigning this issue to you and I'm not writing any "hints" because you have already investigated a lot, but feel free to ask if something is not clear!
Bug Report
Current Behavior/Expected Behavior/Potential Solution
I am developing a custom plugin. The details of the plugin are irrelevant to this bug.
Given this code:
If the inner
foo
declaration is removed viapath.remove()
, then the binding forfoo
in the inner scope should be removed, and the binding forfoo
in the outer scope should not be removed.Instead, both the inner and outer bindings are removed.
Here is a codesandbox that demonstrates the issue (open devtools to see the logs)
Codesandbox
I believe that here is what happens when the inner
foo
is removed viapath.remove()
:foo
is removed from inner scope: https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/removal.js#L11VariableDeclarator
in theVariableDeclaration
,remove()
is called on theVariableDeclaration
: https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/lib/removal-hooks.js#L25_removeFromScope
in theVariableDeclaration
:this.getBindingIdentifiers()
includes thefoo
binding because the innerVariableDeclaratior
hasn't actually been removed yet: https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/removal.js#L25this.scope.removeBinding('foo')
is called, but this time it gets removed from the outer scope becausethis.scope.removeBinding
scans upwards for the binding, and thefoo
binding has already been removed from the inner scope: https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/removal.js#L26Changing
removeBinding
toremoveOwnBinding
seems to fix this use case, but I'm unsure if that would cause problems in other use cases. Any other ideas? Is there good test coverage on scope tracking?The text was updated successfully, but these errors were encountered: