Skip to content

Commit 275ed54

Browse files
Lekoelibarzilay
authored andcommitted
disallows exponentials with BigInts for targets lower than ES2016
1 parent 5b0194b commit 275ed54

11 files changed

+132
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28447,6 +28447,12 @@ namespace ts {
2844728447
case SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
2844828448
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
2844928449
reportOperatorError();
28450+
break;
28451+
case SyntaxKind.AsteriskAsteriskToken:
28452+
case SyntaxKind.AsteriskAsteriskEqualsToken:
28453+
if (languageVersion < ScriptTarget.ES2016) {
28454+
error(errorNode, Diagnostics.Exponentiation_cannot_be_performed_on_bigint_values_unless_the_target_option_is_set_to_es2016_or_later);
28455+
}
2845028456
}
2845128457
resultType = bigintType;
2845228458
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,10 @@
29672967
"category": "Error",
29682968
"code": 2790
29692969
},
2970+
"Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later.": {
2971+
"category": "Error",
2972+
"code": 2791
2973+
},
29702974

29712975
"Import declaration '{0}' is using private name '{1}'.": {
29722976
"category": "Error",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [bigIntWithTargetES2016.ts]
2+
BigInt(1) ** BigInt(1); // should not error
3+
4+
let num = BigInt(2);
5+
num **= BigInt(2); // should not error
6+
7+
8+
//// [bigIntWithTargetES2016.js]
9+
BigInt(1) ** BigInt(1); // should not error
10+
let num = BigInt(2);
11+
num **= BigInt(2); // should not error
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/bigIntWithTargetES2016.ts ===
2+
BigInt(1) ** BigInt(1); // should not error
3+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
4+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
5+
6+
let num = BigInt(2);
7+
>num : Symbol(num, Decl(bigIntWithTargetES2016.ts, 2, 3))
8+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
9+
10+
num **= BigInt(2); // should not error
11+
>num : Symbol(num, Decl(bigIntWithTargetES2016.ts, 2, 3))
12+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
13+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/bigIntWithTargetES2016.ts ===
2+
BigInt(1) ** BigInt(1); // should not error
3+
>BigInt(1) ** BigInt(1) : bigint
4+
>BigInt(1) : bigint
5+
>BigInt : BigIntConstructor
6+
>1 : 1
7+
>BigInt(1) : bigint
8+
>BigInt : BigIntConstructor
9+
>1 : 1
10+
11+
let num = BigInt(2);
12+
>num : bigint
13+
>BigInt(2) : bigint
14+
>BigInt : BigIntConstructor
15+
>2 : 2
16+
17+
num **= BigInt(2); // should not error
18+
>num **= BigInt(2) : bigint
19+
>num : bigint
20+
>BigInt(2) : bigint
21+
>BigInt : BigIntConstructor
22+
>2 : 2
23+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
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.
2+
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.
3+
4+
5+
==== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts (2 errors) ====
6+
BigInt(1) ** BigInt(1); // should error
7+
~~~~~~~~~~~~~~~~~~~~~~
8+
!!! error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later.
9+
10+
let foo = BigInt(2);
11+
foo **= BigInt(2); // should error
12+
~~~~~~~~~~~~~~~~~
13+
!!! error TS2782: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later.
14+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [bigIntWithTargetLessThanES2016.ts]
2+
BigInt(1) ** BigInt(1); // should error
3+
4+
let foo = BigInt(2);
5+
foo **= BigInt(2); // should error
6+
7+
8+
//// [bigIntWithTargetLessThanES2016.js]
9+
Math.pow(BigInt(1), BigInt(1)); // should error
10+
let foo = BigInt(2);
11+
foo = Math.pow(foo, BigInt(2)); // should error
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts ===
2+
BigInt(1) ** BigInt(1); // should error
3+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
4+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
5+
6+
let foo = BigInt(2);
7+
>foo : Symbol(foo, Decl(bigIntWithTargetLessThanES2016.ts, 2, 3))
8+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
9+
10+
foo **= BigInt(2); // should error
11+
>foo : Symbol(foo, Decl(bigIntWithTargetLessThanES2016.ts, 2, 3))
12+
>BigInt : Symbol(BigInt, Decl(lib.esnext.bigint.d.ts, --, --), Decl(lib.esnext.bigint.d.ts, --, --))
13+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/bigIntWithTargetLessThanES2016.ts ===
2+
BigInt(1) ** BigInt(1); // should error
3+
>BigInt(1) ** BigInt(1) : bigint
4+
>BigInt(1) : bigint
5+
>BigInt : BigIntConstructor
6+
>1 : 1
7+
>BigInt(1) : bigint
8+
>BigInt : BigIntConstructor
9+
>1 : 1
10+
11+
let foo = BigInt(2);
12+
>foo : bigint
13+
>BigInt(2) : bigint
14+
>BigInt : BigIntConstructor
15+
>2 : 2
16+
17+
foo **= BigInt(2); // should error
18+
>foo **= BigInt(2) : bigint
19+
>foo : bigint
20+
>BigInt(2) : bigint
21+
>BigInt : BigIntConstructor
22+
>2 : 2
23+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es2016
2+
// @lib: esnext
3+
4+
BigInt(1) ** BigInt(1); // should not error
5+
6+
let num = BigInt(2);
7+
num **= BigInt(2); // should not error
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es2015
2+
// @lib: esnext
3+
4+
BigInt(1) ** BigInt(1); // should error
5+
6+
let foo = BigInt(2);
7+
foo **= BigInt(2); // should error

0 commit comments

Comments
 (0)