From e3b2f4adb49caa003a15d61d07a08f5618260ab7 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Tue, 14 May 2019 02:53:45 +0900 Subject: [PATCH] Keep a trailing comma in type parameter in .tsx files (#6115) --- CHANGELOG.unreleased.md | 19 +++++++++++++++++++ src/language-js/printer-estree.js | 13 +++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++++ tests/typescript_tsx/type-parameters.tsx | 3 +++ 4 files changed, 53 insertions(+) create mode 100644 tests/typescript_tsx/type-parameters.tsx diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 4cf95cc6..848e557a 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -65,3 +65,22 @@ Examples: ) ); ``` + +- TypeScript: Keep trailing comma in tsx type parameters ([#6115] by [@sosukesuzuki]) + + Previously, a trailing comma after single type parameter in arrow function was cleaned up. The formatted result is valid as ts, but is invalid as tsx. Prettier master fixes this issue. + + + ```tsx + // Input + type G = any; + const myFunc = (arg1: G) => false; + + // Output (Prettier stable) + type G = any; + const myFunc = (arg1: G) => false; + + // Output (prettier master) + type G = any; + const myFunc = (arg1: G) => false; + ``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 4575d5c1..d2ff113c 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3000,6 +3000,19 @@ function printPathNoParens(path, options, print, args) { parts.push(" = ", path.call(print, "default")); } + // Keep comma if the file extension is .tsx and + // has one type parameter that isn't extend with any types. + // Because, otherwise formatted result will be invalid as tsx. + if ( + parent.params && + parent.params.length === 1 && + options.filepath && + options.filepath.match(/\.tsx/) && + !n.constraint + ) { + parts.push(","); + } + return concat(parts); } case "TypeofTypeAnnotation": diff --git a/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap index c5867b26..09cd5b84 100644 --- a/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap @@ -103,6 +103,24 @@ printWidth: 80 ================================================================================ `; +exports[`type-parameters.tsx 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const functionName1 = (arg) => false; +const functionName2 = (arg) => false; +const functionName3 = (arg) => false; + +=====================================output===================================== +const functionName1 = (arg) => false; +const functionName2 = (arg) => false; +const functionName3 = (arg) => false; + +================================================================================ +`; + exports[`url.tsx 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript_tsx/type-parameters.tsx b/tests/typescript_tsx/type-parameters.tsx new file mode 100644 index 00000000..3b80dc53 --- /dev/null +++ b/tests/typescript_tsx/type-parameters.tsx @@ -0,0 +1,3 @@ +const functionName1 = (arg) => false; +const functionName2 = (arg) => false; +const functionName3 = (arg) => false;