Skip to content

Commit

Permalink
Add the ASSUME_ES2020 compiler define and use it to simplify "$jsco…
Browse files Browse the repository at this point in the history
…mp.global"

Also Update the option handler to set ASSUME_ES6 and ASSUME_ES2020 when appropriate.

PiperOrigin-RevId: 715927528
  • Loading branch information
concavelenz authored and copybara-github committed Jan 15, 2025
1 parent 64d9caa commit 4d56b62
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/com/google/javascript/jscomp/CompilerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ void setDependentValuesFromYear(CompilerOptions options) {
options.languageOutIsDefaultStrict = Optional.of(true);
options.setDefineToNumberLiteral("goog.FEATURESET_YEAR", year);
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES5", year > 2012);
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES6", year > 2018);
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES6", year >= 2018);
options.setDefineToBooleanLiteral("$jscomp.ASSUME_ES2020", year >= 2021);
}

FeatureSet getFeatureSet() {
Expand Down
1 change: 1 addition & 0 deletions src/com/google/javascript/jscomp/ProcessDefines.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ProcessDefines implements CompilerPass {
"goog.DEBUG",
"$jscomp.ASSUME_ES5",
"$jscomp.ASSUME_ES6",
"$jscomp.ASSUME_ES2020",
"$jscomp.ISOLATE_POLYFILLS",
"$jscomp.INSTRUMENT_ASYNC_CONTEXT");

Expand Down
9 changes: 9 additions & 0 deletions src/com/google/javascript/jscomp/js/util/defines.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ $jscomp.ASSUME_ES5 = false;
*/
$jscomp.ASSUME_ES6 = false;

/**
* Whether to assume ES2020 is available. This enables removing several
* internal polyfills, which must otherwise be detected at runtime.
* @define {boolean}
*/
$jscomp.ASSUME_ES2020 = false;



/**
* Whether to skip the conformance check and simply use the polyfill always.
* @define {boolean}
Expand Down
3 changes: 2 additions & 1 deletion src/com/google/javascript/jscomp/js/util/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @suppress {uselessCode}
*/
'require base';
'require util/defines';

/**
* Locate and return a reference to the global object.
Expand Down Expand Up @@ -85,4 +86,4 @@ $jscomp.getGlobal = function(passedInThis) {
* The global object.
* @const {!Global}
*/
$jscomp.global = $jscomp.getGlobal(this);
$jscomp.global = $jscomp.ASSUME_ES2020 ? globalThis : $jscomp.getGlobal(this);
10 changes: 8 additions & 2 deletions test/com/google/javascript/jscomp/CompilerOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ public void testBrowserFeaturesetYearOptionSetsAssumeES5() {
@Test
public void testBrowserFeaturesetYearOptionSetsAssumeES6() {
CompilerOptions options = new CompilerOptions();
options.setBrowserFeaturesetYear(2018);
options.setBrowserFeaturesetYear(2012);
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES6").getToken())
.isEqualTo(Token.FALSE);
options.setBrowserFeaturesetYear(2019);
options.setBrowserFeaturesetYear(2018);
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES6").getToken())
.isEqualTo(Token.TRUE);
options.setBrowserFeaturesetYear(2020);
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES2020").getToken())
.isEqualTo(Token.FALSE);
options.setBrowserFeaturesetYear(2021);
assertThat(options.getDefineReplacements().get("$jscomp.ASSUME_ES2020").getToken())
.isEqualTo(Token.TRUE);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public void testInjection_doesntCrashOnLibrariesWithCasts_ifTypecheckingHasNotRu

ImmutableList<Node> objectNameNodes =
findNodesNamed(this.getLastCompiler().getRoot(), "globalThis");
assertThat(objectNameNodes).hasSize(3);
assertThat(objectNameNodes).hasSize(4);

Node injectedGlobalThisNode = objectNameNodes.get(0);
Node sourceGlobalThisNode = objectNameNodes.get(2);
Node sourceGlobalThisNode = objectNameNodes.get(3);

assertThat(injectedGlobalThisNode.getSourceFileName()).contains("util/global");
assertThat(sourceGlobalThisNode.getSourceFileName()).contains("testcode");
Expand Down

0 comments on commit 4d56b62

Please sign in to comment.