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
fix: babel-traverse constant flag in bindings #13777
Conversation
flag bindings in loops as not constant. Fixes: babel#13760
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48833/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 82b2d96:
|
I don't think we should fix the issue on |
for (let node of path.getAncestry()) { | ||
if (node.isLoop()) { | ||
debugger; | ||
this.constant = false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- When
node.isFunction()
we can break the loop, since variables don't cross function boundaries:is constant.for (;;) () => { var x };
- We only need to do this if we have a
var
, not forconst
orlet
- We don't need to collect the whole ancestry, since we'll only use part of it (up to a function). We can do something like
let { parentPath } = path; while (parentPath && !parentPath.isFunction()) { // ... ({ parentPath } = parentPath); }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a bit dangerous to mark all bindings within for loop as constant violations. The const
within loop is constant anyway.
We can handle it in
babel/packages/babel-traverse/src/scope/index.ts
Lines 772 to 780 in 3d1481f
if (local) { | |
this.registerConstantViolation(bindingPath); | |
} else { | |
this.bindings[name] = new Binding({ | |
identifier: id, | |
scope: this, | |
path: bindingPath, | |
kind: kind, | |
}); |
After we initialize the binding for var n
within the for loop, we should also register the constant violation if we see a Loop before a Function when we walk up the ancestry.
In this way the constantViolations
are also correct: The binding declaration itself violates the constant because the declaration is effectively hoisted to a function parent and the initializer assignment are executed for more than one time.
flag bindings in loops as not constant.
Fixes: #13760
Fixes #1, Fixes #2