Skip to content

Commit 9024289

Browse files
JohnLuck77copybara-github
authored andcommitted
Add BigInt support into language_in/out=ES_2020
PiperOrigin-RevId: 325250553
1 parent cd5a7a0 commit 9024289

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private static class ReportErrorForBigIntLiterals extends AbstractPostOrderCallb
3939
@Override
4040
public void visit(NodeTraversal t, Node n, Node parent) {
4141
if (n.isBigInt()) {
42-
// Transpilation of BigInt is not yet supported
42+
// Transpilation of BigInt is not supported
4343
t.report(n, BIGINT_TRANSPILATION);
4444
}
4545
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES8;
2424
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES_NEXT;
2525
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES_NEXT_IN;
26-
import static com.google.javascript.jscomp.parsing.parser.FeatureSet.ES_UNSUPPORTED;
2726

2827
import com.google.javascript.jscomp.Es6RewriteDestructuring.ObjectDestructuringRewriteMode;
2928
import com.google.javascript.jscomp.NodeTraversal.Callback;
@@ -78,10 +77,6 @@ public static void addPostCheckTranspilationPasses(
7877
List<PassFactory> passes, CompilerOptions options) {
7978
// Note that, for features >ES8 we detect feature by feature rather than by yearly languages
8079
// in order to handle FeatureSet.BROWSER_2020, which is ES2019 without the new RegExp features.
81-
if (options.needsTranspilationOf(Feature.BIGINT)) {
82-
passes.add(reportBigIntLiteralTranspilationUnsupported);
83-
}
84-
8580
if (options.needsTranspilationOf(Feature.NUMERIC_SEPARATOR)) {
8681
// Numeric separators are flagged as present by the parser,
8782
// but never actually represented in the AST.
@@ -94,6 +89,10 @@ public static void addPostCheckTranspilationPasses(
9489
passes.add(rewriteOptionalChainingOperator);
9590
}
9691

92+
if (options.needsTranspilationOf(Feature.BIGINT)) {
93+
passes.add(reportBigIntLiteralTranspilationUnsupported);
94+
}
95+
9796
if (options.needsTranspilationOf(Feature.NULL_COALESCE_OP)) {
9897
passes.add(rewriteNullishCoalesceOperator);
9998
}
@@ -242,9 +241,9 @@ public static void addRewritePolyfillPass(List<PassFactory> passes) {
242241

243242
private static final PassFactory reportBigIntLiteralTranspilationUnsupported =
244243
PassFactory.builderForHotSwap()
245-
.setName("reportBigIntTranspilation")
244+
.setName("reportBigIntTranspilationUnsupported")
246245
.setInternalFactory(ReportBigIntLiteralTranspilationUnsupported::new)
247-
.setFeatureSet(ES_UNSUPPORTED)
246+
.setFeatureSet(ES2020)
248247
.build();
249248

250249
private static final PassFactory rewriteExponentialOperator =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public enum Feature {
203203
DYNAMIC_IMPORT("Dynamic module import", LangVersion.ES_UNSUPPORTED),
204204

205205
// ES 2020 Stage 4
206-
BIGINT("bigint", LangVersion.ES_UNSUPPORTED),
206+
BIGINT("bigint", LangVersion.ES2020),
207207
IMPORT_META("import.meta", LangVersion.ES2020),
208208
NULL_COALESCE_OP("Nullish coalescing", LangVersion.ES2020),
209209
// Optional chaining is part of the ES_2020 spec, but until we can support it in output code,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected CompilerPass getProcessor(Compiler compiler) {
4141
@Override
4242
protected CompilerOptions getOptions() {
4343
CompilerOptions options = super.getOptions();
44-
options.setLanguageIn(LanguageMode.UNSUPPORTED);
44+
options.setLanguageIn(LanguageMode.ECMASCRIPT_2020);
4545
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
4646
return options;
4747
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,42 @@ public void testBigInt() {
191191
// Confirm that the native BigInt type has been implemented, so the BigInt externs don't cause
192192
// errors.
193193
externs = ImmutableList.of(new TestExternsBuilder().addBigInt().buildExternsFile("externs.js"));
194-
test(options, "BigInt(1)", "BigInt(1)");
194+
testSame(options, "BigInt(1)");
195+
}
196+
197+
@Test
198+
public void testFunctionThatAcceptsAndReturnsBigInt() {
199+
CompilerOptions options = createCompilerOptions();
200+
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
201+
options.setLanguage(LanguageMode.ECMASCRIPT_NEXT);
202+
externs =
203+
ImmutableList.of(
204+
new TestExternsBuilder().addBigInt().addConsole().buildExternsFile("externs.js"));
205+
test(
206+
options,
207+
lines(
208+
"/**",
209+
" * @param {bigint} value",
210+
" * @return {bigint}",
211+
" */",
212+
"function bigintTimes3(value){",
213+
" return value * 3n;",
214+
"}",
215+
"console.log(bigintTimes3(5n));"),
216+
// TODO(b/140132715): This test should expect console.log(15n), must update
217+
// PeepholeFoldConstants for this to work as intended
218+
"console.log(5n * 3n);");
219+
}
220+
221+
@Test
222+
public void testBigIntStringConcatenation() {
223+
CompilerOptions options = createCompilerOptions();
224+
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
225+
options.setLanguage(LanguageMode.ECMASCRIPT_NEXT);
226+
externs = ImmutableList.of(new TestExternsBuilder().addBigInt().buildExternsFile("externs.js"));
227+
// TODO(b/140132715): This test should expect "1nasdf", must update PeepholeFoldConstants
228+
// for this to work as intended
229+
test(options, "1n + 'asdf'", "1n + 'asdf'");
195230
}
196231

197232
@Test

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,7 +3901,6 @@ public void testExponentialLiterals() {
39013901

39023902
@Test
39033903
public void testBigIntLiteralZero() {
3904-
mode = LanguageMode.UNSUPPORTED;
39053904
Node bigint =
39063905
parse("0n;") // SCRIPT
39073906
.getOnlyChild() // EXPR_RESULT
@@ -3915,7 +3914,6 @@ public void testBigIntLiteralZero() {
39153914

39163915
@Test
39173916
public void testBigIntLiteralPositive() {
3918-
mode = LanguageMode.UNSUPPORTED;
39193917
Node bigint =
39203918
parse("1n;") // SCRIPT
39213919
.getOnlyChild() // EXPR_RESULT
@@ -3929,7 +3927,6 @@ public void testBigIntLiteralPositive() {
39293927

39303928
@Test
39313929
public void testBigIntLiteralNegative() {
3932-
mode = LanguageMode.UNSUPPORTED;
39333930
Node bigint =
39343931
parse("-1n;") // SCRIPT
39353932
.getOnlyChild() // EXPR_RESULT
@@ -3943,7 +3940,6 @@ public void testBigIntLiteralNegative() {
39433940

39443941
@Test
39453942
public void testBigIntLiteralBinary() {
3946-
mode = LanguageMode.UNSUPPORTED;
39473943
Node bigint =
39483944
parse("0b10000n;") // SCRIPT
39493945
.getOnlyChild() // EXPR_RESULT
@@ -3957,7 +3953,6 @@ public void testBigIntLiteralBinary() {
39573953

39583954
@Test
39593955
public void testBigIntLiteralOctal() {
3960-
mode = LanguageMode.UNSUPPORTED;
39613956
Node bigint =
39623957
parse("0o100n;") // SCRIPT
39633958
.getOnlyChild() // EXPR_RESULT
@@ -3971,7 +3966,6 @@ public void testBigIntLiteralOctal() {
39713966

39723967
@Test
39733968
public void testBigIntLiteralHex() {
3974-
mode = LanguageMode.UNSUPPORTED;
39753969
Node bigint =
39763970
parse("0xFn;") // SCRIPT
39773971
.getOnlyChild() // EXPR_RESULT
@@ -3985,7 +3979,6 @@ public void testBigIntLiteralHex() {
39853979

39863980
@Test
39873981
public void testBigIntLiteralErrors() {
3988-
mode = LanguageMode.UNSUPPORTED;
39893982
parseError("01n;", "SyntaxError: nonzero BigInt can't have leading zero");
39903983
parseError(".1n", "Semi-colon expected");
39913984
parseError("0.1n", "Semi-colon expected");
@@ -3994,12 +3987,14 @@ public void testBigIntLiteralErrors() {
39943987

39953988
@Test
39963989
public void testBigIntLiteralWarning() {
3997-
parseWarning("1n;", "This language feature is not currently supported by the compiler: bigint");
3990+
mode = LanguageMode.ECMASCRIPT_2019;
3991+
parseWarning(
3992+
"1n;",
3993+
"This language feature is only supported for ECMASCRIPT_2020 mode or better: bigint");
39983994
}
39993995

40003996
@Test
40013997
public void testBigIntLiteralInCall() {
4002-
mode = LanguageMode.UNSUPPORTED;
40033998
parse("alert(1n)");
40043999
}
40054000

0 commit comments

Comments
 (0)