From a14c44f30ea7741aaededf6c6d182303bc08405b Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Thu, 27 Nov 2025 14:23:14 +0800 Subject: [PATCH] Fix `K3WX.yaml` (comments in map key) bug in `yaml-test-suite` (#18324) Co-authored-by: fisker Cheung --- changelog_unreleased/yaml/18324.md | 15 ++++++ src/language-yaml/print/mapping-item.js | 1 + .../__snapshots__/format.test.js.snap | 53 +++++++++++++++++++ .../yaml/flow-mapping/comments/format.test.js | 1 + .../format/yaml/flow-mapping/comments/key.yml | 20 +++++++ .../__snapshots__/format.test.js.snap | 18 +++++++ .../yaml/yaml-test-suite/format.test.js | 1 - 7 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/yaml/18324.md create mode 100644 tests/format/yaml/flow-mapping/comments/__snapshots__/format.test.js.snap create mode 100644 tests/format/yaml/flow-mapping/comments/format.test.js create mode 100644 tests/format/yaml/flow-mapping/comments/key.yml diff --git a/changelog_unreleased/yaml/18324.md b/changelog_unreleased/yaml/18324.md new file mode 100644 index 000000000..1736d6afc --- /dev/null +++ b/changelog_unreleased/yaml/18324.md @@ -0,0 +1,15 @@ +#### Use explicit key style for flow-mapping keys with trailing comments (#18324 by @kovsu, @fisker) + + +```yaml +# Input +{ "foo" # comment + :bar } + +# Prettier stable +{ "foo": bar } # comment + +# Prettier main +{ ? "foo" # comment + : bar } +``` diff --git a/src/language-yaml/print/mapping-item.js b/src/language-yaml/print/mapping-item.js index 233816c50..3eb1645c2 100644 --- a/src/language-yaml/print/mapping-item.js +++ b/src/language-yaml/print/mapping-item.js @@ -135,6 +135,7 @@ function printMappingItem(path, options, print) { isAbsolutelyPrintedAsSingleLineNode(key.content, options) && !hasLeadingComments(key.content) && !hasMiddleComments(key.content) && + !hasTrailingComment(key.content) && !hasEndComments(key) ) { return conditionalGroup([[printedKey, implicitMappingValue]]); diff --git a/tests/format/yaml/flow-mapping/comments/__snapshots__/format.test.js.snap b/tests/format/yaml/flow-mapping/comments/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..4d86e19d7 --- /dev/null +++ b/tests/format/yaml/flow-mapping/comments/__snapshots__/format.test.js.snap @@ -0,0 +1,53 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`key.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +{"key" # 1 +:value} +--- +{["key"] # 2 +:value} +--- +{ +# 3 +"key" +:value} +--- +{ +&[ +"key", +# 4 +""] +:value} +--- +{"key": # 1 +value} + +=====================================output===================================== +{ ? "key" # 1 + : value } +--- +{ ? ["key"] # 2 + : value } +--- +{ + # 3 + "key": value, +} +--- +{ ? & [ + "key", + # 4 + "", + ] + : value } +--- +{ ? "key" # 1 + : value } + +================================================================================ +`; diff --git a/tests/format/yaml/flow-mapping/comments/format.test.js b/tests/format/yaml/flow-mapping/comments/format.test.js new file mode 100644 index 000000000..49e3f4bbf --- /dev/null +++ b/tests/format/yaml/flow-mapping/comments/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["yaml"]); diff --git a/tests/format/yaml/flow-mapping/comments/key.yml b/tests/format/yaml/flow-mapping/comments/key.yml new file mode 100644 index 000000000..21530621c --- /dev/null +++ b/tests/format/yaml/flow-mapping/comments/key.yml @@ -0,0 +1,20 @@ +{"key" # 1 +:value} +--- +{["key"] # 2 +:value} +--- +{ +# 3 +"key" +:value} +--- +{ +&[ +"key", +# 4 +""] +:value} +--- +{"key": # 1 +value} diff --git a/tests/format/yaml/yaml-test-suite/__snapshots__/format.test.js.snap b/tests/format/yaml/yaml-test-suite/__snapshots__/format.test.js.snap index 6a9618e22..d1bd229ee 100644 --- a/tests/format/yaml/yaml-test-suite/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/yaml-test-suite/__snapshots__/format.test.js.snap @@ -3847,6 +3847,24 @@ printWidth: 80 ================================================================================ `; +exports[`snippet: K3WX.yaml - Colon and adjacent value after comment on next line format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +--- +{ "foo" # comment + :bar } + +=====================================output===================================== +--- +{ ? "foo" # comment + : bar } + +================================================================================ +`; + exports[`snippet: K4SU.yaml - Multiple Entry Block Sequence format 1`] = ` ====================================options===================================== parsers: ["yaml"] diff --git a/tests/format/yaml/yaml-test-suite/format.test.js b/tests/format/yaml/yaml-test-suite/format.test.js index 872151321..c51396b85 100644 --- a/tests/format/yaml/yaml-test-suite/format.test.js +++ b/tests/format/yaml/yaml-test-suite/format.test.js @@ -20,7 +20,6 @@ const BUGS = new Set([ "DE56-4.yaml", "JEF9-2.yaml", "JEF9-3.yaml", - "K3WX.yaml", "L24T-2.yaml", ]);