Skip to content

Commit fbd8091

Browse files
Closure Teamcopybara-github
Closure Team
authored andcommitted
Modify RewriteClassMembers.java to handle static members with no reference to this or super differently.
before transpilation - ``` class A { static x = 1; static [y] = 2; static z = () => this.x; } ``` after transpilation - ``` class A { static STATIC$FIELD$0$z() { return () => { return this.x; }; } } A.x = 1; A[y] = 2; A.z = A.STATIC$FIELD$0$z();" ``` PiperOrigin-RevId: 623267705
1 parent 4776a87 commit fbd8091

File tree

2 files changed

+114
-339
lines changed

2 files changed

+114
-339
lines changed

src/com/google/javascript/jscomp/RewriteClassMembers.java

+14
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ private void rewriteStaticMembers(NodeTraversal t, ClassRecord record) {
286286
convStaticMethodToCall(nameToUse, staticMember.detach(), /* callNode= */ null);
287287
break;
288288
}
289+
// if staticMember doesn't reference this or super, then don't extract it into a static
290+
// method
291+
if (!NodeUtil.referencesEnclosingReceiver(staticMember)) {
292+
transpiledNode = convNonCompFieldToGetProp(nameToUse, staticMember.detach());
293+
break;
294+
}
289295
blockNode = createBlockNodeWithReturn(staticMember.removeFirstChild());
290296
callNode =
291297
convBlockToStaticMethod(
@@ -300,6 +306,14 @@ private void rewriteStaticMembers(NodeTraversal t, ClassRecord record) {
300306
break;
301307
case COMPUTED_FIELD_DEF:
302308
if (staticMember.hasChildren() && staticMember.getChildCount() > 1) {
309+
310+
// if staticMember doesn't reference this or super, then don't extract it into a static
311+
// method
312+
if (!NodeUtil.referencesEnclosingReceiver(staticMember)) {
313+
transpiledNode = convCompFieldToGetElem(nameToUse, staticMember.detach());
314+
break;
315+
}
316+
303317
blockNode = createBlockNodeWithReturn(staticMember.getLastChild().detach());
304318
callNode =
305319
convBlockToStaticMethod(

0 commit comments

Comments
 (0)