From f8692f3bdee512a1f20da1a5e2841dbd56ce4e4d Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Tue, 10 Mar 2020 23:33:33 +0900 Subject: [PATCH] TypeScript: Print dangling comment in empty type parameters (#7729) * Modify to print dangling comment in type parameters * Add changelog * Fix changelog * Modify to support line comment * Fix changelog * Change line to hardline --- changelog_unreleased/typescript/pr-7729.md | 13 ++++ src/language-js/printer-estree.js | 25 +++++++- .../__snapshots__/jsfmt.spec.js.snap | 60 +++++++++++++++++++ tests/typescript_comments/type-parameters.ts | 28 +++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/pr-7729.md diff --git a/changelog_unreleased/typescript/pr-7729.md b/changelog_unreleased/typescript/pr-7729.md new file mode 100644 index 000000000..6a5f7f97d --- /dev/null +++ b/changelog_unreleased/typescript/pr-7729.md @@ -0,0 +1,13 @@ +#### Fix printing of comments in empty type parameters ([#7729](https://github.com/prettier/prettier/pull/7729) by [@sosukesuzuki](https://github.com/sosukesuzuki)) + + +```ts +// Input +const a: T = 1; + +// Prettier stable +Error: Comment "comment" was not printed. Please report this error! + +// Prettier master +const a: T = 1; +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 8ee3608f1..9fefddb39 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -4638,8 +4638,31 @@ function printTypeParameters(path, options, print, paramsKey) { n[paramsKey][0].type !== "TSIndexedAccessType" && n[paramsKey][0].type !== "TSArrayType"))); + function printDanglingCommentsForInline(n) { + if (!hasDanglingComments(n)) { + return ""; + } + const hasOnlyBlockComments = n.comments.every( + handleComments.isBlockComment + ); + const printed = comments.printDanglingComments( + path, + options, + /* sameIndent */ hasOnlyBlockComments + ); + if (hasOnlyBlockComments) { + return printed; + } + return concat([printed, hardline]); + } + if (shouldInline) { - return concat(["<", join(", ", path.map(print, paramsKey)), ">"]); + return concat([ + "<", + join(", ", path.map(print, paramsKey)), + printDanglingCommentsForInline(n), + ">" + ]); } return group( diff --git a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap index d0a344660..1282449f9 100644 --- a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap @@ -663,9 +663,69 @@ printWidth: 80 | printWidth =====================================input====================================== functionName(); +const a: T = 1; +functionName(); +function foo() {} +interface Foo { + (arg): any; +} +type T = (arg) => any; + +functionName< + A // comment +>(); +const a: T< + // comment +> = 1; +functionName< + // comment +>(); +function foo< + // comment +>() {} +interface Foo { + < + A// comment + >(arg): any; +} +type T = < + // comment +>(arg) => any; =====================================output===================================== functionName(); +const a: T = 1; +functionName(); +function foo() {} +interface Foo { + (arg): any; +} +type T = (arg) => any; + +functionName< + A // comment +>(); +const a: T< + // comment +> = 1; +functionName< + // comment +>(); +function foo< + // comment +>() {} +interface Foo { + < + A // comment + >( + arg + ): any; +} +type T = < + // comment +>( + arg +) => any; ================================================================================ `; diff --git a/tests/typescript_comments/type-parameters.ts b/tests/typescript_comments/type-parameters.ts index e993143af..42990a4ea 100644 --- a/tests/typescript_comments/type-parameters.ts +++ b/tests/typescript_comments/type-parameters.ts @@ -1 +1,29 @@ functionName(); +const a: T = 1; +functionName(); +function foo() {} +interface Foo { + (arg): any; +} +type T = (arg) => any; + +functionName< + A // comment +>(); +const a: T< + // comment +> = 1; +functionName< + // comment +>(); +function foo< + // comment +>() {} +interface Foo { + < + A// comment + >(arg): any; +} +type T = < + // comment +>(arg) => any;