diff --git a/changelog_unreleased/typescript/19565.md b/changelog_unreleased/typescript/19565.md new file mode 100644 index 000000000..94d717f15 --- /dev/null +++ b/changelog_unreleased/typescript/19565.md @@ -0,0 +1,15 @@ +#### Fix comments being dropped on shorthand `type` import/export specifiers (#19565 by @kirkwaiblinger) + + +```tsx +// Input +export { type /* comment */ T } from "foo"; +import { type /* comment */ T } from "foo"; + +// Prettier stable +Error: Comment "comment" was not printed. Please report this error! + +// Prettier main +export { type /* comment */ T } from "foo"; +import { type /* comment */ T } from "foo"; +``` diff --git a/src/language-js/comments/can-attach-comment.js b/src/language-js/comments/can-attach-comment.js index a2abbacac..28a38e688 100644 --- a/src/language-js/comments/can-attach-comment.js +++ b/src/language-js/comments/can-attach-comment.js @@ -51,11 +51,11 @@ const isChildWontPrint = (node, [parent]) => parent.value !== parent.key) || (parent?.type === "ImportSpecifier" && isShorthandSpecifier(parent) && - parent.imported === node && - parent.imported !== parent.local) || + parent.local === node && + parent.local !== parent.imported) || (parent?.type === "ExportSpecifier" && isShorthandSpecifier(parent) && - parent.local === node && + parent.exported === node && parent.local !== parent.exported); /** diff --git a/tests/format/typescript/import-export/__snapshots__/format.test.js.snap b/tests/format/typescript/import-export/__snapshots__/format.test.js.snap index 34935c13b..ef245142d 100644 --- a/tests/format/typescript/import-export/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/import-export/__snapshots__/format.test.js.snap @@ -116,6 +116,16 @@ import type { foo as bar } from 'baz'; import type * as foo from './bar'; import type foo from 'bar'; +// https://github.com/prettier/prettier/issues/19564 +export { type /* comment */ T } from 'foo'; +import { type /* comment */ T } from 'foo'; + +export { /* e1 */ type /* e2 */ T /* e3 */ } from 'foo'; +import { /* i1 */ type /* i2 */ T /* i3 */ } from 'foo'; + +export { /* e1 */ type /* e2 */ T /* e3 */ as /* e4 */ U /* e5 */ } from 'foo'; +import { /* i1 */ type /* i2 */ T /* i3 */ as /* i4 */ U /* i5 */ } from 'foo'; + =====================================output===================================== export type { SomeThing }; export type { A as B }; @@ -132,5 +142,15 @@ import type { foo as bar } from "baz"; import type * as foo from "./bar"; import type foo from "bar"; +// https://github.com/prettier/prettier/issues/19564 +export { type /* comment */ T } from "foo"; +import { type /* comment */ T } from "foo"; + +export { /* e1 */ type /* e2 */ T /* e3 */ } from "foo"; +import { /* i1 */ type /* i2 */ T /* i3 */ } from "foo"; + +export { /* e1 */ type /* e2 */ T /* e3 */ as /* e4 */ U /* e5 */ } from "foo"; +import { /* i1 */ type /* i2 */ T /* i3 */ as /* i4 */ U /* i5 */ } from "foo"; + ================================================================================ `; diff --git a/tests/format/typescript/import-export/type-modifier.ts b/tests/format/typescript/import-export/type-modifier.ts index cb24bb22b..072105c55 100644 --- a/tests/format/typescript/import-export/type-modifier.ts +++ b/tests/format/typescript/import-export/type-modifier.ts @@ -12,3 +12,13 @@ import type { foo, bar } from 'baz'; import type { foo as bar } from 'baz'; import type * as foo from './bar'; import type foo from 'bar'; + +// https://github.com/prettier/prettier/issues/19564 +export { type /* comment */ T } from 'foo'; +import { type /* comment */ T } from 'foo'; + +export { /* e1 */ type /* e2 */ T /* e3 */ } from 'foo'; +import { /* i1 */ type /* i2 */ T /* i3 */ } from 'foo'; + +export { /* e1 */ type /* e2 */ T /* e3 */ as /* e4 */ U /* e5 */ } from 'foo'; +import { /* i1 */ type /* i2 */ T /* i3 */ as /* i4 */ U /* i5 */ } from 'foo';