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: fix static private field shadowed by local variable #13656
fix: fix static private field shadowed by local variable #13656
Conversation
currently throw an error, maybe we could generate correct code fix babel#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:
|
@@ -273,6 +274,13 @@ const privateNameHandlerSpec: Handler<PrivateNameState & Receiver> & Receiver = | |||
? "classStaticPrivateMethodGet" | |||
: "classStaticPrivateFieldSpecGet"; | |||
|
|||
const binding = member.scope.getBinding(classRef.name); | |||
if (innerBinding && binding && !(binding.identifier === innerBinding)) { |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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.
Thanks!
innerBinding: t.Identifier | undefined, | ||
) { | ||
const binding = scope.getBinding(name); | ||
if (innerBinding && binding && innerBinding !== binding.identifier) { |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can renaming ever fail? I can't think of a case myself.
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.
No, it always succeeds (but it might be observable since it sometimes changes the .name
property of functions).
use
innerBinding
created by #13429, and check whether theclassRef
for 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?