Skip to content

Commit 6e03d62

Browse files
authored
fix(1250): fix bigint literal type declaration emit (#1258)
1 parent 35f3043 commit 6e03d62

11 files changed

+81
-39
lines changed

internal/checker/emitresolver.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -900,12 +900,10 @@ func (r *emitResolver) CreateLiteralConstValue(emitContext *printer.EmitContext,
900900
if t.flags&TypeFlagsLiteral == 0 {
901901
return nil // non-literal type
902902
}
903-
literalValue := t.AsLiteralType().value
904-
switch literalValue.(type) {
903+
switch value := t.AsLiteralType().value.(type) {
905904
case string:
906-
return emitContext.Factory.NewStringLiteral(literalValue.(string))
905+
return emitContext.Factory.NewStringLiteral(value)
907906
case jsnum.Number:
908-
value := literalValue.(jsnum.Number)
909907
if value.Abs() != value {
910908
// negative
911909
return emitContext.Factory.NewPrefixUnaryExpression(
@@ -915,20 +913,13 @@ func (r *emitResolver) CreateLiteralConstValue(emitContext *printer.EmitContext,
915913
}
916914
return emitContext.Factory.NewNumericLiteral(value.String())
917915
case jsnum.PseudoBigInt:
918-
value := literalValue.(jsnum.PseudoBigInt)
919-
if value.Negative {
920-
// negative
921-
return emitContext.Factory.NewPrefixUnaryExpression(
922-
ast.KindMinusToken,
923-
emitContext.Factory.NewBigIntLiteral(value.Base10Value),
924-
)
925-
}
926-
return emitContext.Factory.NewNumericLiteral(value.Base10Value)
916+
return emitContext.Factory.NewBigIntLiteral(pseudoBigIntToString(value) + "n")
927917
case bool:
928-
if literalValue.(bool) {
929-
return emitContext.Factory.NewKeywordExpression(ast.KindTrueKeyword)
918+
kind := ast.KindFalseKeyword
919+
if value {
920+
kind = ast.KindTrueKeyword
930921
}
931-
return emitContext.Factory.NewKeywordExpression(ast.KindFalseKeyword)
922+
return emitContext.Factory.NewKeywordExpression(kind)
932923
}
933924
panic("unhandled literal const value kind")
934925
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////
2+
3+
//// [a.ts]
4+
export const a = 0n;
5+
export const b = 10n;
6+
export const c = -0n;
7+
export const d = -10n;
8+
9+
10+
//// [a.js]
11+
export const a = 0n;
12+
export const b = 10n;
13+
export const c = -0n;
14+
export const d = -10n;
15+
16+
17+
//// [a.d.ts]
18+
export declare const a = 0n;
19+
export declare const b = 10n;
20+
export declare const c = 0n;
21+
export declare const d = -10n;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////
2+
3+
=== a.ts ===
4+
export const a = 0n;
5+
>a : Symbol(a, Decl(a.ts, 0, 12))
6+
7+
export const b = 10n;
8+
>b : Symbol(b, Decl(a.ts, 1, 12))
9+
10+
export const c = -0n;
11+
>c : Symbol(c, Decl(a.ts, 2, 12))
12+
13+
export const d = -10n;
14+
>d : Symbol(d, Decl(a.ts, 3, 12))
15+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////
2+
3+
=== a.ts ===
4+
export const a = 0n;
5+
>a : 0n
6+
>0n : 0n
7+
8+
export const b = 10n;
9+
>b : 10n
10+
>10n : 10n
11+
12+
export const c = -0n;
13+
>c : 0n
14+
>-0n : 0n
15+
>0n : 0n
16+
17+
export const d = -10n;
18+
>d : -10n
19+
>-10n : -10n
20+
>10n : 10n
21+

testdata/baselines/reference/submodule/compiler/bigintWithLib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ declare let bigUintArray: BigUint64Array;
134134
// Test added DataView methods
135135
declare const dataView: DataView<ArrayBuffer>;
136136
// Test emitted declarations files
137-
declare const w = 12; // should emit as const w = 12n
138-
declare const x = -12; // should emit as const x = -12n
137+
declare const w = 12n; // should emit as const w = 12n
138+
declare const x = -12n; // should emit as const x = -12n
139139
declare const y: 12n; // should emit type 12n
140140
declare let z: bigint; // should emit type bigint in declaration file

testdata/baselines/reference/submodule/compiler/bigintWithLib.js.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
-declare const y: 12n;
2121
-declare let z: bigint;
2222
+// Test emitted declarations files
23-
+declare const w = 12; // should emit as const w = 12n
24-
+declare const x = -12; // should emit as const x = -12n
23+
+declare const w = 12n; // should emit as const w = 12n
24+
+declare const x = -12n; // should emit as const x = -12n
2525
+declare const y: 12n; // should emit type 12n
2626
+declare let z: bigint; // should emit type bigint in declaration file

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorsExpressions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ export declare const numberConst = 1;
242242
export declare const numberConstBad1: number;
243243
export declare const numberConstBad2: number;
244244
export declare const numberConstBad3 = 1;
245-
export declare const bigIntConst = 1;
245+
export declare const bigIntConst = 1n;
246246
export declare const bigIntConstBad1: bigint;
247247
export declare const bigIntConstBad2: bigint;
248-
export declare const bigIntConstBad3 = 1;
248+
export declare const bigIntConstBad3 = 1n;
249249
export declare const stringConst = "s";
250250
export declare const stringConstBad: string;
251251
// These are just strings

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorsExpressions.js.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
+export declare const numberConstBad1: number;
1212
+export declare const numberConstBad2: number;
1313
+export declare const numberConstBad3 = 1;
14-
+export declare const bigIntConst = 1;
14+
+export declare const bigIntConst = 1n;
1515
+export declare const bigIntConstBad1: bigint;
1616
+export declare const bigIntConstBad2: bigint;
17-
+export declare const bigIntConstBad3 = 1;
17+
+export declare const bigIntConstBad3 = 1n;
1818
+export declare const stringConst = "s";
1919
+export declare const stringConstBad: string;
2020
+// These are just strings

testdata/baselines/reference/submodule/compiler/isolatedDeclarationsLiterals.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ export declare const oneOctal = 1;
154154
export declare const oneHex = 1;
155155
export declare const pOne = 1;
156156
export declare const mOne = -1;
157-
export declare const onen = 1;
158-
export declare const mOnen = -1;
157+
export declare const onen = 1n;
158+
export declare const mOnen = -1n;
159159
export declare const oneStrDoubleQuote = "1";
160160
export declare const oneStrSingleQuote = "1";
161161
export declare const oneStrTemplate = "1";

testdata/baselines/reference/submodule/compiler/isolatedDeclarationsLiterals.js.diff

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @target: es2020
2+
// @declaration: true
3+
// @filename: a.ts
4+
export const a = 0n;
5+
export const b = 10n;
6+
export const c = -0n;
7+
export const d = -10n;

0 commit comments

Comments
 (0)