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

Removing a VariableDeclarator causes same-named variables in outer scope's bindings to be removed #11551

Open
1 task done
calebeby opened this issue May 12, 2020 · 2 comments · May be fixed by #11553
Open
1 task done

Comments

@calebeby
Copy link
Contributor

calebeby commented May 12, 2020

Bug Report

  • I would like to work on a fix!

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:

const foo = '1'
const main = () => {
	const foo = '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)

Codesandbox

I believe that here is what happens when the inner foo is removed via path.remove():

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?

@babel-bot
Copy link
Collaborator

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."

@nicolo-ribaudo
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants