From 275ed548dfc71569773ac0e209014c5b1cb60c3f Mon Sep 17 00:00:00 2001 From: Leko Date: Fri, 15 Nov 2019 23:14:25 +0900 Subject: [PATCH] disallows exponentials with BigInts for targets lower than ES2016 --- src/compiler/checker.ts | 6 +++++ src/compiler/diagnosticMessages.json | 4 ++++ .../reference/bigIntWithTargetES2016.js | 11 +++++++++ .../reference/bigIntWithTargetES2016.symbols | 13 +++++++++++ .../reference/bigIntWithTargetES2016.types | 23 +++++++++++++++++++ .../bigIntWithTargetLessThanES2016.errors.txt | 14 +++++++++++ .../bigIntWithTargetLessThanES2016.js | 11 +++++++++ .../bigIntWithTargetLessThanES2016.symbols | 13 +++++++++++ .../bigIntWithTargetLessThanES2016.types | 23 +++++++++++++++++++ .../cases/compiler/bigIntWithTargetES2016.ts | 7 ++++++ .../bigIntWithTargetLessThanES2016.ts | 7 ++++++ 11 files changed, 132 insertions(+) create mode 100644 tests/baselines/reference/bigIntWithTargetES2016.js create mode 100644 tests/baselines/reference/bigIntWithTargetES2016.symbols create mode 100644 tests/baselines/reference/bigIntWithTargetES2016.types create mode 100644 tests/baselines/reference/bigIntWithTargetLessThanES2016.errors.txt create mode 100644 tests/baselines/reference/bigIntWithTargetLessThanES2016.js create mode 100644 tests/baselines/reference/bigIntWithTargetLessThanES2016.symbols create mode 100644 tests/baselines/reference/bigIntWithTargetLessThanES2016.types create mode 100644 tests/cases/compiler/bigIntWithTargetES2016.ts create mode 100644 tests/cases/compiler/bigIntWithTargetLessThanES2016.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c788c6807a9..a3371e26fc0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28447,6 +28447,12 @@ namespace ts { case SyntaxKind.GreaterThanGreaterThanGreaterThanToken: case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: reportOperatorError(); + break; + case SyntaxKind.AsteriskAsteriskToken: + case SyntaxKind.AsteriskAsteriskEqualsToken: + if (languageVersion < ScriptTarget.ES2016) { + error(errorNode, Diagnostics.Exponentiation_cannot_be_performed_on_bigint_values_unless_the_target_option_is_set_to_es2016_or_later); + } } resultType = bigintType; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 03f42608454..60a8d44b48f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2967,6 +2967,10 @@ "category": "Error", "code": 2790 }, + "Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later.": { + "category": "Error", + "code": 2791 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/bigIntWithTargetES2016.js b/tests/baselines/reference/bigIntWithTargetES2016.js new file mode 100644 index 00000000000..81ffd1058df --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetES2016.js @@ -0,0 +1,11 @@ +//// [bigIntWithTargetES2016.ts] +BigInt(1) ** BigInt(1); // should not error + +let num = BigInt(2); +num **= BigInt(2); // should not error + + +//// [bigIntWithTargetES2016.js] +BigInt(1) ** BigInt(1); // should not error +let num = BigInt(2); +num **= BigInt(2); // should not error diff --git a/tests/baselines/reference/bigIntWithTargetES2016.symbols b/tests/baselines/reference/bigIntWithTargetES2016.symbols new file mode 100644 index 00000000000..0f42e9612f5 --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetES2016.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/bigIntWithTargetES2016.ts === +BigInt(1) ** BigInt(1); // should not error +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + +let num = BigInt(2); +>num : Symbol(num, Decl(bigIntWithTargetES2016.ts, 2, 3)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + +num **= BigInt(2); // should not error +>num : Symbol(num, Decl(bigIntWithTargetES2016.ts, 2, 3)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + diff --git a/tests/baselines/reference/bigIntWithTargetES2016.types b/tests/baselines/reference/bigIntWithTargetES2016.types new file mode 100644 index 00000000000..eba94dbe3a9 --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetES2016.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/bigIntWithTargetES2016.ts === +BigInt(1) ** BigInt(1); // should not error +>BigInt(1) ** BigInt(1) : bigint +>BigInt(1) : bigint +>BigInt : BigIntConstructor +>1 : 1 +>BigInt(1) : bigint +>BigInt : BigIntConstructor +>1 : 1 + +let num = BigInt(2); +>num : bigint +>BigInt(2) : bigint +>BigInt : BigIntConstructor +>2 : 2 + +num **= BigInt(2); // should not error +>num **= BigInt(2) : bigint +>num : bigint +>BigInt(2) : bigint +>BigInt : BigIntConstructor +>2 : 2 + diff --git a/tests/baselines/reference/bigIntWithTargetLessThanES2016.errors.txt b/tests/baselines/reference/bigIntWithTargetLessThanES2016.errors.txt new file mode 100644 index 00000000000..b014370c015 --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetLessThanES2016.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/bigIntWithTargetLessThanES2016.ts(1,1): error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. +tests/cases/compiler/bigIntWithTargetLessThanES2016.ts(4,1): error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. + + +==== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts (2 errors) ==== + BigInt(1) ** BigInt(1); // should error + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. + + let foo = BigInt(2); + foo **= BigInt(2); // should error + ~~~~~~~~~~~~~~~~~ +!!! error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. + \ No newline at end of file diff --git a/tests/baselines/reference/bigIntWithTargetLessThanES2016.js b/tests/baselines/reference/bigIntWithTargetLessThanES2016.js new file mode 100644 index 00000000000..6f875d2d387 --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetLessThanES2016.js @@ -0,0 +1,11 @@ +//// [bigIntWithTargetLessThanES2016.ts] +BigInt(1) ** BigInt(1); // should error + +let foo = BigInt(2); +foo **= BigInt(2); // should error + + +//// [bigIntWithTargetLessThanES2016.js] +Math.pow(BigInt(1), BigInt(1)); // should error +let foo = BigInt(2); +foo = Math.pow(foo, BigInt(2)); // should error diff --git a/tests/baselines/reference/bigIntWithTargetLessThanES2016.symbols b/tests/baselines/reference/bigIntWithTargetLessThanES2016.symbols new file mode 100644 index 00000000000..2285bf9a67e --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetLessThanES2016.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts === +BigInt(1) ** BigInt(1); // should error +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + +let foo = BigInt(2); +>foo : Symbol(foo, Decl(bigIntWithTargetLessThanES2016.ts, 2, 3)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + +foo **= BigInt(2); // should error +>foo : Symbol(foo, Decl(bigIntWithTargetLessThanES2016.ts, 2, 3)) +>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --)) + diff --git a/tests/baselines/reference/bigIntWithTargetLessThanES2016.types b/tests/baselines/reference/bigIntWithTargetLessThanES2016.types new file mode 100644 index 00000000000..9f81fea6220 --- /dev/null +++ b/tests/baselines/reference/bigIntWithTargetLessThanES2016.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts === +BigInt(1) ** BigInt(1); // should error +>BigInt(1) ** BigInt(1) : bigint +>BigInt(1) : bigint +>BigInt : BigIntConstructor +>1 : 1 +>BigInt(1) : bigint +>BigInt : BigIntConstructor +>1 : 1 + +let foo = BigInt(2); +>foo : bigint +>BigInt(2) : bigint +>BigInt : BigIntConstructor +>2 : 2 + +foo **= BigInt(2); // should error +>foo **= BigInt(2) : bigint +>foo : bigint +>BigInt(2) : bigint +>BigInt : BigIntConstructor +>2 : 2 + diff --git a/tests/cases/compiler/bigIntWithTargetES2016.ts b/tests/cases/compiler/bigIntWithTargetES2016.ts new file mode 100644 index 00000000000..329605bf3c4 --- /dev/null +++ b/tests/cases/compiler/bigIntWithTargetES2016.ts @@ -0,0 +1,7 @@ +// @target: es2016 +// @lib: esnext + +BigInt(1) ** BigInt(1); // should not error + +let num = BigInt(2); +num **= BigInt(2); // should not error diff --git a/tests/cases/compiler/bigIntWithTargetLessThanES2016.ts b/tests/cases/compiler/bigIntWithTargetLessThanES2016.ts new file mode 100644 index 00000000000..4ad684629fd --- /dev/null +++ b/tests/cases/compiler/bigIntWithTargetLessThanES2016.ts @@ -0,0 +1,7 @@ +// @target: es2015 +// @lib: esnext + +BigInt(1) ** BigInt(1); // should error + +let foo = BigInt(2); +foo **= BigInt(2); // should error