Skip to content

Commit

Permalink
[TypeScript] Add trailing comma for only arrow functions in tsx. (#6190)
Browse files Browse the repository at this point in the history
* 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 file
  • Loading branch information
sosukesuzuki authored and duailibe committed Jun 7, 2019
1 parent 4cc9924 commit 8812792
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/language-js/printer-estree.js
Expand Up @@ -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(",");
}
Expand Down
36 changes: 36 additions & 0 deletions tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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>;
================================================================================
`;
Expand Down
18 changes: 18 additions & 0 deletions tests/typescript_tsx/type-parameters.tsx
@@ -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>;

0 comments on commit 8812792

Please sign in to comment.