Keep comment on the same line in property signature (#19688)

This commit is contained in:
Konv Suu authored and GitHub committed 2026-07-22 18:10:36 +08:00
1 parent 5a763f1525
commit f5927d5199
7 files changed
+80 -21

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#### Keep comment on the same line in property signature (#19688 by @kovsu)
<!-- prettier-ignore -->
```tsx
// Input
interface A { property: /** Comment */
B }
// Prettier stable
interface A {
property: /** Comment */
B;
}
// Prettier main
interface A {
property: /** Comment */ B;
}
```
+10 -8
View File
@@ -1,7 +1,7 @@
/** @import {Doc} from "../../document/index.js" */
import * as assert from "#universal/assert";
import { replaceEndOfLine } from "../../document/index.js";
import { group, replaceEndOfLine } from "../../document/index.js";
import printNumber from "../../utilities/print-number.js";
import printString from "../../utilities/print-string.js";
import { getRaw } from "../utilities/get-raw.js";
@@ -261,13 +261,15 @@ function printFlow(path, options, print, args) {
}
return [
modifier,
node.kind !== "init" ? node.kind + " " : "",
node.variance ? print("variance") : "",
printKey(path, options, print),
printOptionalToken(path),
isMethod(node) ? "" : ": ",
print("value"),
group([
modifier,
node.kind !== "init" ? node.kind + " " : "",
node.variance ? print("variance") : "",
printKey(path, options, print),
printOptionalToken(path),
isMethod(node) ? "" : ": ",
print("value"),
]),
printClassMemberSemicolon(path, options),
];
}
+9 -7
View File
@@ -21,13 +21,15 @@ function printIndexSignature(path, options, print) {
const isClassMember = path.key === "body" && path.parent.type === "ClassBody";
return [
// `static` only allowed in class member
isClassMember && node.static ? "static " : "",
node.readonly ? "readonly " : "",
"[",
node.parameters ? parametersGroup : "",
"]",
printTypeAnnotationProperty(path, print),
group([
// `static` only allowed in class member
isClassMember && node.static ? "static " : "",
node.readonly ? "readonly " : "",
"[",
node.parameters ? parametersGroup : "",
"]",
printTypeAnnotationProperty(path, print),
]),
printClassMemberSemicolon(path, options),
];
}
+7 -4
View File
@@ -1,3 +1,4 @@
import { group } from "../../document/index.js";
import UnexpectedNodeError from "../../utilities/unexpected-node-error.js";
import isTsKeywordType from "../utilities/is-ts-keyword-type.js";
import { printArray } from "./array.js";
@@ -111,10 +112,12 @@ function printTypescript(path, options, print, args) {
return printArrayType(print);
case "TSPropertySignature":
return [
node.readonly ? "readonly " : "",
printKey(path, options, print),
printOptionalToken(path),
printTypeAnnotationProperty(path, print),
group([
node.readonly ? "readonly " : "",
printKey(path, options, print),
printOptionalToken(path),
printTypeAnnotationProperty(path, print),
]),
printClassMemberSemicolon(path, options),
];
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`comment.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
interface A {
[key: string]: /** Comment */
B;
}
interface A {
[key: string]: /** Comment */ B;
}
=====================================output=====================================
interface A {
[key: string]: /** Comment */ B;
}
interface A {
[key: string]: /** Comment */ B;
}
================================================================================
`;
exports[`index-signature.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
@@ -0,0 +1,8 @@
interface A {
[key: string]: /** Comment */
B;
}
interface A {
[key: string]: /** Comment */ B;
}
@@ -42,8 +42,7 @@ interface A {
}
interface A {
property: /** Comment */
B;
property: /** Comment */ B;
}
interface A {