Skip to content

Commit 9dbe220

Browse files
Closure Teamcopybara-github
authored andcommitted
Move Es6RewriteClassExtends and Es6ExtractClasses passes above RewriteClassMembers in the transpilation order.
This change allows RewriteClassMembers to correctly transpile public fields when the class is located in a location that doesn't easily allow multiple statements such as declaring a class inside of a new call (e.g. `new (class Foo { static x; })()`). For that to work Es6ExtractClasses was also changed to transpile all classes instead of only transpiling when it detects that Es6 classes are not supported for the output language. PiperOrigin-RevId: 552519883
1 parent 0f94b98 commit 9dbe220

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType;
2525
import com.google.javascript.jscomp.colors.StandardColors;
2626
import com.google.javascript.jscomp.deps.ModuleNames;
27-
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
2827
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
2928
import com.google.javascript.rhino.IR;
3029
import com.google.javascript.rhino.JSDocInfo;
@@ -67,7 +66,6 @@ public final class Es6ExtractClasses extends NodeTraversal.AbstractPostOrderCall
6766
private final AstFactory astFactory;
6867
private final ExpressionDecomposer expressionDecomposer;
6968
private int classDeclVarCounter = 0;
70-
private static final FeatureSet features = FeatureSet.BARE_MINIMUM.with(Feature.CLASSES);
7169

7270
Es6ExtractClasses(AbstractCompiler compiler) {
7371
this.compiler = compiler;
@@ -83,10 +81,8 @@ public void process(Node externs, Node root) {
8381
// TODO(b/197349249): Remove once b/197349249 is fixed.
8482
NodeTraversal.traverse(compiler, root, new NormalizeArrows());
8583
}
86-
TranspilationPasses.processTranspile(
87-
compiler, externs, features, this, new SelfReferenceRewriter());
88-
TranspilationPasses.processTranspile(
89-
compiler, root, features, this, new SelfReferenceRewriter());
84+
NodeTraversal.traverseRoots(compiler, this, externs, root);
85+
NodeTraversal.traverseRoots(compiler, new SelfReferenceRewriter(), externs, root);
9086
}
9187

9288
@Override

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public static void addEarlyOptimizationTranspilationPasses(
7878
"markEs2022FeaturesNotRequiringTranspilationAsRemoved", Feature.REGEXP_FLAG_D));
7979
}
8080

81+
if (options.needsTranspilationOf(Feature.PUBLIC_CLASS_FIELDS)
82+
|| options.needsTranspilationOf(Feature.CLASS_STATIC_BLOCK)
83+
|| options.needsTranspilationOf(Feature.CLASSES)) {
84+
// Make sure that a variable is created to hold every class definition.
85+
// This allows us to add static properties and methods by adding properties
86+
// to that variable.
87+
passes.maybeAdd(es6RewriteClassExtends);
88+
passes.maybeAdd(es6ExtractClasses);
89+
}
90+
8191
if (options.needsTranspilationOf(Feature.PUBLIC_CLASS_FIELDS)
8292
|| options.needsTranspilationOf(Feature.CLASS_STATIC_BLOCK)) {
8393
passes.maybeAdd(rewriteClassMembers);
@@ -173,7 +183,6 @@ public static void addEarlyOptimizationTranspilationPasses(
173183
passes.maybeAdd(es6NormalizeShorthandProperties);
174184
}
175185
if (options.needsTranspilationOf(Feature.CLASSES)) {
176-
passes.maybeAdd(es6RewriteClassExtends);
177186
passes.maybeAdd(es6ConvertSuper);
178187
}
179188
if (options.needsTranspilationFrom(
@@ -190,7 +199,6 @@ public static void addEarlyOptimizationTranspilationPasses(
190199
passes.maybeAdd(es6RewriteArrowFunction);
191200
}
192201
if (options.needsTranspilationOf(Feature.CLASSES)) {
193-
passes.maybeAdd(es6ExtractClasses);
194202
passes.maybeAdd(es6RewriteClass);
195203
}
196204
if (options.needsTranspilationFrom(

test/com/google/javascript/jscomp/TranspileOnlyIntegrationTest.java renamed to test/com/google/javascript/jscomp/integration/TranspileOnlyIntegrationTest.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.google.javascript.jscomp;
16+
package com.google.javascript.jscomp.integration;
1717

18-
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.javascript.jscomp.base.JSCompStrings.lines;
1919

20+
import com.google.javascript.jscomp.CompilerOptions;
2021
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
2122
import org.jspecify.nullness.Nullable;
2223
import org.junit.Before;
@@ -30,14 +31,12 @@
3031
* <p>This class actually tests several transpilation passes together.
3132
*/
3233
@RunWith(JUnit4.class)
33-
public final class TranspileOnlyIntegrationTest {
34+
public final class TranspileOnlyIntegrationTest extends IntegrationTestCase {
3435

35-
private @Nullable Compiler compiler = null;
3636
private @Nullable CompilerOptions options = null;
3737

3838
@Before
3939
public void init() {
40-
compiler = new Compiler();
4140
options = new CompilerOptions();
4241
options.setLanguage(LanguageMode.ECMASCRIPT_NEXT);
4342
options.setSkipNonTranspilationPasses(true);
@@ -47,21 +46,41 @@ public void init() {
4746
@Test
4847
public void esModuleNoTranspilationForSameLanguageLevel() {
4948
String js = "export default function fn(){};";
50-
test(js, js);
49+
test(options, js, js);
5150
}
5251

5352
@Test
5453
public void esModuleTranspilationForDifferentLanguageLevel() {
5554
String js = "export default function fn() {}";
5655
String transpiled =
57-
"function fn$$module$in(){}var module$in={};module$in.default=fn$$module$in;";
56+
"function fn$$module$i0(){}var module$i0={};module$i0.default=fn$$module$i0;";
5857
options.setLanguageOut(LanguageMode.ECMASCRIPT_2020);
59-
test(js, transpiled);
58+
test(options, js, transpiled);
6059
}
6160

62-
private void test(String js, String transpiled) {
63-
compiler.compile(SourceFile.fromCode("ext.js", ""), SourceFile.fromCode("in.js", js), options);
64-
assertThat(compiler.getErrors()).isEmpty();
65-
assertThat(compiler.toSource()).isEqualTo(transpiled);
61+
// Added when moving Es6ExtractClasses before RewriteClassMembers and fixing an issue with
62+
// not rewriting extends
63+
@Test
64+
public void testClassExtendsAnonymousClass() {
65+
options.setLanguage(LanguageMode.UNSTABLE);
66+
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
67+
test(
68+
options,
69+
lines(
70+
"class Bar {}", //
71+
"class Foo extends (class extends Bar {}) {",
72+
" static x;",
73+
"}"),
74+
lines(
75+
"var Bar = function() {};",
76+
"var i0$classextends$var0 = function() {",
77+
" Bar.apply(this, arguments)",
78+
"};",
79+
"$jscomp.inherits(i0$classextends$var0, Bar);",
80+
"var Foo = function() {",
81+
" i0$classextends$var0.apply(this, arguments)",
82+
"};",
83+
"$jscomp.inherits(Foo, i0$classextends$var0);",
84+
"Foo.x;"));
6685
}
6786
}

0 commit comments

Comments
 (0)