diff --git a/changelog_unreleased/typescript/19688.md b/changelog_unreleased/typescript/19688.md new file mode 100644 index 000000000..307e6b85c --- /dev/null +++ b/changelog_unreleased/typescript/19688.md @@ -0,0 +1,19 @@ +#### Keep comment on the same line in property signature (#19688 by @kovsu) + + +```tsx +// Input +interface A { property: /** Comment */ +B } + +// Prettier stable +interface A { + property: /** Comment */ + B; +} + +// Prettier main +interface A { + property: /** Comment */ B; +} +``` diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index 9447bfd6e..020c827c7 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -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), ]; } diff --git a/src/language-js/print/index-signature.js b/src/language-js/print/index-signature.js index 98e0ff23f..c6436bc9e 100644 --- a/src/language-js/print/index-signature.js +++ b/src/language-js/print/index-signature.js @@ -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), ]; } diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 650ff7386..9bcb864f3 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -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), ]; diff --git a/tests/format/typescript/index-signature/__snapshots__/format.test.js.snap b/tests/format/typescript/index-signature/__snapshots__/format.test.js.snap index 33058b1c4..9b81ce590 100644 --- a/tests/format/typescript/index-signature/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/index-signature/__snapshots__/format.test.js.snap @@ -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"] diff --git a/tests/format/typescript/index-signature/comment.ts b/tests/format/typescript/index-signature/comment.ts new file mode 100644 index 000000000..31f36179d --- /dev/null +++ b/tests/format/typescript/index-signature/comment.ts @@ -0,0 +1,8 @@ +interface A { + [key: string]: /** Comment */ + B; +} + +interface A { + [key: string]: /** Comment */ B; +} diff --git a/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap index e7036273d..3ae1d876b 100644 --- a/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap @@ -42,8 +42,7 @@ interface A { } interface A { - property: /** Comment */ - B; + property: /** Comment */ B; } interface A {