Keep a trailing comma in type parameter in .tsx files (#6115)

master
Sosuke Suzuki 2019-05-14 02:53:45 +09:00 committed by Lucas Duailibe
parent 7bb32f41f5
commit e3b2f4adb4
4 changed files with 53 additions and 0 deletions

View File

@ -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.
<!-- prettier-ignore -->
```tsx
// Input
type G<T> = any;
const myFunc = <T,>(arg1: G<T>) => false;
// Output (Prettier stable)
type G<T> = any;
const myFunc = <T>(arg1: G<T>) => false;
// Output (prettier master)
type G<T> = any;
const myFunc = <T,>(arg1: G<T>) => false;
```

View File

@ -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":

View File

@ -103,6 +103,24 @@ printWidth: 80
================================================================================
`;
exports[`type-parameters.tsx 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
const functionName1 = <T,>(arg) => false;
const functionName2 = <T extends any>(arg) => false;
const functionName3 = <T, S>(arg) => false;
=====================================output=====================================
const functionName1 = <T,>(arg) => false;
const functionName2 = <T extends any>(arg) => false;
const functionName3 = <T, S>(arg) => false;
================================================================================
`;
exports[`url.tsx 1`] = `
====================================options=====================================
parsers: ["typescript"]

View File

@ -0,0 +1,3 @@
const functionName1 = <T,>(arg) => false;
const functionName2 = <T extends any>(arg) => false;
const functionName3 = <T, S>(arg) => false;