fix: fix static private field shadowed by local variable#13656
fix: fix static private field shadowed by local variable#13656nicolo-ribaudo merged 5 commits intobabel:mainfrom colinaaa:fix/shadowed-private-static-field
Conversation
currently throw an error, maybe we could generate correct code fix #12960
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48164/ |
|
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 68decd2:
|
| : "classStaticPrivateFieldSpecGet"; | ||
|
|
||
| const binding = member.scope.getBinding(classRef.name); | ||
| if (innerBinding && binding && !(binding.identifier === innerBinding)) { |
There was a problem hiding this comment.
Nit:
| if (innerBinding && binding && !(binding.identifier === innerBinding)) { | |
| if (innerBinding && binding && binding.identifier !== innerBinding) { |
Also, binding should always be defined (because if there isn't a conflicting variable, it's innerBinding). It would be better to assert it, rather than checking it:
if (innerBinding) {
if (!binding) throw new Error("Internal Babel error: binding should be defined");
if (binding.identifier !== innerBinding) {
// ...
}
}There was a problem hiding this comment.
Infect, binding could be undefined.
e.g: for a ClassExpression, classRef is generated by path.scope.generateUidIdentifier("class"); and would be inserted after our transformation.
const cls = class Test {
static #x = 1
method() {
const Test = 1;
return this.#x;
}
}There was a problem hiding this comment.
Oh thanks, I didn't realize it
| throw binding.path.buildCodeFrameError( | ||
| `Shadowing class ${classRef.name} with private property`, | ||
| ); |
There was a problem hiding this comment.
Q: any idea how could we generate correct code?
Should we rename the local variable?
Yeah, I'd rename the local variable:
path.scope.rename(classRef.name);There was a problem hiding this comment.
Sure!
I renamed all the local variables that shadowing the classRef including those in parent scopes.
...ges/babel-plugin-proposal-class-properties/test/fixtures/private-loose/static-shadow/exec.js
Outdated
Show resolved
Hide resolved
| innerBinding: t.Identifier | undefined, | ||
| ) { | ||
| const binding = scope.getBinding(name); | ||
| if (innerBinding && binding && innerBinding !== binding.identifier) { |
There was a problem hiding this comment.
The if branch is an inlined version of Scope#bindingIdentifierEquals, guess we can just put
while (!scope.bindingIdentifierEquals(name, innerBinding)) {
scope.rename(name);
scope = scope.parent;
}here?
There was a problem hiding this comment.
Sounds great! And all tests passed.
But I got a question: there are some cases that scope.bindingIdentifier(name) === undefined and innerBinding !== undefined which means scope.bindingIdentifierEquals(name, innerBinding) will always returns false. This will cause the while loop runs all the way alone to the top level scope and renaming all the variables.
The renaming and looping here are useless, and maybe cause performance problem.
So maybe we change it to:
while (
scope?.hasBinding(name) &&
!scope.bindingIdentifierEquals(name, innerBinding)
) {
scope.rename(name);
scope = scope.parent;
}There was a problem hiding this comment.
there are some cases that scope.bindingIdentifier(name) === undefined and innerBinding !== undefined
If the scope is the parent scope of the class where innerBinding is defined, then scope.bindingIdentifier(classRef.name) is surely undefined, unless defined otherwise. We could exit the loop after scope becomes the class scope.
simplify logic and add comments
| scope?.hasBinding(name) && | ||
| !scope.bindingIdentifierEquals(name, innerBinding) | ||
| ) { | ||
| scope.rename(name); |
There was a problem hiding this comment.
Can renaming ever fail? I can't think of a case myself.
There was a problem hiding this comment.
No, it always succeeds (but it might be observable since it sometimes changes the .name property of functions).
use
innerBindingcreated by #13429, and check whether theclassReffor private fields equals theinnerBinding.currently throw an error, maybe we could generate correct code
Q: any idea how could we generate correct code?
Should we rename the local variable?
Or rename the class?
Or capture a separate reference in a temp variable before it's shadowed?