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
This commit is contained in:
1 parent
a7e92624b3
commit
f8692f3bde
4 files changed
+125
-1
No files matched your search
@@ -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))
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```ts
|
||||
// Input
|
||||
const a: T</* comment */> = 1;
|
||||
|
||||
// Prettier stable
|
||||
Error: Comment "comment" was not printed. Please report this error!
|
||||
|
||||
// Prettier master
|
||||
const a: T</* comment */> = 1;
|
||||
```
|
||||
@@ -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(
|
||||
|
||||
@@ -663,9 +663,69 @@ printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
functionName<A /* A comment */>();
|
||||
const a: T</* comment */> = 1;
|
||||
functionName</* comment */>();
|
||||
function foo</* comment */>() {}
|
||||
interface Foo {
|
||||
</* comment */>(arg): any;
|
||||
}
|
||||
type T = </* comment */>(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<A /* A comment */>();
|
||||
const a: T</* comment */> = 1;
|
||||
functionName</* comment */>();
|
||||
function foo</* comment */>() {}
|
||||
interface Foo {
|
||||
</* comment */>(arg): any;
|
||||
}
|
||||
type T = </* comment */>(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;
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
@@ -1 +1,29 @@
|
||||
functionName<A /* A comment */>();
|
||||
const a: T</* comment */> = 1;
|
||||
functionName</* comment */>();
|
||||
function foo</* comment */>() {}
|
||||
interface Foo {
|
||||
</* comment */>(arg): any;
|
||||
}
|
||||
type T = </* comment */>(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;
|
||||
Reference in new issue
Block a user