Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeScript] Add trailing comma for only arrow functions in tsx. #6190

Merged
merged 5 commits into from Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>;