Skip to content

Commit 6c59a3b

Browse files
committed
Fix ambient initializer emit for bigint
1 parent ecd1245 commit 6c59a3b

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30128,6 +30128,12 @@ namespace ts {
3012830128
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.NumericLiteral;
3012930129
}
3013030130

30131+
function isBigIntLiteralExpression(expr: Expression) {
30132+
return expr.kind === SyntaxKind.BigIntLiteral ||
30133+
expr.kind === SyntaxKind.PrefixUnaryExpression && (<PrefixUnaryExpression>expr).operator === SyntaxKind.MinusToken &&
30134+
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.BigIntLiteral;
30135+
}
30136+
3013130137
function isSimpleLiteralEnumReference(expr: Expression) {
3013230138
if (
3013330139
(isPropertyAccessExpression(expr) || (isElementAccessExpression(expr) && isStringOrNumberLiteralExpression(expr.argumentExpression))) &&
@@ -30136,19 +30142,25 @@ namespace ts {
3013630142
}
3013730143

3013830144
function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) {
30139-
if (node.initializer) {
30140-
const isInvalidInitializer = !(isStringOrNumberLiteralExpression(node.initializer) || isSimpleLiteralEnumReference(node.initializer) || node.initializer.kind === SyntaxKind.TrueKeyword || node.initializer.kind === SyntaxKind.FalseKeyword);
30145+
const {initializer} = node;
30146+
if (initializer) {
30147+
const isInvalidInitializer = !(
30148+
isStringOrNumberLiteralExpression(initializer) ||
30149+
isSimpleLiteralEnumReference(initializer) ||
30150+
initializer.kind === SyntaxKind.TrueKeyword || initializer.kind === SyntaxKind.FalseKeyword ||
30151+
isBigIntLiteralExpression(initializer)
30152+
);
3014130153
const isConstOrReadonly = isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node);
3014230154
if (isConstOrReadonly && !node.type) {
3014330155
if (isInvalidInitializer) {
30144-
return grammarErrorOnNode(node.initializer!, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
30156+
return grammarErrorOnNode(initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
3014530157
}
3014630158
}
3014730159
else {
30148-
return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
30160+
return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
3014930161
}
3015030162
if (!isConstOrReadonly || isInvalidInitializer) {
30151-
return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
30163+
return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
3015230164
}
3015330165
}
3015430166
}

tests/baselines/reference/bigintWithLib.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
7474
bigintVal = dataView.getBigInt64(1);
7575
bigintVal = dataView.getBigInt64(1, true);
7676
bigintVal = dataView.getBigUint64(2);
77-
bigintVal = dataView.getBigUint64(2, true);
77+
bigintVal = dataView.getBigUint64(2, true);
78+
79+
// Test emitted declarations files
80+
const w = 12n; // should emit as const w = 12n
81+
const x = -12n; // should emit as const x = -12n
82+
const y: 12n = 12n; // should emit type 12n
83+
let z = 12n; // should emit type bigint in declaration file

tests/baselines/reference/bigintWithLib.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ dataView.setBigUint64(2, 123); // should error
4545
bigintVal = dataView.getBigInt64(1);
4646
bigintVal = dataView.getBigInt64(1, true);
4747
bigintVal = dataView.getBigUint64(2);
48-
bigintVal = dataView.getBigUint64(2, true);
48+
bigintVal = dataView.getBigUint64(2, true);
49+
50+
// Test emitted declarations files
51+
const w = 12n; // should emit as const w = 12n
52+
const x = -12n; // should emit as const x = -12n
53+
const y: 12n = 12n; // should emit type 12n
54+
let z = 12n; // should emit type bigint in declaration file
4955

5056
//// [bigintWithLib.js]
5157
// Test BigInt functions
@@ -92,3 +98,22 @@ bigintVal = dataView.getBigInt64(1);
9298
bigintVal = dataView.getBigInt64(1, true);
9399
bigintVal = dataView.getBigUint64(2);
94100
bigintVal = dataView.getBigUint64(2, true);
101+
// Test emitted declarations files
102+
const w = 12n; // should emit as const w = 12n
103+
const x = -12n; // should emit as const x = -12n
104+
const y = 12n; // should emit type 12n
105+
let z = 12n; // should emit type bigint in declaration file
106+
107+
108+
//// [bigintWithLib.d.ts]
109+
declare let bigintVal: bigint;
110+
declare let stringVal: string;
111+
declare let bigIntArray: BigInt64Array;
112+
declare let len: number;
113+
declare let arrayBufferLike: ArrayBufferView;
114+
declare let bigUintArray: BigUint64Array;
115+
declare const dataView: DataView;
116+
declare const w = 12n;
117+
declare const x = -12n;
118+
declare const y: 12n;
119+
declare let z: bigint;

tests/baselines/reference/bigintWithLib.symbols

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,16 @@ bigintVal = dataView.getBigUint64(2, true);
204204
>dataView : Symbol(dataView, Decl(bigintWithLib.ts, 36, 5))
205205
>getBigUint64 : Symbol(DataView.getBigUint64, Decl(lib.esnext.bigint.d.ts, --, --))
206206

207+
// Test emitted declarations files
208+
const w = 12n; // should emit as const w = 12n
209+
>w : Symbol(w, Decl(bigintWithLib.ts, 49, 5))
210+
211+
const x = -12n; // should emit as const x = -12n
212+
>x : Symbol(x, Decl(bigintWithLib.ts, 50, 5))
213+
214+
const y: 12n = 12n; // should emit type 12n
215+
>y : Symbol(y, Decl(bigintWithLib.ts, 51, 5))
216+
217+
let z = 12n; // should emit type bigint in declaration file
218+
>z : Symbol(z, Decl(bigintWithLib.ts, 52, 3))
219+

tests/baselines/reference/bigintWithLib.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,21 @@ bigintVal = dataView.getBigUint64(2, true);
332332
>2 : 2
333333
>true : true
334334

335+
// Test emitted declarations files
336+
const w = 12n; // should emit as const w = 12n
337+
>w : 12n
338+
>12n : 12n
339+
340+
const x = -12n; // should emit as const x = -12n
341+
>x : -12n
342+
>-12n : -12n
343+
>12n : 12n
344+
345+
const y: 12n = 12n; // should emit type 12n
346+
>y : 12n
347+
>12n : 12n
348+
349+
let z = 12n; // should emit type bigint in declaration file
350+
>z : bigint
351+
>12n : 12n
352+

tests/cases/compiler/bigintWithLib.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @experimentalBigInt: true
22
// @target: esnext
3+
// @declaration: true
34

45
// Test BigInt functions
56
let bigintVal: bigint = BigInt(123);
@@ -47,4 +48,10 @@ dataView.setBigUint64(2, 123); // should error
4748
bigintVal = dataView.getBigInt64(1);
4849
bigintVal = dataView.getBigInt64(1, true);
4950
bigintVal = dataView.getBigUint64(2);
50-
bigintVal = dataView.getBigUint64(2, true);
51+
bigintVal = dataView.getBigUint64(2, true);
52+
53+
// Test emitted declarations files
54+
const w = 12n; // should emit as const w = 12n
55+
const x = -12n; // should emit as const x = -12n
56+
const y: 12n = 12n; // should emit type 12n
57+
let z = 12n; // should emit type bigint in declaration file

0 commit comments

Comments
 (0)