From b7b2b0d4348a758e5f37c226bd26edf0d667fba8 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 23 Sep 2025 17:51:36 +0800 Subject: [PATCH] Print trailing comma in `TSImportType` options (#17798) --- changelog_unreleased/typescript/17798.md | 27 ++++++++++++++ src/language-js/print/object.js | 7 +--- .../comma/__snapshots__/format.test.js.snap | 37 +++++++++++++++++++ .../typescript/import-type/comma/comma.ts | 10 +++++ .../import-type/comma/format.test.js | 1 + .../__snapshots__/format.test.js.snap | 4 +- 6 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 changelog_unreleased/typescript/17798.md create mode 100644 tests/format/typescript/import-type/comma/__snapshots__/format.test.js.snap create mode 100644 tests/format/typescript/import-type/comma/comma.ts create mode 100644 tests/format/typescript/import-type/comma/format.test.js diff --git a/changelog_unreleased/typescript/17798.md b/changelog_unreleased/typescript/17798.md new file mode 100644 index 000000000..d1aff05f6 --- /dev/null +++ b/changelog_unreleased/typescript/17798.md @@ -0,0 +1,27 @@ +#### Print trailing comma in `TSImportType` options (#17798 by @fisker) + +Trailing comma in import type attribute wasn't allowed before TypeScript v5.9, now [the bug](https://github.com/microsoft/TypeScript/issues/61916) has been fixed. + + +```tsx +// Input +type A = import("foo", { + with:{ + type:'json', + } // <- Should be a comma here +}) + +// Prettier stable +type A = import("foo", { + with: { + type: "json", + } // <- Should be a comma here +}); + +// Prettier main +type A = import("foo", { + with: { + type: "json", + }, // <- Should be a comma here +}); +``` diff --git a/src/language-js/print/object.js b/src/language-js/print/object.js index e04b2ee20..32fcd58a8 100644 --- a/src/language-js/print/object.js +++ b/src/language-js/print/object.js @@ -161,12 +161,7 @@ function printObject(path, options, print) { lastElem.type === "TSMethodSignature" || lastElem.type === "TSConstructSignatureDeclaration" || lastElem.type === "TSIndexSignature") && - hasComment(lastElem, CommentCheckFlags.PrettierIgnore)))) || - // https://github.com/microsoft/TypeScript/issues/61916 - path.match( - undefined, - (node, key) => node.type === "TSImportType" && key === "options", - ) + hasComment(lastElem, CommentCheckFlags.PrettierIgnore)))) ); let content; diff --git a/tests/format/typescript/import-type/comma/__snapshots__/format.test.js.snap b/tests/format/typescript/import-type/comma/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..7a50b1102 --- /dev/null +++ b/tests/format/typescript/import-type/comma/__snapshots__/format.test.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`comma.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A = import("foo", { + with: { + type: "json",}}) +type A = import("foo", { + with: { + type: "json"},}) +// Not supported, https://github.com/microsoft/TypeScript/issues/61489 +// type A = import("foo", { +// with: { +// type: "json"}},) + +=====================================output===================================== +type A = import("foo", { + with: { + type: "json", + }, +}); +type A = import("foo", { + with: { + type: "json", + }, +}); +// Not supported, https://github.com/microsoft/TypeScript/issues/61489 +// type A = import("foo", { +// with: { +// type: "json"}},) + +================================================================================ +`; diff --git a/tests/format/typescript/import-type/comma/comma.ts b/tests/format/typescript/import-type/comma/comma.ts new file mode 100644 index 000000000..3c90a1324 --- /dev/null +++ b/tests/format/typescript/import-type/comma/comma.ts @@ -0,0 +1,10 @@ +type A = import("foo", { + with: { + type: "json",}}) +type A = import("foo", { + with: { + type: "json"},}) +// Not supported, https://github.com/microsoft/TypeScript/issues/61489 +// type A = import("foo", { +// with: { +// type: "json"}},) diff --git a/tests/format/typescript/import-type/comma/format.test.js b/tests/format/typescript/import-type/comma/format.test.js new file mode 100644 index 000000000..116dcbd78 --- /dev/null +++ b/tests/format/typescript/import-type/comma/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["typescript"]); diff --git a/tests/format/typescript/import-type/long-module-name/__snapshots__/format.test.js.snap b/tests/format/typescript/import-type/long-module-name/__snapshots__/format.test.js.snap index 20d11c7d3..e9d610445 100644 --- a/tests/format/typescript/import-type/long-module-name/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/import-type/long-module-name/__snapshots__/format.test.js.snap @@ -147,13 +147,13 @@ type C = import( { with: { type: "json", - } + }, } ); type D = import( "./long/long/long/long/long/long/long/long/long/long/path/to/module", { - with: { type: "json" } + with: { type: "json" }, } );