diff --git a/changelog_unreleased/markdown/15286.md b/changelog_unreleased/markdown/15286.md new file mode 100644 index 000000000..fd7b82f12 --- /dev/null +++ b/changelog_unreleased/markdown/15286.md @@ -0,0 +1,19 @@ +#### Do not print trailing commas in arrow function type parameter lists in `ts` code blocks (#15286 by @sosukesuzuki) + + +````md + +```ts +const foo = () => {} +``` + + +```ts +const foo = () => {} +``` + + +```ts +const foo = () => {} +``` +```` diff --git a/src/language-js/parse/typescript.js b/src/language-js/parse/typescript.js index fd1b3d7da..c0773a3c8 100644 --- a/src/language-js/parse/typescript.js +++ b/src/language-js/parse/typescript.js @@ -40,7 +40,7 @@ function createParseError(error) { }); } -// https://typescript-eslint.io/architecture/parser/#jsx +// https://typescript-eslint.io/packages/parser/#jsx const isKnownFileType = (filepath) => /\.(?:js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(filepath); diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index 2c4147639..8eba48472 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -17,7 +17,14 @@ function embed(path, options) { Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1), ); const newOptions = { parser }; - if (node.lang === "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 === "typescript") { + newOptions.filepath = "dummy.ts"; + } else if (node.lang === "tsx") { newOptions.filepath = "dummy.tsx"; } diff --git a/tests/format/markdown/code/__snapshots__/jsfmt.spec.js.snap b/tests/format/markdown/code/__snapshots__/jsfmt.spec.js.snap index 74bd74591..a8b6684a9 100644 --- a/tests/format/markdown/code/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/markdown/code/__snapshots__/jsfmt.spec.js.snap @@ -291,6 +291,33 @@ hello world ================================================================================ `; +exports[`ts-trailing-comma.md - {"proseWrap":"always"} format 1`] = ` +====================================options===================================== +parsers: ["markdown"] +printWidth: 80 +proseWrap: "always" + | printWidth +=====================================input====================================== +\`\`\`ts +const test = (value: T) => {}; +\`\`\` + +\`\`\`typescript +const test = (value: T) => {}; +\`\`\` + +=====================================output===================================== +\`\`\`ts +const test = (value: T) => {}; +\`\`\` + +\`\`\`typescript +const test = (value: T) => {}; +\`\`\` + +================================================================================ +`; + exports[`tsx-trailing-comma.md - {"proseWrap":"always"} format 1`] = ` ====================================options===================================== parsers: ["markdown"] diff --git a/tests/format/markdown/code/ts-trailing-comma.md b/tests/format/markdown/code/ts-trailing-comma.md new file mode 100644 index 000000000..26d195597 --- /dev/null +++ b/tests/format/markdown/code/ts-trailing-comma.md @@ -0,0 +1,7 @@ +```ts +const test = (value: T) => {}; +``` + +```typescript +const test = (value: T) => {}; +```