From 0f553baffc7a0ff06f6cffffc943c0e8f7ee142f Mon Sep 17 00:00:00 2001 From: SUZUKI Sosuke Date: Fri, 30 May 2025 18:22:16 +0900 Subject: [PATCH] Don't print extra semicolon after `prettier-ignore`d index-signature (#17538) * Don't print extra semicolon after `prettier-ignore`d index-signature * Update snapshots * Add changelog --- changelog_unreleased/typescript/17538.md | 22 +++++++++++++++++++ src/language-js/print/object.js | 3 ++- .../__snapshots__/format.test.js.snap | 20 +++++++++++++++++ .../typescript/prettier-ignore/issue-16927.ts | 4 ++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/17538.md create mode 100644 tests/format/typescript/prettier-ignore/issue-16927.ts diff --git a/changelog_unreleased/typescript/17538.md b/changelog_unreleased/typescript/17538.md new file mode 100644 index 000000000..f0dd805dd --- /dev/null +++ b/changelog_unreleased/typescript/17538.md @@ -0,0 +1,22 @@ +#### Don't print extra semicolon after `prettier-ignore`d index-signature (#17538 by @sosukesuzuki) + + +```tsx +// Input +type foo = { + // prettier-ignore + [key: string]: bar; +}; + +// Prettier stable +type foo = { + // prettier-ignore + [key: string]: bar;; +}; + +// Prettier main +type foo = { + // prettier-ignore + [key: string]: bar; +}; +``` diff --git a/src/language-js/print/object.js b/src/language-js/print/object.js index d1d788fb4..32fcd58a8 100644 --- a/src/language-js/print/object.js +++ b/src/language-js/print/object.js @@ -159,7 +159,8 @@ function printObject(path, options, print) { ((lastElem.type === "TSPropertySignature" || lastElem.type === "TSCallSignatureDeclaration" || lastElem.type === "TSMethodSignature" || - lastElem.type === "TSConstructSignatureDeclaration") && + lastElem.type === "TSConstructSignatureDeclaration" || + lastElem.type === "TSIndexSignature") && hasComment(lastElem, CommentCheckFlags.PrettierIgnore)))) ); diff --git a/tests/format/typescript/prettier-ignore/__snapshots__/format.test.js.snap b/tests/format/typescript/prettier-ignore/__snapshots__/format.test.js.snap index 574b60950..367e8cf4f 100644 --- a/tests/format/typescript/prettier-ignore/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/prettier-ignore/__snapshots__/format.test.js.snap @@ -18,6 +18,26 @@ export const foo = // prettier-ignore ================================================================================ `; +exports[`issue-16927.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Foo = { + // prettier-ignore + [key: string]: bar; +}; + +=====================================output===================================== +type Foo = { + // prettier-ignore + [key: string]: bar; +}; + +================================================================================ +`; + exports[`mapped-types.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/prettier-ignore/issue-16927.ts b/tests/format/typescript/prettier-ignore/issue-16927.ts new file mode 100644 index 000000000..1ec77ded6 --- /dev/null +++ b/tests/format/typescript/prettier-ignore/issue-16927.ts @@ -0,0 +1,4 @@ +type Foo = { + // prettier-ignore + [key: string]: bar; +};