diff --git a/changelog_unreleased/typescript/18702.md b/changelog_unreleased/typescript/18702.md new file mode 100644 index 000000000..ad1078470 --- /dev/null +++ b/changelog_unreleased/typescript/18702.md @@ -0,0 +1,32 @@ +#### Respect `quoteProps` in method signature (#18702 by @fisker) + + +```tsx +// Input +type T = { + method1(): string; + "method2"(): string; + "method-3"(): string; +}; + +// Prettier stable +type T = { + method1(): string; + "method2"(): string; + "method-3"(): string; +}; + +// Prettier main (--quote-props=as-needed) +type T = { + method1(): string; + method2(): string; + "method-3"(): string; +}; + +// Prettier main (--quote-props=consistent) +type T = { + "method1"(): string; + "method2"(): string; + "method-3"(): string; +}; +``` diff --git a/src/language-js/clean.js b/src/language-js/clean.js index 81fa9bab1..3432fa06b 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -83,6 +83,7 @@ function clean(original, cloned, parent) { original.type === "PropertyDefinition" || original.type === "TSDeclareMethod" || original.type === "TSPropertySignature" || + original.type === "TSMethodSignature" || original.type === "ObjectTypeProperty" || original.type === "ImportAttribute") && !original.computed diff --git a/src/language-js/print/method-signature.js b/src/language-js/print/method-signature.js index 20d38eb39..f38b666c8 100644 --- a/src/language-js/print/method-signature.js +++ b/src/language-js/print/method-signature.js @@ -4,6 +4,7 @@ import { printFunctionParameters, shouldGroupFunctionParameters, } from "./function-parameters.js"; +import { printKey } from "./key.js"; import { printOptionalToken, printTypeScriptAccessibilityToken, @@ -20,9 +21,7 @@ function printMethodSignature(path, options, print) { parts.push( printTypeScriptAccessibilityToken(node), kind, - node.computed ? "[" : "", - print("key"), - node.computed ? "]" : "", + printKey(path, options, print), printOptionalToken(path), ); diff --git a/tests/format/typescript/quote-props/__snapshots__/format.test.js.snap b/tests/format/typescript/quote-props/__snapshots__/format.test.js.snap index 78826ac2c..c5a8d5d7f 100644 --- a/tests/format/typescript/quote-props/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/quote-props/__snapshots__/format.test.js.snap @@ -141,6 +141,216 @@ enum E4 { ================================================================================ `; +exports[`interfaces.ts - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "as-needed" + | printWidth +=====================================input====================================== +interface I1 { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +interface I1 { + method(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + property: string; + property2: string; + "property-3": string; +} + +================================================================================ +`; + +exports[`interfaces.ts - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "consistent" + | printWidth +=====================================input====================================== +interface I1 { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +interface I1 { + "method"(): string; + "method2"(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + "property2": string; + "property-3": string; +} + +================================================================================ +`; + +exports[`interfaces.ts - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "preserve" + | printWidth +=====================================input====================================== +interface I1 { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +interface I1 { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + property2: string; + "property-3": string; +} + +================================================================================ +`; + +exports[`type-literal.ts - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "as-needed" + | printWidth +=====================================input====================================== +type T1 = { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +type T2 = { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +type T1 = { + method(): string; + method2(): string; + "method-3"(): string; +}; + +type T2 = { + property: string; + property2: string; + "property-3": string; +}; + +================================================================================ +`; + +exports[`type-literal.ts - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "consistent" + | printWidth +=====================================input====================================== +type T1 = { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +type T2 = { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +type T1 = { + "method"(): string; + "method2"(): string; + "method-3"(): string; +}; + +type T2 = { + "property": string; + "property2": string; + "property-3": string; +}; + +================================================================================ +`; + +exports[`type-literal.ts - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +quoteProps: "preserve" + | printWidth +=====================================input====================================== +type T1 = { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +type T2 = { + "property": string; + property2: string; + "property-3": string; +} + +=====================================output===================================== +type T1 = { + "method"(): string; + method2(): string; + "method-3"(): string; +}; + +type T2 = { + "property": string; + property2: string; + "property-3": string; +}; + +================================================================================ +`; + exports[`types.ts - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/quote-props/interfaces.ts b/tests/format/typescript/quote-props/interfaces.ts new file mode 100644 index 000000000..d2b03e0dc --- /dev/null +++ b/tests/format/typescript/quote-props/interfaces.ts @@ -0,0 +1,11 @@ +interface I1 { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +interface I2 { + "property": string; + property2: string; + "property-3": string; +} diff --git a/tests/format/typescript/quote-props/type-literal.ts b/tests/format/typescript/quote-props/type-literal.ts new file mode 100644 index 000000000..f630db14b --- /dev/null +++ b/tests/format/typescript/quote-props/type-literal.ts @@ -0,0 +1,11 @@ +type T1 = { + "method"(): string; + method2(): string; + "method-3"(): string; +} + +type T2 = { + "property": string; + property2: string; + "property-3": string; +}