[TypeScript] Add trailing comma for only arrow functions in tsx. (#6190)
* Modify to add traling comma only for allow-function * Add tests * Update CHANGELOG.unreleased.md * Add pr number and link * Modify to improve RegExp to detect tsx filemaster
parent
4cc99242a8
commit
8812792e93
|
@ -43,3 +43,37 @@ const link = <a href="example.com">http://example.com</a>;
|
|||
```
|
||||
|
||||
-->
|
||||
|
||||
#### TypeScript: Add trailing comma in tsx, only for arrow function ([#6190] by [@sosukesuzuki])
|
||||
|
||||
Prettier inserts a trailing comma to single type parameter for arrow functions in tsx, since v 1.18. But, this feature inserts a trailing comma to type parameter for besides arrow functions too (e.g, function , interface). This change fix it.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
interface Interface1<T> {
|
||||
one: "one";
|
||||
}
|
||||
function function1<T>() {
|
||||
return "one";
|
||||
}
|
||||
|
||||
// Output (Prettier stable)
|
||||
interface Interface1<T,> {
|
||||
one: "one";
|
||||
}
|
||||
function function1<T,>() {
|
||||
return "one";
|
||||
}
|
||||
|
||||
// Output (Prettier master)
|
||||
interface Interface1<T> {
|
||||
one: "one";
|
||||
}
|
||||
function function1<T>() {
|
||||
return "one";
|
||||
}
|
||||
```
|
||||
|
||||
[#6190]: https://github.com/prettier/prettier/pull/6190
|
||||
[@sosukesuzuki]: https://github.com/sosukesuzuki
|
||||
|
|
|
@ -3003,12 +3003,14 @@ function printPathNoParens(path, options, print, args) {
|
|||
// 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.
|
||||
const grandParent = path.getNode(2);
|
||||
if (
|
||||
parent.params &&
|
||||
parent.params.length === 1 &&
|
||||
options.filepath &&
|
||||
options.filepath.match(/\.tsx/) &&
|
||||
!n.constraint
|
||||
/\.tsx$/i.test(options.filepath) &&
|
||||
!n.constraint &&
|
||||
grandParent.type === "ArrowFunctionExpression"
|
||||
) {
|
||||
parts.push(",");
|
||||
}
|
||||
|
|
|
@ -113,11 +113,47 @@ const functionName1 = <T,>(arg) => false;
|
|||
const functionName2 = <T extends any>(arg) => false;
|
||||
const functionName3 = <T, S>(arg) => false;
|
||||
|
||||
function functionName4<T>() {
|
||||
return false;
|
||||
}
|
||||
|
||||
functionName5<T>();
|
||||
|
||||
interface Interface1<T> {
|
||||
one: "one";
|
||||
}
|
||||
|
||||
interface Interface2 {
|
||||
two: Two<T>;
|
||||
}
|
||||
|
||||
type Type1<T> = "type1";
|
||||
|
||||
type Type2 = Two<T>;
|
||||
|
||||
=====================================output=====================================
|
||||
const functionName1 = <T,>(arg) => false;
|
||||
const functionName2 = <T extends any>(arg) => false;
|
||||
const functionName3 = <T, S>(arg) => false;
|
||||
|
||||
function functionName4<T>() {
|
||||
return false;
|
||||
}
|
||||
|
||||
functionName5<T>();
|
||||
|
||||
interface Interface1<T> {
|
||||
one: "one";
|
||||
}
|
||||
|
||||
interface Interface2 {
|
||||
two: Two<T>;
|
||||
}
|
||||
|
||||
type Type1<T> = "type1";
|
||||
|
||||
type Type2 = Two<T>;
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
|
|
|
@ -1,3 +1,21 @@
|
|||
const functionName1 = <T,>(arg) => false;
|
||||
const functionName2 = <T extends any>(arg) => false;
|
||||
const functionName3 = <T, S>(arg) => false;
|
||||
|
||||
function functionName4<T>() {
|
||||
return false;
|
||||
}
|
||||
|
||||
functionName5<T>();
|
||||
|
||||
interface Interface1<T> {
|
||||
one: "one";
|
||||
}
|
||||
|
||||
interface Interface2 {
|
||||
two: Two<T>;
|
||||
}
|
||||
|
||||
type Type1<T> = "type1";
|
||||
|
||||
type Type2 = Two<T>;
|
||||
|
|
Loading…
Reference in New Issue