Skip to content

Commit 84e5c05

Browse files
Closure Teamcopybara-github
Closure Team
authored andcommitted
Fix Es6RewriteClassExtendsExpressions pass to use the recommended traversal
pattern, by just using `shouldTraverse` to traverse only relevant files for the given language feature. PiperOrigin-RevId: 601938308
1 parent eec68e4 commit 84e5c05

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
* <p>TODO(bradfordcsmith): This pass may no longer be necessary once the typechecker passes have
4949
* all been updated to understand ES6 classes.
5050
*/
51-
public final class Es6RewriteClassExtendsExpressions extends NodeTraversal.AbstractPostOrderCallback
52-
implements CompilerPass {
51+
public final class Es6RewriteClassExtendsExpressions
52+
implements NodeTraversal.Callback, CompilerPass {
5353

5454
static final String CLASS_EXTENDS_VAR = "$classextends$var";
5555

@@ -63,9 +63,19 @@ public final class Es6RewriteClassExtendsExpressions extends NodeTraversal.Abstr
6363
this.astFactory = compiler.createAstFactory();
6464
}
6565

66+
6667
@Override
6768
public void process(Node externs, Node root) {
68-
TranspilationPasses.processTranspile(compiler, root, features, this);
69+
NodeTraversal.traverse(compiler, root, this);
70+
}
71+
72+
@Override
73+
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
74+
if (n.isScript()) {
75+
FeatureSet scriptFeatures = NodeUtil.getFeatureSetOfScript(n);
76+
return scriptFeatures == null || scriptFeatures.contains(features);
77+
}
78+
return true;
6979
}
7080

7181
@Override

test/com/google/javascript/jscomp/Es6RewriteClassExtendsExpressionsTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ public void testClassExpressions() {
9696
"baz(class extends foo.bar {});"));
9797
}
9898

99+
@Test
100+
public void testClassExtendsClassTranspilation() {
101+
String code =
102+
lines(
103+
"class __PRIVATE_WebChannelConnection extends class __PRIVATE_RestConnection {",
104+
" constructor(e) {",
105+
" this.databaseInfo = e, this.databaseId = e.databaseId;",
106+
" }",
107+
"} {",
108+
" constructor(e) {",
109+
" super(e), this.forceLongPolling = e.forceLongPolling, this.autoDetectLongPolling"
110+
+ " = e.autoDetectLongPolling, this.useFetchStreams = e.useFetchStreams,"
111+
+ " this.longPollingOptions = e.longPollingOptions;",
112+
" console.log('test');",
113+
" }",
114+
"}");
115+
String expectedCode =
116+
lines(
117+
"const testcode$classextends$var0 = class __PRIVATE_RestConnection {",
118+
" constructor(e) {",
119+
" this.databaseInfo = e, this.databaseId = e.databaseId;",
120+
" }",
121+
"};",
122+
"class __PRIVATE_WebChannelConnection extends testcode$classextends$var0 {",
123+
" constructor(e) {",
124+
" super(e), this.forceLongPolling = e.forceLongPolling, this.autoDetectLongPolling"
125+
+ " = e.autoDetectLongPolling, this.useFetchStreams = e.useFetchStreams,"
126+
+ " this.longPollingOptions = e.longPollingOptions;",
127+
" console.log('test');",
128+
" }",
129+
"}");
130+
test(code, expectedCode);
131+
}
132+
99133
@Test
100134
public void testVarDeclaration() {
101135
test(

test/com/google/javascript/jscomp/integration/ES2022IntegrationTest.java

+64
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,68 @@ public void computedMethodExecutionOrderAndDeadAssignmentElimination() {
385385
" console.log(c.a)",
386386
"};"));
387387
}
388+
389+
@Test
390+
public void testEs6RewriteClassExtendsExpression() {
391+
CompilerOptions options = createCompilerOptions();
392+
393+
String code =
394+
lines(
395+
"/** @unrestricted */",
396+
"class __PRIVATE_WebChannelConnection extends class __PRIVATE_RestConnection {",
397+
" constructor(e) {",
398+
" this.databaseInfo = e, this.databaseId = e.databaseId;",
399+
" }",
400+
"} {",
401+
" constructor(e) {",
402+
" super(e), this.forceLongPolling = e.forceLongPolling, this.autoDetectLongPolling"
403+
+ " = e.autoDetectLongPolling, this.useFetchStreams = e.useFetchStreams,"
404+
+ " this.longPollingOptions = e.longPollingOptions;",
405+
" console.log('test');",
406+
" }",
407+
"}");
408+
String expectedCodeNonTraspiled =
409+
lines(
410+
"class __PRIVATE_WebChannelConnection extends class __PRIVATE_RestConnection {",
411+
" constructor(e) {",
412+
" this.databaseInfo = e, this.databaseId = e.databaseId;",
413+
" }",
414+
"} {",
415+
" constructor(e) {",
416+
" super(e), this.forceLongPolling = e.forceLongPolling, this.autoDetectLongPolling ="
417+
+ " ",
418+
" e.autoDetectLongPolling, this.useFetchStreams = e.useFetchStreams,"
419+
+ " this.longPollingOptions = ",
420+
" e.longPollingOptions;",
421+
" console.log('test');",
422+
" }",
423+
"}\n");
424+
425+
String expectedCodeTranspiled =
426+
lines(
427+
"const i0$classdecl$var0 = class {",
428+
" constructor(e) {",
429+
" this.databaseInfo = e, this.databaseId = e.databaseId;",
430+
" }",
431+
"};",
432+
"const i0$classextends$var0 = i0$classdecl$var0;",
433+
"class __PRIVATE_WebChannelConnection extends i0$classdecl$var0 {",
434+
" constructor(e) {",
435+
" super(e), this.forceLongPolling = e.forceLongPolling, this.autoDetectLongPolling ="
436+
+ " ",
437+
" e.autoDetectLongPolling, this.useFetchStreams = e.useFetchStreams,"
438+
+ " this.longPollingOptions = ",
439+
" e.longPollingOptions;",
440+
" console.log('test');",
441+
" }",
442+
"}\n");
443+
444+
options.setLanguageIn(LanguageMode.UNSTABLE);
445+
options.setLanguageOut(LanguageMode.ECMASCRIPT_2019);
446+
test(options, code, expectedCodeTranspiled);
447+
448+
options.setLanguageIn(LanguageMode.UNSTABLE);
449+
options.setLanguageOut(LanguageMode.UNSTABLE);
450+
test(options, code, expectedCodeNonTraspiled);
451+
}
388452
}

0 commit comments

Comments
 (0)