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

Do not print trailing commas in arrow function type parameter lists in ts code blocks #15286

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions changelog_unreleased/markdown/15286.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#### Do not print trailing commas in arrow function type parameter lists in `ts` code blocks (#15286 by @sosukesuzuki)

<!-- prettier-ignore -->
````md
<!-- Input -->
```ts
const foo = <T>() => {}
```

<!-- Prettier stable -->
```ts
const foo = <T,>() => {}
```

<!-- Prettier main -->
```ts
const foo = <T>() => {}
```
````
9 changes: 7 additions & 2 deletions src/language-markdown/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ function embed(path, options) {
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
);
const newOptions = { parser };
if (node.lang === "tsx") {
newOptions.filepath = "dummy.tsx";

// Override the filepath option.
// This is because whether the trailing comma of type parameters
// should be printed depends on whether it is `*.ts` or `*.tsx`.
// https://github.com/prettier/prettier/issues/15282
if (node.lang === "ts" || node.lang === "tsx") {
newOptions.filepath = `dummy.${node.lang}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should we print this?

```typescript
const foo = <T>() => {}
```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for ts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

const doc = await textToDoc(
Expand Down
19 changes: 19 additions & 0 deletions tests/format/markdown/code/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,25 @@ hello world
================================================================================
`;

exports[`ts-trailing-comma.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`\`\`ts
const test = <T,>(value: T) => {};
\`\`\`

=====================================output=====================================
\`\`\`ts
const test = <T>(value: T) => {};
\`\`\`

================================================================================
`;

exports[`tsx-trailing-comma.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand Down
3 changes: 3 additions & 0 deletions tests/format/markdown/code/ts-trailing-comma.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ts
const test = <T,>(value: T) => {};
```