[Bug]: Reference to class expression in private methods is not correctly compiled #13399
Closed
1 task
Labels
i: bug
outdated
A closed issue/PR that is archived due to age. Recommended to make a new issue
Spec: Class Fields
Spec: Private Methods
💻
How are you using Babel?
Programmatic API (
babel.transform
,babel.parse
)Input code
REPL
Configuration file name
No response
Configuration
Current and expected behavior
It is compiled to this:
As you can see,
WorkerClient
is not reachable. It should be compiled to something like this:Environment
Possible solution
In the
replaceThisContext
function that we use to replacethis
usage in class fields and private methods:babel/packages/babel-helper-create-class-features-plugin/src/fields.ts
Line 672 in 813f2a3
we should add a new
path.traverse
call to replace references to the inner binding, with a visitor similar to this one:where
state
is the one used bythisContextVisitor
with the addition ofstate.innerBinding
, which is the node representing the id of the class expression.Note that we don't need to merge this visitor with
environmentVisitor
:environmentVisitor
is only needed to avoid replacing nestedthis
es that don't refer to the class, while for normal bindings we usepath.scope.bindingIdentifierEquals
.We can get
innerBinding
from right before extracting the class reference atbabel/packages/babel-helper-create-class-features-plugin/src/index.ts
Lines 173 to 178 in 813f2a3
We only need to get it if we have a
ClassExpression
andpath.node.id
is set, otherwise we can leaveinnerBinding
asundefined
and use it as a "signal" to skip the newpath.traverse
call.After working on the fix itself, we'll need a bunch of tests:
input.js
/output.js
test, for example my original exampleexec.js
test, to check that the binding is the correct one. For example:input
/output
test to verify that it works when the binding is used in nested scopes, for example:input
/output
test to verify that it doesn't rewrite shadowed bindings:Additional context
The same bug happens with static class fields (both public and private) and instance private methods:
we should duplicate the tests for those cases.
The text was updated successfully, but these errors were encountered: