From bb99c4123003ade3e471cf17472be6d3f72d96ff Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Mon, 5 Nov 2018 12:23:02 -0800 Subject: [PATCH] Don't error on bigint literal used in type --- src/compiler/checker.ts | 16 ++++++++++------ .../warnExperimentalBigIntLiteral.errors.txt | 16 +++++++--------- .../reference/warnExperimentalBigIntLiteral.js | 6 ++++-- .../warnExperimentalBigIntLiteral.symbols | 9 ++++++--- .../warnExperimentalBigIntLiteral.types | 7 ++++++- .../compiler/warnExperimentalBigIntLiteral.ts | 3 ++- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 308f3a61b2c..08f5c3925fd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -30471,13 +30471,17 @@ namespace ts { } function checkGrammarBigIntLiteral(node: BigIntLiteral): boolean { - if (languageVersion < ScriptTarget.ESNext) { - if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targetting_lower_than_ESNext)) { - return true; + const literalType = isLiteralTypeNode(node.parent) || + isPrefixUnaryExpression(node.parent) && isLiteralTypeNode(node.parent.parent); + if (!literalType) { + if (languageVersion < ScriptTarget.ESNext) { + if (grammarErrorOnNode(node, Diagnostics.BigInt_literals_are_not_available_when_targetting_lower_than_ESNext)) { + return true; + } + } + if (!compilerOptions.experimentalBigInt) { + return grammarErrorOnNode(node, Diagnostics.Experimental_support_for_BigInt_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalBigInt_option_to_remove_this_warning); } - } - if (!compilerOptions.experimentalBigInt) { - return grammarErrorOnNode(node, Diagnostics.Experimental_support_for_BigInt_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalBigInt_option_to_remove_this_warning); } return false; } diff --git a/tests/baselines/reference/warnExperimentalBigIntLiteral.errors.txt b/tests/baselines/reference/warnExperimentalBigIntLiteral.errors.txt index 6df7ea6e0a2..3e8e50d148c 100644 --- a/tests/baselines/reference/warnExperimentalBigIntLiteral.errors.txt +++ b/tests/baselines/reference/warnExperimentalBigIntLiteral.errors.txt @@ -1,16 +1,14 @@ -tests/cases/compiler/warnExperimentalBigIntLiteral.ts(3,24): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. -tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,22): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. -tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,29): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. -tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,39): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. -tests/cases/compiler/warnExperimentalBigIntLiteral.ts(4,48): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. +tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,22): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. +tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,29): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. +tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,39): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. +tests/cases/compiler/warnExperimentalBigIntLiteral.ts(5,48): error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. -==== tests/cases/compiler/warnExperimentalBigIntLiteral.ts (5 errors) ==== +==== tests/cases/compiler/warnExperimentalBigIntLiteral.ts (4 errors) ==== const normalNumber = 123; // should not error let bigintType: bigint; // should not error - let bigintLiteralType: 123n; // should error when used as type - ~~~~ -!!! error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. + let bigintLiteralType: 123n; // should not error when used as type + let bigintNegativeLiteralType: -123n; // should not error when used as type const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error ~~~~ !!! error TS1351: Experimental support for BigInt is a feature that is subject to change in a future release. Set the 'experimentalBigInt' option to remove this warning. diff --git a/tests/baselines/reference/warnExperimentalBigIntLiteral.js b/tests/baselines/reference/warnExperimentalBigIntLiteral.js index 8516350ddbf..2a1ee53b2c0 100644 --- a/tests/baselines/reference/warnExperimentalBigIntLiteral.js +++ b/tests/baselines/reference/warnExperimentalBigIntLiteral.js @@ -1,11 +1,13 @@ //// [warnExperimentalBigIntLiteral.ts] const normalNumber = 123; // should not error let bigintType: bigint; // should not error -let bigintLiteralType: 123n; // should error when used as type +let bigintLiteralType: 123n; // should not error when used as type +let bigintNegativeLiteralType: -123n; // should not error when used as type const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error //// [warnExperimentalBigIntLiteral.js] const normalNumber = 123; // should not error let bigintType; // should not error -let bigintLiteralType; // should error when used as type +let bigintLiteralType; // should not error when used as type +let bigintNegativeLiteralType; // should not error when used as type const bigintNumber = 123n * 15n + 292n * 0x7fn; // each literal should error diff --git a/tests/baselines/reference/warnExperimentalBigIntLiteral.symbols b/tests/baselines/reference/warnExperimentalBigIntLiteral.symbols index 782e7edcac6..1ea1c380601 100644 --- a/tests/baselines/reference/warnExperimentalBigIntLiteral.symbols +++ b/tests/baselines/reference/warnExperimentalBigIntLiteral.symbols @@ -5,9 +5,12 @@ const normalNumber = 123; // should not error let bigintType: bigint; // should not error >bigintType : Symbol(bigintType, Decl(warnExperimentalBigIntLiteral.ts, 1, 3)) -let bigintLiteralType: 123n; // should error when used as type +let bigintLiteralType: 123n; // should not error when used as type >bigintLiteralType : Symbol(bigintLiteralType, Decl(warnExperimentalBigIntLiteral.ts, 2, 3)) -const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error ->bigintNumber : Symbol(bigintNumber, Decl(warnExperimentalBigIntLiteral.ts, 3, 5)) +let bigintNegativeLiteralType: -123n; // should not error when used as type +>bigintNegativeLiteralType : Symbol(bigintNegativeLiteralType, Decl(warnExperimentalBigIntLiteral.ts, 3, 3)) + +const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error +>bigintNumber : Symbol(bigintNumber, Decl(warnExperimentalBigIntLiteral.ts, 4, 5)) diff --git a/tests/baselines/reference/warnExperimentalBigIntLiteral.types b/tests/baselines/reference/warnExperimentalBigIntLiteral.types index a3f01b31294..a0ba0a9564b 100644 --- a/tests/baselines/reference/warnExperimentalBigIntLiteral.types +++ b/tests/baselines/reference/warnExperimentalBigIntLiteral.types @@ -6,9 +6,14 @@ const normalNumber = 123; // should not error let bigintType: bigint; // should not error >bigintType : bigint -let bigintLiteralType: 123n; // should error when used as type +let bigintLiteralType: 123n; // should not error when used as type >bigintLiteralType : 123n +let bigintNegativeLiteralType: -123n; // should not error when used as type +>bigintNegativeLiteralType : -123n +>-123n : -123n +>123n : 123n + const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error >bigintNumber : bigint >123n * 0b1111n + 0o444n * 0x7fn : bigint diff --git a/tests/cases/compiler/warnExperimentalBigIntLiteral.ts b/tests/cases/compiler/warnExperimentalBigIntLiteral.ts index c9f94fcc992..c59bff0696e 100644 --- a/tests/cases/compiler/warnExperimentalBigIntLiteral.ts +++ b/tests/cases/compiler/warnExperimentalBigIntLiteral.ts @@ -2,5 +2,6 @@ const normalNumber = 123; // should not error let bigintType: bigint; // should not error -let bigintLiteralType: 123n; // should error when used as type +let bigintLiteralType: 123n; // should not error when used as type +let bigintNegativeLiteralType: -123n; // should not error when used as type const bigintNumber = 123n * 0b1111n + 0o444n * 0x7fn; // each literal should error \ No newline at end of file