From 7a76a9b1192557eb1d7fd66f42679bfc9bdcc64a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 13 Jan 2026 02:18:04 +0800 Subject: [PATCH] Fix GraphQL dangling comment print (#18581) --- changelog_unreleased/graphql/18581.md | 27 ++++++++++++++++++ src/language-graphql/printer-graphql.js | 14 +++++++--- .../__snapshots__/format.test.js.snap | 28 +++++++++++++++++++ .../format/graphql/comments/dangling.graphql | 7 +++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 changelog_unreleased/graphql/18581.md create mode 100644 tests/format/graphql/comments/dangling.graphql diff --git a/changelog_unreleased/graphql/18581.md b/changelog_unreleased/graphql/18581.md new file mode 100644 index 000000000..e1a1b1ac7 --- /dev/null +++ b/changelog_unreleased/graphql/18581.md @@ -0,0 +1,27 @@ +#### Fix comment print (#18581 by @fisker) + + +```gql +# Input +query { + search(filters: { + # TODO + }) { + id + } +} + +# Prettier stable +Error: Comment "TODO" was not printed. Please report this error! + +# Prettier main +query { + search( + filters: { + # TODO + } + ) { + id + } +} +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 67d0c4058..e689764aa 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -7,6 +7,7 @@ import { line, softline, } from "../document/index.js"; +import { printDanglingComments } from "../main/comments/print.js"; import isNextLineEmpty from "../utilities/is-next-line-empty.js"; import isNonEmptyArray from "../utilities/is-non-empty-array.js"; import UnexpectedNodeError from "../utilities/unexpected-node-error.js"; @@ -145,10 +146,15 @@ function genericPrint(path, options, print) { return group([ "{", bracketSpace, - indent([ - softline, - join([ifBreak("", ", "), softline], path.map(print, "fields")), - ]), + printDanglingComments(path, options, { indent: true }), + isNonEmptyArray(node.fields) + ? [ + indent([ + softline, + join([ifBreak("", ", "), softline], path.map(print, "fields")), + ]), + ] + : "", softline, ifBreak("", bracketSpace), "}", diff --git a/tests/format/graphql/comments/__snapshots__/format.test.js.snap b/tests/format/graphql/comments/__snapshots__/format.test.js.snap index cefdcd3d6..dc8a4311d 100644 --- a/tests/format/graphql/comments/__snapshots__/format.test.js.snap +++ b/tests/format/graphql/comments/__snapshots__/format.test.js.snap @@ -25,6 +25,34 @@ query ( ================================================================================ `; +exports[`dangling.graphql format 1`] = ` +====================================options===================================== +parsers: ["graphql"] +printWidth: 80 + | printWidth +=====================================input====================================== +query { + search(filters: { + # TODO + }) { + id + } +} + +=====================================output===================================== +query { + search( + filters: { + # TODO + } + ) { + id + } +} + +================================================================================ +`; + exports[`fields.graphql format 1`] = ` ====================================options===================================== parsers: ["graphql"] diff --git a/tests/format/graphql/comments/dangling.graphql b/tests/format/graphql/comments/dangling.graphql new file mode 100644 index 000000000..4d1b618ba --- /dev/null +++ b/tests/format/graphql/comments/dangling.graphql @@ -0,0 +1,7 @@ +query { + search(filters: { + # TODO + }) { + id + } +}