From 351e399362b087291f92c39ce5c37d679a5d7f3c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 19 Jul 2026 10:20:39 +0800 Subject: [PATCH] YAML: Preserve order of anchor and tag (#19599) --- changelog_unreleased/yaml/19599.md | 16 +++++++++++ src/language-yaml/printer-yaml.js | 23 +++++++++------ .../spec/__snapshots__/format.test.js.snap | 28 +++++++++---------- .../__snapshots__/format.test.js.snap | 14 +++++----- 4 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 changelog_unreleased/yaml/19599.md diff --git a/changelog_unreleased/yaml/19599.md b/changelog_unreleased/yaml/19599.md new file mode 100644 index 000000000..27cc3e5fc --- /dev/null +++ b/changelog_unreleased/yaml/19599.md @@ -0,0 +1,16 @@ +#### Preserve order of anchor and tag (#19599 by @fisker) + + +```yaml +# Input +foo: &anchor1 !!str Value1 +bar: !!str &anchor2 Value2 + +# Prettier stable +foo: !!str &anchor1 Value1 +bar: !!str &anchor2 Value2 + +# Prettier main +foo: &anchor1 !!str Value1 +bar: !!str &anchor2 Value2 +``` diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 84da9b1ba..0e0d69b1e 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -52,15 +52,20 @@ function genericPrint(path, options, print) { parts.push([join(hardline, path.map(print, "leadingComments")), hardline]); } - const { tag, anchor } = node; - if (tag) { - parts.push(print("tag")); + const tagAndAnchor = ["tag", "anchor"].filter((property) => node[property]); + if (tagAndAnchor.length > 1) { + tagAndAnchor.sort( + (propertyA, propertyB) => + locStart(node[propertyA]) - locStart(node[propertyB]), + ); } - if (tag && anchor) { - parts.push(" "); - } - if (anchor) { - parts.push(print("anchor")); + + for (const [index, property] of tagAndAnchor.entries()) { + if (index > 0) { + parts.push(" "); + } + + parts.push(print(property)); } /** @type {Doc} */ @@ -80,7 +85,7 @@ function genericPrint(path, options, print) { nextEmptyLine = printNextEmptyLine(path, options.originalText); } - if (tag || anchor) { + if (tagAndAnchor.length > 0) { if (isNode(node, ["sequence", "mapping"]) && !hasMiddleComments(node)) { parts.push(hardline); } else { diff --git a/tests/format/yaml/spec/__snapshots__/format.test.js.snap b/tests/format/yaml/spec/__snapshots__/format.test.js.snap index 2c9286ae5..9f537a26a 100644 --- a/tests/format/yaml/spec/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/spec/__snapshots__/format.test.js.snap @@ -312,7 +312,7 @@ proseWrap: "always" - &d d =====================================output===================================== -- !!str &a a +- &a !!str a - !!int 2 - !!int &c 4 - &d d @@ -332,7 +332,7 @@ useTabs: true - &d d =====================================output===================================== -- !!str &a a +- &a !!str a - !!int 2 - !!int &c 4 - &d d @@ -2139,7 +2139,7 @@ key: &anchor a: b =====================================output===================================== -key: !!map &anchor +key: &anchor !!map a: b ================================================================================ @@ -2156,7 +2156,7 @@ key: &anchor a: b =====================================output===================================== -key: !!map &anchor +key: &anchor !!map a: b ================================================================================ @@ -7840,20 +7840,20 @@ value11 =====================================output===================================== --- -!!str &a1 scalar1 +&a1 !!str scalar1 --- !!str &a2 scalar2 --- -!!str &a3 scalar3 +&a3 !!str scalar3 --- -!!map &a4 -!!str &a5 key5: value4 +&a4 !!map +&a5 !!str key5: value4 --- a6: 1 &anchor6 b6: 2 --- !!map -!!str &a8 key8: value7 +&a8 !!str key8: value7 --- !!map !!str &a10 key10: value9 @@ -7898,20 +7898,20 @@ value11 =====================================output===================================== --- -!!str &a1 scalar1 +&a1 !!str scalar1 --- !!str &a2 scalar2 --- -!!str &a3 scalar3 +&a3 !!str scalar3 --- -!!map &a4 -!!str &a5 key5: value4 +&a4 !!map +&a5 !!str key5: value4 --- a6: 1 &anchor6 b6: 2 --- !!map -!!str &a8 key8: value7 +&a8 !!str key8: value7 --- !!map !!str &a10 key10: value9 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 dfd9bc8e8..cdd573166 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 @@ -1592,20 +1592,20 @@ value11 =====================================output===================================== --- -!!str &a1 scalar1 +&a1 !!str scalar1 --- !!str &a2 scalar2 --- -!!str &a3 scalar3 +&a3 !!str scalar3 --- -!!map &a4 -!!str &a5 key5: value4 +&a4 !!map +&a5 !!str key5: value4 --- a6: 1 &anchor6 b6: 2 --- !!map -!!str &a8 key8: value7 +&a8 !!str key8: value7 --- !!map !!str &a10 key10: value9 @@ -2420,7 +2420,7 @@ key: &anchor a: b =====================================output===================================== -key: !!map &anchor +key: &anchor !!map a: b ================================================================================ @@ -3007,7 +3007,7 @@ parsers: ["yaml"] - &d d =====================================output===================================== -- !!str &a a +- &a !!str a - !!int 2 - !!int &c 4 - &d d