Skip to content

Commit 32d43fd

Browse files
trevoradecopybara-github
authored andcommitted
Add ES2022 configuration to JSCompiler.
PiperOrigin-RevId: 839794371
1 parent 2b30bfa commit 32d43fd

8 files changed

Lines changed: 49 additions & 22 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,6 +3132,9 @@ public enum LanguageMode {
31323132
/** ECMAScript standard approved in 2021. */
31333133
ECMASCRIPT_2021,
31343134

3135+
/** ECMAScript standard approved in 2022. */
3136+
ECMASCRIPT_2022,
3137+
31353138
/** ECMAScript features from the upcoming standard. */
31363139
ECMASCRIPT_NEXT,
31373140

@@ -3215,6 +3218,7 @@ public FeatureSet toFeatureSet() {
32153218
case ECMASCRIPT_2019 -> FeatureSet.ES2019_MODULES;
32163219
case ECMASCRIPT_2020 -> FeatureSet.ES2020_MODULES;
32173220
case ECMASCRIPT_2021 -> FeatureSet.ES2021_MODULES;
3221+
case ECMASCRIPT_2022 -> FeatureSet.ES2022_MODULES;
32183222
case ECMASCRIPT_NEXT -> FeatureSet.ES_NEXT;
32193223
case NO_TRANSPILE, UNSTABLE -> FeatureSet.ES_UNSTABLE;
32203224
case UNSUPPORTED -> FeatureSet.ES_UNSUPPORTED;

src/com/google/javascript/jscomp/parsing/Config.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum LanguageMode {
5454
ECMASCRIPT_2019(FeatureSet.ES2019_MODULES),
5555
ECMASCRIPT_2020(FeatureSet.ES2020_MODULES),
5656
ECMASCRIPT_2021(FeatureSet.ES2021_MODULES),
57+
ECMASCRIPT_2022(FeatureSet.ES2022_MODULES),
5758
// NOTE: When adding a new language level here, also update latestEcmaScript() below to return
5859
// it.
5960
ES_NEXT(FeatureSet.ES_NEXT),

src/com/google/javascript/jscomp/parsing/ParserRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ private static com.google.javascript.jscomp.parsing.parser.Parser.Config newPars
163163
ECMASCRIPT_2019,
164164
ECMASCRIPT_2020,
165165
ECMASCRIPT_2021,
166+
ECMASCRIPT_2022,
166167
ES_NEXT,
167168
UNSTABLE,
168169
UNSUPPORTED ->

src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@ public final class FeatureSet implements Serializable {
8585

8686
public static final FeatureSet ES2021 = ES2021_MODULES.without(Feature.MODULES);
8787

88-
// NOTE: when ES2022 / ES2023 are added, the BROWSER_2024 FeatureSet defined below should be
89-
// updated to include them.
88+
public static final FeatureSet ES2022_MODULES =
89+
ES2021_MODULES.with(LangVersion.ES2022.features());
90+
91+
public static final FeatureSet ES2022 = ES2022_MODULES.without(Feature.MODULES);
92+
93+
// NOTE: when ES2023 is added, the BROWSER_2024 and BROWSER_2025 FeatureSets defined below should
94+
// be updated to include it.
9095

9196
// Set of all fully supported features, even those part of language versions not fully supported
92-
public static final FeatureSet ES_NEXT = ES2021_MODULES.with(LangVersion.ES_NEXT.features());
97+
public static final FeatureSet ES_NEXT = ES2022_MODULES.with(LangVersion.ES_NEXT.features());
9398

9499
// Set of features fully supported in checks, even those not fully supported in optimizations
95100
public static final FeatureSet ES_UNSTABLE = ES_NEXT.with(LangVersion.ES_UNSTABLE.features());
@@ -154,21 +159,23 @@ public final class FeatureSet implements Serializable {
154159
Feature.REGEXP_LOOKBEHIND);
155160

156161
public static final FeatureSet BROWSER_2023 =
157-
ES2021_MODULES.without(
162+
ES2022_MODULES.without(
158163
// IMPORTANT: There is special casing for this feature and the ones excluded for
159164
// BROWSER_2020 above in RewritePolyfills.
160165
// If future Browser FeatureSet Year definitions have to remove any other features, then
161166
// we need to change the way that is done to avoid incorrect inclusion of polyfills.
162167
// Chrome 62 (2017). Firefox 78 (2020). Safari 16.4 (2023).
163-
Feature.REGEXP_LOOKBEHIND);
168+
Feature.REGEXP_LOOKBEHIND,
169+
// Chrome 94 (2021). Firefox 93 (2021). Safari 16.4 (2023).
170+
Feature.CLASS_STATIC_BLOCK);
164171

165172
// Note: Only "Hashbang Grammar" is an ES2023 syntax feature. Its versions are:
166173
// Chrome 74 (2019). Firefox 67 (2019). Safari 13.1 (2020).
167-
public static final FeatureSet BROWSER_2024 = ES2021_MODULES;
174+
public static final FeatureSet BROWSER_2024 = ES2022_MODULES;
168175

169176
// Note: Only "RegExp `v` flag" is an ES2024 syntax feature. Its versions are:
170177
// Chrome 112 (2023). Firefox 116 (2023). Safari 17 (2023).
171-
public static final FeatureSet BROWSER_2025 = ES2021_MODULES;
178+
public static final FeatureSet BROWSER_2025 = ES2022_MODULES;
172179

173180
public static final FeatureSet ALL = ES_UNSUPPORTED.with(LangVersion.TYPESCRIPT.features());
174181

@@ -182,6 +189,7 @@ private enum LangVersion {
182189
ES2019,
183190
ES2020,
184191
ES2021,
192+
ES2022,
185193
ES_NEXT,
186194
ES_UNSTABLE,
187195
ES_UNSUPPORTED,
@@ -292,9 +300,15 @@ public enum Feature {
292300
NUMERIC_SEPARATOR("numeric separator", LangVersion.ES2021),
293301
LOGICAL_ASSIGNMENT("Logical assignments", LangVersion.ES2021),
294302

303+
// ES 2022 adds https://github.com/tc39/proposal-class-fields
304+
PUBLIC_CLASS_FIELDS("Public class fields", LangVersion.ES2022),
305+
306+
// ES 2022 adds https://github.com/tc39/proposal-class-static-block
307+
CLASS_STATIC_BLOCK("Class static block", LangVersion.ES2022),
308+
295309
// ES 2022 adds https://github.com/tc39/proposal-regexp-match-indices
296310
// Note: Untranspilable.
297-
REGEXP_FLAG_D("RegExp flag 'd'", LangVersion.ES_NEXT),
311+
REGEXP_FLAG_D("RegExp flag 'd'", LangVersion.ES2022),
298312

299313
// ES_NEXT: Features that are fully supported, but part of a language version that is not yet
300314
// fully supported
@@ -311,11 +325,6 @@ public enum Feature {
311325
// incorrectly prune those polyfills.
312326
ES_UNSTABLE_RUNTIME("es_unstable runtime", LangVersion.ES_UNSTABLE),
313327

314-
PUBLIC_CLASS_FIELDS("Public class fields", LangVersion.ES_NEXT), // Part of ES2022
315-
316-
// ES 2022 adds https://github.com/tc39/proposal-class-static-block
317-
CLASS_STATIC_BLOCK("Class static block", LangVersion.ES_UNSTABLE),
318-
319328
// ES_UNSUPPORTED: Features that we can parse, but not yet supported in all checks
320329

321330
// ES 2022 adds https://github.com/tc39/proposal-class-fields
@@ -377,6 +386,9 @@ public String version() {
377386
if (ES2021_MODULES.contains(this)) {
378387
return "es_2021";
379388
}
389+
if (ES2022_MODULES.contains(this)) {
390+
return "es_2022";
391+
}
380392
if (ES_NEXT.contains(this)) {
381393
return "es_next";
382394
}
@@ -530,6 +542,7 @@ public static FeatureSet valueOf(String name) {
530542
case "es_2019" -> ES2019;
531543
case "es_2020" -> ES2020;
532544
case "es_2021" -> ES2021;
545+
case "es_2022" -> ES2022;
533546
case "es_next" -> ES_NEXT;
534547
case "es_unstable" -> ES_UNSTABLE;
535548
case "es_unsupported" -> ES_UNSUPPORTED;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testMinimumBrowserFeatureSetYearRequiredFor() {
106106
public void testMinimumBrowserFeatureSetYearRequiredFor_returnsUnspecifiedIfUnsupported() {
107107
// Newer features, in particular anything in ES_NEXT, may not be part of a browser featureset
108108
// year yet.
109-
assertThat(BrowserFeaturesetYear.minimumRequiredFor(Feature.PUBLIC_CLASS_FIELDS)).isNull();
109+
assertThat(BrowserFeaturesetYear.minimumRequiredFor(Feature.ES_NEXT_RUNTIME)).isNull();
110110
}
111111

112112
@Test

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,21 @@ public void testEs2022RegexFlagD() {
248248
"const a = /^(?<half>.*).\\k<half>$/d;",
249249
UNTRANSPILABLE_FEATURE_PRESENT,
250250
"""
251-
Cannot convert feature "RegExp flag 'd'" to targeted output language. Feature requires\
252-
at minimum ES_NEXT. Consider targeting a more modern output.\
251+
Cannot convert feature "RegExp flag 'd'" to targeted output language. Feature requires at \
252+
minimum ECMASCRIPT_2022. Consider targeting a more modern output.\
253253
""");
254254
}
255255

256256
@Test
257257
public void testEs2022RegexFlagD_usingBrowserFeaturesetYear() {
258-
browserFeaturesetYear = BrowserFeaturesetYear.YEAR_2024;
258+
browserFeaturesetYear = BrowserFeaturesetYear.YEAR_2022;
259259
testError(
260260
"const a = /^(?<half>.*).\\k<half>$/d;",
261261
UNTRANSPILABLE_FEATURE_PRESENT,
262262
"""
263-
Cannot convert feature "RegExp flag 'd'" to targeted output language. Feature requires\
264-
at minimum ES_NEXT, which is not yet supported by any browser featureset year.\
263+
Cannot convert feature "RegExp flag 'd'" to targeted output language. Feature requires at \
264+
minimum browser featureset year 2023. Consider targeting a more modern output.
265+
Current browser featureset year: 2022\
265266
""");
266267
}
267268

test/com/google/javascript/jscomp/parsing/ParserTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6127,8 +6127,10 @@ public void testClassComputedField_es2020_warnsForCodeOutsideClosureUnawareRange
61276127
// This warning is expected - it is used to halt compilation during parsing when
61286128
// closure-unaware code is present when it should not be.
61296129
"@closureUnaware annotation is not allowed in this compilation",
6130-
"This language feature is only supported for ES_NEXT mode or better: Public class"
6131-
+ " fields");
6130+
"""
6131+
This language feature is only supported for ECMASCRIPT_2022 mode or better: \
6132+
Public class fields\
6133+
""");
61326134

61336135
expectFeatures(Feature.CLASSES, Feature.PUBLIC_CLASS_FIELDS);
61346136
}

test/com/google/javascript/jscomp/parsing/parser/FeatureSetTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void testEsOrdering() {
5050
assertFS(FeatureSet.ALL).contains(FeatureSet.ES_UNSUPPORTED);
5151
assertFS(FeatureSet.ES_UNSUPPORTED).contains(FeatureSet.ES_UNSTABLE);
5252
assertFS(FeatureSet.ES_UNSTABLE).contains(FeatureSet.ES_NEXT);
53-
assertFS(FeatureSet.ES_NEXT).contains(FeatureSet.ES2021);
53+
assertFS(FeatureSet.ES_NEXT).contains(FeatureSet.ES2022);
54+
assertFS(FeatureSet.ES2022).contains(FeatureSet.ES2021);
5455
assertFS(FeatureSet.ES2021).contains(FeatureSet.ES2020);
5556
assertFS(FeatureSet.ES2020).contains(FeatureSet.ES2019);
5657
assertFS(FeatureSet.ES2019).contains(FeatureSet.ES2018);
@@ -64,6 +65,7 @@ public void testEsOrdering() {
6465

6566
@Test
6667
public void testEsModuleOrdering() {
68+
assertFS(FeatureSet.ES2022_MODULES.without(Feature.MODULES)).equals(FeatureSet.ES2022);
6769
assertFS(FeatureSet.ES2021_MODULES.without(Feature.MODULES)).equals(FeatureSet.ES2021);
6870
assertFS(FeatureSet.ES2020_MODULES.without(Feature.MODULES)).equals(FeatureSet.ES2020);
6971
assertFS(FeatureSet.ES2019_MODULES.without(Feature.MODULES)).equals(FeatureSet.ES2019);
@@ -92,6 +94,8 @@ public void testVersionForDebugging() {
9294
assertThat(FeatureSet.ES2020_MODULES.version()).isEqualTo("es_2020");
9395
assertThat(FeatureSet.ES2021.version()).isEqualTo("es_2021");
9496
assertThat(FeatureSet.ES2021_MODULES.version()).isEqualTo("es_2021");
97+
assertThat(FeatureSet.ES2022.version()).isEqualTo("es_2022");
98+
assertThat(FeatureSet.ES2022_MODULES.version()).isEqualTo("es_2022");
9599
assertThat(FeatureSet.ALL.version()).isEqualTo("all");
96100
}
97101

@@ -120,6 +124,7 @@ public void testValueOf() {
120124
assertFS(FeatureSet.valueOf("es_2019")).equals(FeatureSet.ES2019);
121125
assertFS(FeatureSet.valueOf("es_2020")).equals(FeatureSet.ES2020);
122126
assertFS(FeatureSet.valueOf("es_2021")).equals(FeatureSet.ES2021);
127+
assertFS(FeatureSet.valueOf("es_2022")).equals(FeatureSet.ES2022);
123128
assertFS(FeatureSet.valueOf("es_next")).equals(FeatureSet.ES_NEXT);
124129
assertFS(FeatureSet.valueOf("es_unstable")).equals(FeatureSet.ES_UNSTABLE);
125130
assertFS(FeatureSet.valueOf("es_unsupported")).equals(FeatureSet.ES_UNSUPPORTED);

0 commit comments

Comments
 (0)