From 16dab6d5d64dce76927a16e70416bb28d5e3d4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 14 Aug 2023 22:13:42 +0200 Subject: [PATCH] Preserve input key style of computed properties in declaration emit (#55298) --- src/compiler/checker.ts | 9 ++++- ...declarationEmitPropertyNumericStringKey.js | 40 +++++++++++++++++++ ...rationEmitPropertyNumericStringKey.symbols | 31 ++++++++++++++ ...larationEmitPropertyNumericStringKey.types | 39 ++++++++++++++++++ ...ationEmitStringEnumUsedInNonlocalSpread.js | 4 +- ...onEmitStringEnumUsedInNonlocalSpread.types | 6 +-- ...eObjectLiteralProperty_computedName1.types | 4 +- .../literalsInComputedProperties1.types | 12 +++--- ...declarationEmitPropertyNumericStringKey.ts | 13 ++++++ 9 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitPropertyNumericStringKey.js create mode 100644 tests/baselines/reference/declarationEmitPropertyNumericStringKey.symbols create mode 100644 tests/baselines/reference/declarationEmitPropertyNumericStringKey.types create mode 100644 tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c43435bc001..33441d618d7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8082,7 +8082,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function isStringNamed(d: Declaration) { const name = getNameOfDeclaration(d); - return !!name && isStringLiteral(name); + if (!name) { + return false; + } + if (isComputedPropertyName(name)) { + const type = checkExpression(name.expression); + return !!(type.flags & TypeFlags.StringLike); + } + return isStringLiteral(name); } function isSingleQuotedStringNamed(d: Declaration) { diff --git a/tests/baselines/reference/declarationEmitPropertyNumericStringKey.js b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.js new file mode 100644 index 00000000000..2c1a4c01da1 --- /dev/null +++ b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.js @@ -0,0 +1,40 @@ +//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] //// + +//// [declarationEmitPropertyNumericStringKey.ts] +// https://github.com/microsoft/TypeScript/issues/55292 + +const STATUS = { + ["404"]: "not found", +} as const; + +const hundredStr = "100"; +const obj = { [hundredStr]: "foo" }; + +const hundredNum = 100; +const obj2 = { [hundredNum]: "bar" }; + + +//// [declarationEmitPropertyNumericStringKey.js] +// https://github.com/microsoft/TypeScript/issues/55292 +var _a, _b, _c; +var STATUS = (_a = {}, + _a["404"] = "not found", + _a); +var hundredStr = "100"; +var obj = (_b = {}, _b[hundredStr] = "foo", _b); +var hundredNum = 100; +var obj2 = (_c = {}, _c[hundredNum] = "bar", _c); + + +//// [declarationEmitPropertyNumericStringKey.d.ts] +declare const STATUS: { + readonly "404": "not found"; +}; +declare const hundredStr = "100"; +declare const obj: { + "100": string; +}; +declare const hundredNum = 100; +declare const obj2: { + 100: string; +}; diff --git a/tests/baselines/reference/declarationEmitPropertyNumericStringKey.symbols b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.symbols new file mode 100644 index 00000000000..99d9687cb22 --- /dev/null +++ b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.symbols @@ -0,0 +1,31 @@ +//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] //// + +=== declarationEmitPropertyNumericStringKey.ts === +// https://github.com/microsoft/TypeScript/issues/55292 + +const STATUS = { +>STATUS : Symbol(STATUS, Decl(declarationEmitPropertyNumericStringKey.ts, 2, 5)) + + ["404"]: "not found", +>["404"] : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16)) +>"404" : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16)) + +} as const; +>const : Symbol(const) + +const hundredStr = "100"; +>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5)) + +const obj = { [hundredStr]: "foo" }; +>obj : Symbol(obj, Decl(declarationEmitPropertyNumericStringKey.ts, 7, 5)) +>[hundredStr] : Symbol([hundredStr], Decl(declarationEmitPropertyNumericStringKey.ts, 7, 13)) +>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5)) + +const hundredNum = 100; +>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5)) + +const obj2 = { [hundredNum]: "bar" }; +>obj2 : Symbol(obj2, Decl(declarationEmitPropertyNumericStringKey.ts, 10, 5)) +>[hundredNum] : Symbol([hundredNum], Decl(declarationEmitPropertyNumericStringKey.ts, 10, 14)) +>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5)) + diff --git a/tests/baselines/reference/declarationEmitPropertyNumericStringKey.types b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.types new file mode 100644 index 00000000000..2d4d8531b15 --- /dev/null +++ b/tests/baselines/reference/declarationEmitPropertyNumericStringKey.types @@ -0,0 +1,39 @@ +//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] //// + +=== declarationEmitPropertyNumericStringKey.ts === +// https://github.com/microsoft/TypeScript/issues/55292 + +const STATUS = { +>STATUS : { readonly "404": "not found"; } +>{ ["404"]: "not found",} as const : { readonly "404": "not found"; } +>{ ["404"]: "not found",} : { readonly "404": "not found"; } + + ["404"]: "not found", +>["404"] : "not found" +>"404" : "404" +>"not found" : "not found" + +} as const; + +const hundredStr = "100"; +>hundredStr : "100" +>"100" : "100" + +const obj = { [hundredStr]: "foo" }; +>obj : { "100": string; } +>{ [hundredStr]: "foo" } : { "100": string; } +>[hundredStr] : string +>hundredStr : "100" +>"foo" : "foo" + +const hundredNum = 100; +>hundredNum : 100 +>100 : 100 + +const obj2 = { [hundredNum]: "bar" }; +>obj2 : { 100: string; } +>{ [hundredNum]: "bar" } : { 100: string; } +>[hundredNum] : string +>hundredNum : 100 +>"bar" : "bar" + diff --git a/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.js b/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.js index b303c32b198..b2cce438773 100644 --- a/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.js +++ b/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.js @@ -109,7 +109,7 @@ import { A } from './class'; export declare class B extends A { getA(): { a: string; - 123123: string; - 12312312312: string; + "123123": string; + "12312312312": string; }; } diff --git a/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.types b/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.types index 784334b5c95..031089bf3b9 100644 --- a/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.types +++ b/tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.types @@ -34,7 +34,7 @@ export class A { >getA : () => ITest return { ->{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { 123123: string; 12312312312: string; } +>{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { "123123": string; "12312312312": string; } [TestEnum.Test1]: '123', >[TestEnum.Test1] : string @@ -62,10 +62,10 @@ export class B extends A { >A : A getA() { // TS4053 error ->getA : () => { a: string; 123123: string; 12312312312: string; } +>getA : () => { a: string; "123123": string; "12312312312": string; } return { ->{ ...super.getA(), a: '123', } : { a: string; 123123: string; 12312312312: string; } +>{ ...super.getA(), a: '123', } : { a: string; "123123": string; "12312312312": string; } ...super.getA(), >super.getA() : import("class").ITest diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types index fd94a2b9e90..7c3f3e50cd3 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types @@ -90,8 +90,8 @@ const t6 = { } const t7 = { ->t7 : { [-1]: number; } ->{ "-1": 1, ["-1"]: 0 // duplicate} : { [-1]: number; } +>t7 : { "-1": number; } +>{ "-1": 1, ["-1"]: 0 // duplicate} : { "-1": number; } "-1": 1, >"-1" : number diff --git a/tests/baselines/reference/literalsInComputedProperties1.types b/tests/baselines/reference/literalsInComputedProperties1.types index 869ce9f56d8..cd04bb0c8cc 100644 --- a/tests/baselines/reference/literalsInComputedProperties1.types +++ b/tests/baselines/reference/literalsInComputedProperties1.types @@ -2,8 +2,8 @@ === 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 @@ -27,7 +27,7 @@ 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 @@ -35,7 +35,7 @@ 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 @@ -43,7 +43,7 @@ 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 @@ -51,7 +51,7 @@ 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 diff --git a/tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts b/tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts new file mode 100644 index 00000000000..b5692f9d4cc --- /dev/null +++ b/tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts @@ -0,0 +1,13 @@ +// @declaration: true + +// https://github.com/microsoft/TypeScript/issues/55292 + +const STATUS = { + ["404"]: "not found", +} as const; + +const hundredStr = "100"; +const obj = { [hundredStr]: "foo" }; + +const hundredNum = 100; +const obj2 = { [hundredNum]: "bar" };