From b394fe36fa687b01a9de180b5463f3418d073db3 Mon Sep 17 00:00:00 2001 From: rudy-07 Date: Fri, 6 Nov 2026 16:30:39 +0530 Subject: [PATCH] fix(61769): emit object literal methods with bigint property names in declaration emit --- tsc/internal/ast/utilities.go | 5 +- tsc/internal/checker/nodebuilderimpl.go | 19 ++++++- .../compiler/bigintPropertyName.errors.txt | 22 ++++---- .../compiler/bigintPropertyName.types | 20 ++++---- .../reference/compiler/enumWithBigint.types | 2 +- ...bjectLiteralBigIntMethodDeclarationEmit.js | 51 +++++++++++++++++++ ...LiteralBigIntMethodDeclarationEmit.symbols | 34 +++++++++++++ ...ctLiteralBigIntMethodDeclarationEmit.types | 40 +++++++++++++++ ...bjectLiteralBigIntMethodDeclarationEmit.ts | 21 ++++++++ 9 files changed, 192 insertions(+), 22 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.js create mode 100644 tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.types create mode 100644 tsc/testdata/tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts diff --git a/tsc/internal/ast/utilities.go b/tsc/internal/ast/utilities.go index c63ce7238c840..1a89c11836e52 100644 --- a/tsc/internal/ast/utilities.go +++ b/tsc/internal/ast/utilities.go @@ -311,7 +311,8 @@ func IsPropertyNameLiteral(node *Node) bool { case KindIdentifier, KindStringLiteral, KindNoSubstitutionTemplateLiteral, - KindNumericLiteral: + KindNumericLiteral, + KindBigIntLiteral: return true } return false @@ -331,6 +332,8 @@ func IsPropertyName(node *Node) bool { KindPrivateIdentifier, KindStringLiteral, KindNumericLiteral, + KindBigIntLiteral, + KindNoSubstitutionTemplateLiteral, KindComputedPropertyName: return true } diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index d25c14a5fff44..cd95f632ac150 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -2470,8 +2470,13 @@ const ( propertyNameNodeKindIdentifier propertyNameNodeKind = iota propertyNameNodeKindNumericLiteral propertyNameNodeKindStringLiteral + propertyNameNodeKindBigIntLiteral ) +func isBigIntLiteralName(name string) bool { + return len(name) >= 2 && name[0] != '-' && strings.HasSuffix(name, "n") && isValidBigIntString(name[:len(name)-1], true) +} + func classifyPropertyName(name string, stringNamed bool, isMethod bool) propertyNameNodeKind { if isMethod && name == "new" { return propertyNameNodeKindStringLiteral @@ -2479,7 +2484,13 @@ func classifyPropertyName(name string, stringNamed bool, isMethod bool) property if scanner.IsIdentifierText(name, core.LanguageVariantStandard) { return propertyNameNodeKindIdentifier } - return core.IfElse(!stringNamed && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0, propertyNameNodeKindNumericLiteral, propertyNameNodeKindStringLiteral) + if !stringNamed && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0 { + return propertyNameNodeKindNumericLiteral + } + if !stringNamed && isBigIntLiteralName(name) { + return propertyNameNodeKindBigIntLiteral + } + return propertyNameNodeKindStringLiteral } func (b *NodeBuilderImpl) createPropertyNameNodeForIdentifierOrLiteral(name string, singleQuote bool, stringNamed bool, isMethod bool, symbol *ast.Symbol) *ast.Node { @@ -2488,6 +2499,8 @@ func (b *NodeBuilderImpl) createPropertyNameNodeForIdentifierOrLiteral(name stri return b.newIdentifier(name, symbol) case propertyNameNodeKindNumericLiteral: return b.f.NewNumericLiteral(name, ast.TokenFlagsNone) + case propertyNameNodeKindBigIntLiteral: + return b.f.NewBigIntLiteral(name, ast.TokenFlagsNone) default: return b.f.NewStringLiteral(name, core.IfElse(singleQuote, ast.TokenFlagsSingleQuote, ast.TokenFlagsNone)) } @@ -2586,6 +2599,10 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S } return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol) } + if nameType.flags&TypeFlagsBigIntLiteral != 0 { + name := pseudoBigIntToString(getBigIntLiteralValue(nameType)) + "n" + return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol) + } if nameType.flags&TypeFlagsUniqueESSymbol != 0 { // The reference was tracked in the destination scope by trackComputedName. // Reconstructing its spelling in the source scope must not paint that scope's declarations visible. diff --git a/tsc/testdata/baselines/reference/compiler/bigintPropertyName.errors.txt b/tsc/testdata/baselines/reference/compiler/bigintPropertyName.errors.txt index 2b2db4f45c631..c5a4d1209fe81 100644 --- a/tsc/testdata/baselines/reference/compiler/bigintPropertyName.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/bigintPropertyName.errors.txt @@ -9,15 +9,16 @@ g.ts(8,5): error TS1539: A 'bigint' literal cannot be used as a property name. g.ts(15,17): error TS1539: A 'bigint' literal cannot be used as a property name. g.ts(17,3): error TS2538: Type 'bigint' cannot be used as an index type. g.ts(18,4): error TS2538: Type 'bigint' cannot be used as an index type. -g.ts(20,7): error TS2741: Property '"3n"' is missing in type '{}' but required in type 'H'. g.ts(20,17): error TS1539: A 'bigint' literal cannot be used as a property name. g.ts(22,3): error TS2538: Type 'bigint' cannot be used as an index type. g.ts(23,4): error TS2538: Type 'bigint' cannot be used as an index type. g.ts(25,17): error TS1539: A 'bigint' literal cannot be used as a property name. +g.ts(25,17): error TS2322: Type 'string' is not assignable to type 'number'. +g.ts(26,18): error TS2322: Type 'string' is not assignable to type 'number'. g.ts(27,3): error TS2538: Type 'bigint' cannot be used as an index type. g.ts(28,4): error TS2538: Type 'bigint' cannot be used as an index type. -g.ts(30,7): error TS2741: Property '"5n"' is missing in type '{}' but required in type 'L'. g.ts(30,17): error TS1539: A 'bigint' literal cannot be used as a property name. +g.ts(30,17): error TS2322: Type 'string' is not assignable to type 'number'. g.ts(31,18): error TS2322: Type 'string' is not assignable to type 'number'. g.ts(32,3): error TS2538: Type 'bigint' cannot be used as an index type. g.ts(33,4): error TS2538: Type 'bigint' cannot be used as an index type. @@ -56,7 +57,7 @@ q.ts(2,19): error TS2322: Type 'bigint' is not assignable to type 'string | numb ~~ !!! error TS1539: A 'bigint' literal cannot be used as a property name. -==== g.ts (19 errors) ==== +==== g.ts (20 errors) ==== interface G { 2n: string; ~~ @@ -87,9 +88,6 @@ q.ts(2,19): error TS2322: Type 'bigint' is not assignable to type 'string | numb !!! error TS2538: Type 'bigint' cannot be used as an index type. const h : H = { 3n: "propertyNameErrorAndMissingProperty3" }; - ~ -!!! error TS2741: Property '"3n"' is missing in type '{}' but required in type 'H'. -!!! related TS2728 g.ts:5:5: '"3n"' is declared here. ~~ !!! error TS1539: A 'bigint' literal cannot be used as a property name. const h2 : H = { "3n": "ok3" }; @@ -103,7 +101,13 @@ q.ts(2,19): error TS2322: Type 'bigint' is not assignable to type 'string | numb const k : K = { 4n: "propertyNameError4" }; ~~ !!! error TS1539: A 'bigint' literal cannot be used as a property name. + ~~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! related TS6500 g.ts:8:5: The expected type comes from property '4n' which is declared here on type 'K' const k2 : K = { "4n": "ok4" }; + ~~~~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! related TS6500 g.ts:8:5: The expected type comes from property '4n' which is declared here on type 'K' k[4n]; ~~ !!! error TS2538: Type 'bigint' cannot be used as an index type. @@ -112,11 +116,11 @@ q.ts(2,19): error TS2322: Type 'bigint' is not assignable to type 'string | numb !!! error TS2538: Type 'bigint' cannot be used as an index type. const l : L = { 5n: "propertyNameErrorAndMissingProperty5" }; - ~ -!!! error TS2741: Property '"5n"' is missing in type '{}' but required in type 'L'. -!!! related TS2728 g.ts:12:5: '"5n"' is declared here. ~~ !!! error TS1539: A 'bigint' literal cannot be used as a property name. + ~~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! related TS6500 g.ts:12:5: The expected type comes from property '5n' which is declared here on type 'L' const l2 : L = { "5n": "ok4" }; ~~~~ !!! error TS2322: Type 'string' is not assignable to type 'number'. diff --git a/tsc/testdata/baselines/reference/compiler/bigintPropertyName.types b/tsc/testdata/baselines/reference/compiler/bigintPropertyName.types index 5de36ea088f57..e3d4412c9495b 100644 --- a/tsc/testdata/baselines/reference/compiler/bigintPropertyName.types +++ b/tsc/testdata/baselines/reference/compiler/bigintPropertyName.types @@ -3,8 +3,8 @@ === a.ts === // BigInt cannot be used as an object literal property { ({1n: 123}); }; ->({1n: 123}) : {} ->{1n: 123} : {} +>({1n: 123}) : { 1n: number; } +>{1n: 123} : { 1n: number; } >1n : number >123 : 123 @@ -13,8 +13,8 @@ const bigNum: bigint = 0n; >0n : 0n const a = { 1n: 123 }; ->a : {} ->{ 1n: 123 } : {} +>a : { 1n: number; } +>{ 1n: 123 } : { 1n: number; } >1n : number >123 : 123 @@ -54,8 +54,8 @@ const { 0n: f } = arr; // bigint should give an index error // BigInt cannot be used as an property name const x = { 0n: 123 }; ->x : {} ->{ 0n: 123 } : {} +>x : { 0n: number; } +>{ 0n: 123 } : { 0n: number; } >0n : number >123 : 123 @@ -86,7 +86,7 @@ class L { const g : G = { 2n: "propertyNameError2" }; >g : G ->{ 2n: "propertyNameError2" } : {} +>{ 2n: "propertyNameError2" } : { 2n: string; } >2n : string >"propertyNameError2" : "propertyNameError2" @@ -108,7 +108,7 @@ g2[2n]; const h : H = { 3n: "propertyNameErrorAndMissingProperty3" }; >h : H ->{ 3n: "propertyNameErrorAndMissingProperty3" } : {} +>{ 3n: "propertyNameErrorAndMissingProperty3" } : { 3n: string; } >3n : string >"propertyNameErrorAndMissingProperty3" : "propertyNameErrorAndMissingProperty3" @@ -130,7 +130,7 @@ h2[3n]; const k : K = { 4n: "propertyNameError4" }; >k : K ->{ 4n: "propertyNameError4" } : {} +>{ 4n: "propertyNameError4" } : { 4n: string; } >4n : string >"propertyNameError4" : "propertyNameError4" @@ -152,7 +152,7 @@ k2[4n]; const l : L = { 5n: "propertyNameErrorAndMissingProperty5" }; >l : L ->{ 5n: "propertyNameErrorAndMissingProperty5" } : {} +>{ 5n: "propertyNameErrorAndMissingProperty5" } : { 5n: string; } >5n : string >"propertyNameErrorAndMissingProperty5" : "propertyNameErrorAndMissingProperty5" diff --git a/tsc/testdata/baselines/reference/compiler/enumWithBigint.types b/tsc/testdata/baselines/reference/compiler/enumWithBigint.types index 0c7bfd2a40ff2..15a62fad8deb9 100644 --- a/tsc/testdata/baselines/reference/compiler/enumWithBigint.types +++ b/tsc/testdata/baselines/reference/compiler/enumWithBigint.types @@ -5,7 +5,7 @@ enum E { >E : E 0n = 0, ->0n : (typeof E)["\uFFFDmissing"] +>0n : (typeof E)["0n"] >0 : 0 } diff --git a/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.js b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.js new file mode 100644 index 0000000000000..5896f453ffa96 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.js @@ -0,0 +1,51 @@ +//// [tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts] //// + +//// [objectLiteralBigIntMethodDeclarationEmit.ts] +export const foo = { + 1n() { + return 1; + }, + 2() { + return 2; + } +}; + +export class Foo { + 1n() { + return 1; + } + 2() { + return 2; + } +} + + + +//// [objectLiteralBigIntMethodDeclarationEmit.js] +export const foo = { + 1n() { + return 1; + }, + 2() { + return 2; + } +}; +export class Foo { + 1n() { + return 1; + } + 2() { + return 2; + } +} + + +//// [objectLiteralBigIntMethodDeclarationEmit.d.ts] +export declare const foo: { + 1n(): number; + 2(): number; +}; +export declare class Foo { + 1n(): number; + 2(): number; +} diff --git a/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.symbols b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.symbols new file mode 100644 index 0000000000000..10c09fdcf5229 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.symbols @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts] //// + +=== objectLiteralBigIntMethodDeclarationEmit.ts === +export const foo = { +>foo : Symbol(foo, Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 0, 12)) + + 1n() { +>1n : Symbol(1n, Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 0, 20)) + + return 1; + }, + 2() { +>2 : Symbol(2, Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 3, 6)) + + return 2; + } +}; + +export class Foo { +>Foo : Symbol(Foo, Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 7, 2)) + + 1n() { +>1n : Symbol(Foo[1n], Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 9, 18)) + + return 1; + } + 2() { +>2 : Symbol(Foo[2], Decl(objectLiteralBigIntMethodDeclarationEmit.ts, 12, 5)) + + return 2; + } +} + + diff --git a/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.types b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.types new file mode 100644 index 0000000000000..468e16ad539b9 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/objectLiteralBigIntMethodDeclarationEmit.types @@ -0,0 +1,40 @@ +//// [tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts] //// + +=== objectLiteralBigIntMethodDeclarationEmit.ts === +export const foo = { +>foo : { 1n(): number; 2(): number; } +>{ 1n() { return 1; }, 2() { return 2; }} : { 1n(): number; 2(): number; } + + 1n() { +>1n : () => number + + return 1; +>1 : 1 + + }, + 2() { +>2 : () => number + + return 2; +>2 : 2 + } +}; + +export class Foo { +>Foo : Foo + + 1n() { +>1n : () => number + + return 1; +>1 : 1 + } + 2() { +>2 : () => number + + return 2; +>2 : 2 + } +} + + diff --git a/tsc/testdata/tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts b/tsc/testdata/tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts new file mode 100644 index 0000000000000..f7e6cb2175502 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/objectLiteralBigIntMethodDeclarationEmit.ts @@ -0,0 +1,21 @@ +// @target: es2020 +// @declaration: true + +export const foo = { + 1n() { + return 1; + }, + 2() { + return 2; + } +}; + +export class Foo { + 1n() { + return 1; + } + 2() { + return 2; + } +} +