Skip to content

fix(1250): fix bigint literal type declaration emit #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,10 @@ func (r *emitResolver) CreateLiteralConstValue(emitContext *printer.EmitContext,
if t.flags&TypeFlagsLiteral == 0 {
return nil // non-literal type
}
literalValue := t.AsLiteralType().value
switch literalValue.(type) {
switch value := t.AsLiteralType().value.(type) {
case string:
return emitContext.Factory.NewStringLiteral(literalValue.(string))
return emitContext.Factory.NewStringLiteral(value)
case jsnum.Number:
value := literalValue.(jsnum.Number)
if value.Abs() != value {
// negative
return emitContext.Factory.NewPrefixUnaryExpression(
Expand All @@ -915,20 +913,13 @@ func (r *emitResolver) CreateLiteralConstValue(emitContext *printer.EmitContext,
}
return emitContext.Factory.NewNumericLiteral(value.String())
case jsnum.PseudoBigInt:
value := literalValue.(jsnum.PseudoBigInt)
if value.Negative {
// negative
return emitContext.Factory.NewPrefixUnaryExpression(
ast.KindMinusToken,
emitContext.Factory.NewBigIntLiteral(value.Base10Value),
)
}
return emitContext.Factory.NewNumericLiteral(value.Base10Value)
return emitContext.Factory.NewBigIntLiteral(pseudoBigIntToString(value) + "n")
case bool:
if literalValue.(bool) {
return emitContext.Factory.NewKeywordExpression(ast.KindTrueKeyword)
kind := ast.KindFalseKeyword
if value {
kind = ast.KindTrueKeyword
}
return emitContext.Factory.NewKeywordExpression(ast.KindFalseKeyword)
return emitContext.Factory.NewKeywordExpression(kind)
}
panic("unhandled literal const value kind")
}
Expand Down
21 changes: 21 additions & 0 deletions testdata/baselines/reference/compiler/declarationEmitBigInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////

//// [a.ts]
export const a = 0n;
export const b = 10n;
export const c = -0n;
export const d = -10n;


//// [a.js]
export const a = 0n;
export const b = 10n;
export const c = -0n;
export const d = -10n;


//// [a.d.ts]
export declare const a = 0n;
export declare const b = 10n;
export declare const c = 0n;
export declare const d = -10n;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////

=== a.ts ===
export const a = 0n;
>a : Symbol(a, Decl(a.ts, 0, 12))

export const b = 10n;
>b : Symbol(b, Decl(a.ts, 1, 12))

export const c = -0n;
>c : Symbol(c, Decl(a.ts, 2, 12))

export const d = -10n;
>d : Symbol(d, Decl(a.ts, 3, 12))

21 changes: 21 additions & 0 deletions testdata/baselines/reference/compiler/declarationEmitBigInt.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/declarationEmitBigInt.ts] ////

=== a.ts ===
export const a = 0n;
>a : 0n
>0n : 0n

export const b = 10n;
>b : 10n
>10n : 10n

export const c = -0n;
>c : 0n
>-0n : 0n
>0n : 0n

export const d = -10n;
>d : -10n
>-10n : -10n
>10n : 10n

Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ declare let bigUintArray: BigUint64Array;
// Test added DataView methods
declare const dataView: DataView<ArrayBuffer>;
// Test emitted declarations files
declare const w = 12; // should emit as const w = 12n
declare const x = -12; // should emit as const x = -12n
declare const w = 12n; // should emit as const w = 12n
declare const x = -12n; // should emit as const x = -12n
declare const y: 12n; // should emit type 12n
declare let z: bigint; // should emit type bigint in declaration file
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
-declare const y: 12n;
-declare let z: bigint;
+// Test emitted declarations files
+declare const w = 12; // should emit as const w = 12n
+declare const x = -12; // should emit as const x = -12n
+declare const w = 12n; // should emit as const w = 12n
+declare const x = -12n; // should emit as const x = -12n
+declare const y: 12n; // should emit type 12n
+declare let z: bigint; // should emit type bigint in declaration file
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ export declare const numberConst = 1;
export declare const numberConstBad1: number;
export declare const numberConstBad2: number;
export declare const numberConstBad3 = 1;
export declare const bigIntConst = 1;
export declare const bigIntConst = 1n;
export declare const bigIntConstBad1: bigint;
export declare const bigIntConstBad2: bigint;
export declare const bigIntConstBad3 = 1;
export declare const bigIntConstBad3 = 1n;
export declare const stringConst = "s";
export declare const stringConstBad: string;
// These are just strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
+export declare const numberConstBad1: number;
+export declare const numberConstBad2: number;
+export declare const numberConstBad3 = 1;
+export declare const bigIntConst = 1;
+export declare const bigIntConst = 1n;
+export declare const bigIntConstBad1: bigint;
+export declare const bigIntConstBad2: bigint;
+export declare const bigIntConstBad3 = 1;
+export declare const bigIntConstBad3 = 1n;
+export declare const stringConst = "s";
+export declare const stringConstBad: string;
+// These are just strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export declare const oneOctal = 1;
export declare const oneHex = 1;
export declare const pOne = 1;
export declare const mOne = -1;
export declare const onen = 1;
export declare const mOnen = -1;
export declare const onen = 1n;
export declare const mOnen = -1n;
export declare const oneStrDoubleQuote = "1";
export declare const oneStrSingleQuote = "1";
export declare const oneStrTemplate = "1";
Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions testdata/tests/cases/compiler/declarationEmitBigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @target: es2020
// @declaration: true
// @filename: a.ts
export const a = 0n;
export const b = 10n;
export const c = -0n;
export const d = -10n;