Skip to content

Commit 4d56b62

Browse files
concavelenzcopybara-github
authored andcommitted
Add the ASSUME_ES2020 compiler define and use it to simplify "$jscomp.global"
Also Update the option handler to set ASSUME_ES6 and ASSUME_ES2020 when appropriate. PiperOrigin-RevId: 715927528
1 parent 64d9caa commit 4d56b62

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ void setDependentValuesFromYear(CompilerOptions options) {
185185
options.languageOutIsDefaultStrict = Optional.of(true);
186186
options.setDefineToNumberLiteral("goog.FEATURESET_YEAR", year);
187187
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES5", year > 2012);
188-
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES6", year > 2018);
188+
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES6", year >= 2018);
189+
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES2020", year >= 2021);
189190
}
190191

191192
FeatureSet getFeatureSet() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ProcessDefines implements CompilerPass {
6060
"goog.DEBUG",
6161
"$jscomp.ASSUME_ES5",
6262
"$jscomp.ASSUME_ES6",
63+
"$jscomp.ASSUME_ES2020",
6364
"$jscomp.ISOLATE_POLYFILLS",
6465
"$jscomp.INSTRUMENT_ASYNC_CONTEXT");
6566

src/com/google/javascript/jscomp/js/util/defines.js

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ $jscomp.ASSUME_ES5 = false;
3131
*/
3232
$jscomp.ASSUME_ES6 = false;
3333

34+
/**
35+
* Whether to assume ES2020 is available. This enables removing several
36+
* internal polyfills, which must otherwise be detected at runtime.
37+
* @define {boolean}
38+
*/
39+
$jscomp.ASSUME_ES2020 = false;
40+
41+
42+
3443
/**
3544
* Whether to skip the conformance check and simply use the polyfill always.
3645
* @define {boolean}

src/com/google/javascript/jscomp/js/util/global.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @suppress {uselessCode}
2020
*/
2121
'require base';
22+
'require util/defines';
2223

2324
/**
2425
* Locate and return a reference to the global object.
@@ -85,4 +86,4 @@ $jscomp.getGlobal = function(passedInThis) {
8586
* The global object.
8687
* @const {!Global}
8788
*/
88-
$jscomp.global = $jscomp.getGlobal(this);
89+
$jscomp.global = $jscomp.ASSUME_ES2020 ? globalThis : $jscomp.getGlobal(this);

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ public void testBrowserFeaturesetYearOptionSetsAssumeES5() {
8181
@Test
8282
public void testBrowserFeaturesetYearOptionSetsAssumeES6() {
8383
CompilerOptions options = new CompilerOptions();
84-
options.setBrowserFeaturesetYear(2018);
84+
options.setBrowserFeaturesetYear(2012);
8585
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES6").getToken())
8686
.isEqualTo(Token.FALSE);
87-
options.setBrowserFeaturesetYear(2019);
87+
options.setBrowserFeaturesetYear(2018);
8888
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES6").getToken())
8989
.isEqualTo(Token.TRUE);
90+
options.setBrowserFeaturesetYear(2020);
91+
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES2020").getToken())
92+
.isEqualTo(Token.FALSE);
93+
options.setBrowserFeaturesetYear(2021);
94+
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES2020").getToken())
95+
.isEqualTo(Token.TRUE);
9096
}
9197

9298
@Test

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ public void testInjection_doesntCrashOnLibrariesWithCasts_ifTypecheckingHasNotRu
113113

114114
ImmutableList<Node> objectNameNodes =
115115
findNodesNamed(this.getLastCompiler().getRoot(), "globalThis");
116-
assertThat(objectNameNodes).hasSize(3);
116+
assertThat(objectNameNodes).hasSize(4);
117117

118118
Node injectedGlobalThisNode = objectNameNodes.get(0);
119-
Node sourceGlobalThisNode = objectNameNodes.get(2);
119+
Node sourceGlobalThisNode = objectNameNodes.get(3);
120120

121121
assertThat(injectedGlobalThisNode.getSourceFileName()).contains("util/global");
122122
assertThat(sourceGlobalThisNode.getSourceFileName()).contains("testcode");

0 commit comments

Comments
 (0)