Skip to content
Open
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
5 changes: 4 additions & 1 deletion tsc/internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ func IsPropertyNameLiteral(node *Node) bool {
case KindIdentifier,
KindStringLiteral,
KindNoSubstitutionTemplateLiteral,
KindNumericLiteral:
KindNumericLiteral,
KindBigIntLiteral:
return true
}
return false
Expand All @@ -331,6 +332,8 @@ func IsPropertyName(node *Node) bool {
KindPrivateIdentifier,
KindStringLiteral,
KindNumericLiteral,
KindBigIntLiteral,
KindNoSubstitutionTemplateLiteral,
KindComputedPropertyName:
return true
}
Expand Down
19 changes: 18 additions & 1 deletion tsc/internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2470,16 +2470,27 @@ 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
}
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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
~~
Expand Down Expand Up @@ -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" };
Expand All @@ -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.
Expand All @@ -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'.
Expand Down
20 changes: 10 additions & 10 deletions tsc/testdata/baselines/reference/compiler/bigintPropertyName.types
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -86,7 +86,7 @@ class L {

const g : G = { 2n: "propertyNameError2" };
>g : G
>{ 2n: "propertyNameError2" } : {}
>{ 2n: "propertyNameError2" } : { 2n: string; }
>2n : string
>"propertyNameError2" : "propertyNameError2"

Expand All @@ -108,7 +108,7 @@ g2[2n];

const h : H = { 3n: "propertyNameErrorAndMissingProperty3" };
>h : H
>{ 3n: "propertyNameErrorAndMissingProperty3" } : {}
>{ 3n: "propertyNameErrorAndMissingProperty3" } : { 3n: string; }
>3n : string
>"propertyNameErrorAndMissingProperty3" : "propertyNameErrorAndMissingProperty3"

Expand All @@ -130,7 +130,7 @@ h2[3n];

const k : K = { 4n: "propertyNameError4" };
>k : K
>{ 4n: "propertyNameError4" } : {}
>{ 4n: "propertyNameError4" } : { 4n: string; }
>4n : string
>"propertyNameError4" : "propertyNameError4"

Expand All @@ -152,7 +152,7 @@ k2[4n];

const l : L = { 5n: "propertyNameErrorAndMissingProperty5" };
>l : L
>{ 5n: "propertyNameErrorAndMissingProperty5" } : {}
>{ 5n: "propertyNameErrorAndMissingProperty5" } : { 5n: string; }
>5n : string
>"propertyNameErrorAndMissingProperty5" : "propertyNameErrorAndMissingProperty5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum E {
>E : E

0n = 0,
>0n : (typeof E)["\uFFFDmissing"]
>0n : (typeof E)["0n"]
>0 : 0
}

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}


Original file line number Diff line number Diff line change
@@ -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
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
}