Skip to content

Properly preserve numeric string named properties in declaration files #39658

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 2 commits into from
Jul 20, 2020
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
20 changes: 10 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5575,15 +5575,14 @@ namespace ts {
}
}

function isStringNamed(d: Declaration) {
const name = getNameOfDeclaration(d);
return !!name && isStringLiteral(name);
}

function isSingleQuotedStringNamed(d: Declaration) {
const name = getNameOfDeclaration(d);
if (name && isStringLiteral(name) && (
name.singleQuote ||
(!nodeIsSynthesized(name) && startsWith(getTextOfNode(name, /*includeTrivia*/ false), "'"))
)) {
return true;
}
return false;
return !!(name && isStringLiteral(name) && (name.singleQuote || !nodeIsSynthesized(name) && startsWith(getTextOfNode(name, /*includeTrivia*/ false), "'")));
}

function getPropertyNameNodeForSymbol(symbol: Symbol, context: NodeBuilderContext) {
Expand All @@ -5596,7 +5595,8 @@ namespace ts {
return factory.createComputedPropertyName(factory.createPropertyAccessExpression(factory.createIdentifier("Symbol"), (symbol.escapedName as string).substr(3)));
}
const rawName = unescapeLeadingUnderscores(symbol.escapedName);
return createPropertyNameNodeForIdentifierOrLiteral(rawName, singleQuote);
const stringNamed = !!length(symbol.declarations) && every(symbol.declarations, isStringNamed);
return createPropertyNameNodeForIdentifierOrLiteral(rawName, stringNamed, singleQuote);
}

// See getNameForSymbolFromNameType for a stringy equivalent
Expand All @@ -5619,9 +5619,9 @@ namespace ts {
}
}

function createPropertyNameNodeForIdentifierOrLiteral(name: string, singleQuote?: boolean) {
function createPropertyNameNodeForIdentifierOrLiteral(name: string, stringNamed?: boolean, singleQuote?: boolean) {
return isIdentifierText(name, compilerOptions.target) ? factory.createIdentifier(name) :
isNumericLiteralName(name) && +name >= 0 ? factory.createNumericLiteral(+name) :
!stringNamed && isNumericLiteralName(name) && +name >= 0 ? factory.createNumericLiteral(+name) :
factory.createStringLiteral(name, !!singleQuote);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(34,5): error TS2741: Property ''1.'' is missing in type 'S' but required in type '{ '1.': string; bar?: string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(35,5): error TS2741: Property ''1.'' is missing in type 'S2' but required in type '{ '1.': string; bar?: string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(36,5): error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(42,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(65,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(71,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
Expand Down Expand Up @@ -108,11 +108,11 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme

a2 = b2;
~~
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
!!! error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts:18:16: ''1.0'' is declared here.
b2 = a2;
~~
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts:19:16: ''1'' is declared here.
a2 = b; // ok
a2 = t2; // ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ module JustStrings {
>'' : ""

var b2 = { '1': '' };
>b2 : { 1: string; }
>{ '1': '' } : { 1: string; }
>b2 : { '1': string; }
>{ '1': '' } : { '1': string; }
>'1' : string
>'' : ""

Expand Down Expand Up @@ -126,13 +126,13 @@ module JustStrings {
>a2 : { '1.0': string; }

a2 = b2;
>a2 = b2 : { 1: string; }
>a2 = b2 : { '1': string; }
>a2 : { '1.0': string; }
>b2 : { 1: string; }
>b2 : { '1': string; }

b2 = a2;
>b2 = a2 : { '1.0': string; }
>b2 : { 1: string; }
>b2 : { '1': string; }
>a2 : { '1.0': string; }

a2 = b; // ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tests/cases/compiler/assignmentIndexedToPrimitives.ts(8,7): error TS2322: Type '
tests/cases/compiler/assignmentIndexedToPrimitives.ts(9,7): error TS2322: Type 'string[]' is not assignable to type '"01"'.
tests/cases/compiler/assignmentIndexedToPrimitives.ts(11,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'number'.
tests/cases/compiler/assignmentIndexedToPrimitives.ts(13,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
tests/cases/compiler/assignmentIndexedToPrimitives.ts(14,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
tests/cases/compiler/assignmentIndexedToPrimitives.ts(14,7): error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
tests/cases/compiler/assignmentIndexedToPrimitives.ts(15,7): error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.


Expand Down Expand Up @@ -48,7 +48,7 @@ tests/cases/compiler/assignmentIndexedToPrimitives.ts(15,7): error TS2322: Type
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
const so2: string = { "0": 1 };
~~~
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
!!! error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
const so3: string = { 0: "1" };
~~~
!!! error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const so1: string = { 0: 1 };

const so2: string = { "0": 1 };
>so2 : string
>{ "0": 1 } : { 0: number; }
>{ "0": 1 } : { "0": number; }
>"0" : number
>1 : 1

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/deepKeysIndexing.types
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type keys2workaround<O extends DeepObject, K1 extends keyof O> = Extract<

interface Foo extends DeepObject {
a: {
>a : { 1: 123; 2: string; 3: boolean; }
>a : { "1": 123; "2": string; "3": boolean; }

"1": 123;
>"1" : 123
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"1" : string
>"one" : "one"
>"2" : string
Expand All @@ -19,7 +19,7 @@
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"1" : string
>"one" : "one"
>"2" : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"1" : string
>"one" : "one"
>"2" : string
Expand All @@ -19,7 +19,7 @@
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
>"1" : string
>"one" : "one"
>"2" : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export declare enum MouseButton {
}
export declare const DOMMouseButton: {
'-1': MouseButton;
0: MouseButton;
1: MouseButton;
2: MouseButton;
3: MouseButton;
4: MouseButton;
"0": MouseButton;
"1": MouseButton;
"2": MouseButton;
"3": MouseButton;
"4": MouseButton;
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export enum MouseButton {
}

export const DOMMouseButton = {
>DOMMouseButton : { '-1': MouseButton; 0: MouseButton; 1: MouseButton; 2: MouseButton; 3: MouseButton; 4: MouseButton; }
>{ '-1': MouseButton.NO_BUTTON, "0": MouseButton.LEFT_BUTTON, "1": MouseButton.MIDDLE_BUTTON, "2": MouseButton.RIGHT_BUTTON, "3": MouseButton.XBUTTON1_BUTTON, "4": MouseButton.XBUTTON2_BUTTON,} : { '-1': MouseButton; 0: MouseButton; 1: MouseButton; 2: MouseButton; 3: MouseButton; 4: MouseButton; }
>DOMMouseButton : { '-1': MouseButton; "0": MouseButton; "1": MouseButton; "2": MouseButton; "3": MouseButton; "4": MouseButton; }
>{ '-1': MouseButton.NO_BUTTON, "0": MouseButton.LEFT_BUTTON, "1": MouseButton.MIDDLE_BUTTON, "2": MouseButton.RIGHT_BUTTON, "3": MouseButton.XBUTTON1_BUTTON, "4": MouseButton.XBUTTON2_BUTTON,} : { '-1': MouseButton; "0": MouseButton; "1": MouseButton; "2": MouseButton; "3": MouseButton; "4": MouseButton; }

'-1': MouseButton.NO_BUTTON,
>'-1' : MouseButton
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/inferringAnyFunctionType1.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/inferringAnyFunctionType1.ts ===
function f<T extends { "0": (p1: number) => number }>(p: T): T {
>f : <T extends { 0: (p1: number) => number; }>(p: T) => T
>f : <T extends { "0": (p1: number) => number; }>(p: T) => T
>"0" : (p1: number) => number
>p1 : number
>p : T
Expand All @@ -12,7 +12,7 @@ function f<T extends { "0": (p1: number) => number }>(p: T): T {
var v = f([x => x]);
>v : [(x: number) => number]
>f([x => x]) : [(x: number) => number]
>f : <T extends { 0: (p1: number) => number; }>(p: T) => T
>f : <T extends { "0": (p1: number) => number; }>(p: T) => T
>[x => x] : [(x: number) => number]
>x => x : (x: number) => number
>x : number
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/literalsInComputedProperties1.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/compiler/literalsInComputedProperties1.ts ===
let x = {
>x : { 1: number; 2: number; 3: number; 4: number; }
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; 3: number; 4: number; }
>x : { 1: number; 2: number; "3": number; 4: number; }
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; 4: number; }

1:1,
>1 : number
Expand All @@ -25,31 +25,31 @@ x[1].toExponential();
>x[1].toExponential() : string
>x[1].toExponential : (fractionDigits?: number) => string
>x[1] : number
>x : { 1: number; 2: number; 3: number; 4: number; }
>x : { 1: number; 2: number; "3": number; 4: number; }
>1 : 1
>toExponential : (fractionDigits?: number) => string

x[2].toExponential();
>x[2].toExponential() : string
>x[2].toExponential : (fractionDigits?: number) => string
>x[2] : number
>x : { 1: number; 2: number; 3: number; 4: number; }
>x : { 1: number; 2: number; "3": number; 4: number; }
>2 : 2
>toExponential : (fractionDigits?: number) => string

x[3].toExponential();
>x[3].toExponential() : string
>x[3].toExponential : (fractionDigits?: number) => string
>x[3] : number
>x : { 1: number; 2: number; 3: number; 4: number; }
>x : { 1: number; 2: number; "3": number; 4: number; }
>3 : 3
>toExponential : (fractionDigits?: number) => string

x[4].toExponential();
>x[4].toExponential() : string
>x[4].toExponential : (fractionDigits?: number) => string
>x[4] : number
>x : { 1: number; 2: number; 3: number; 4: number; }
>x : { 1: number; 2: number; "3": number; 4: number; }
>4 : 4
>toExponential : (fractionDigits?: number) => string

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/multipleNumericIndexers.types
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var b: {
>x : number

} = { 1: '', "2": '' }
>{ 1: '', "2": '' } : { 1: string; 2: string; }
>{ 1: '', "2": '' } : { 1: string; "2": string; }
>1 : string
>'' : ""
>"2" : string
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/noImplicitAnyIndexing.types
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ interface MyMap<T> {

var m: MyMap<number> = {
>m : MyMap<number>
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { 0: number; 1: number; 2: number; "Okay that's enough for today.": number; }
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { "0": number; "1": number; "2": number; "Okay that's enough for today.": number; }

"0": 0,
>"0" : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface MyMap<T> {

var m: MyMap<number> = {
>m : MyMap<number>
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { 0: number; 1: number; 2: number; "Okay that's enough for today.": number; }
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { "0": number; "1": number; "2": number; "Okay that's enough for today.": number; }

"0": 0,
>"0" : number
Expand Down
Loading