YAML: Preserve order of anchor and tag (#19599)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-07-19 10:20:39 +08:00
1 parent b35943fa92
commit 351e399362
4 files changed
+51 -30

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#### Preserve order of anchor and tag (#19599 by @fisker)
<!-- prettier-ignore -->
```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
```
+14 -9
View File
@@ -52,15 +52,20 @@ function genericPrint(path, options, print) {
parts.push([join(hardline, path.map(print, "leadingComments")), hardline]); parts.push([join(hardline, path.map(print, "leadingComments")), hardline]);
} }
const { tag, anchor } = node; const tagAndAnchor = ["tag", "anchor"].filter((property) => node[property]);
if (tag) { if (tagAndAnchor.length > 1) {
parts.push(print("tag")); tagAndAnchor.sort(
(propertyA, propertyB) =>
locStart(node[propertyA]) - locStart(node[propertyB]),
);
} }
if (tag && anchor) {
parts.push(" "); for (const [index, property] of tagAndAnchor.entries()) {
} if (index > 0) {
if (anchor) { parts.push(" ");
parts.push(print("anchor")); }
parts.push(print(property));
} }
/** @type {Doc} */ /** @type {Doc} */
@@ -80,7 +85,7 @@ function genericPrint(path, options, print) {
nextEmptyLine = printNextEmptyLine(path, options.originalText); nextEmptyLine = printNextEmptyLine(path, options.originalText);
} }
if (tag || anchor) { if (tagAndAnchor.length > 0) {
if (isNode(node, ["sequence", "mapping"]) && !hasMiddleComments(node)) { if (isNode(node, ["sequence", "mapping"]) && !hasMiddleComments(node)) {
parts.push(hardline); parts.push(hardline);
} else { } else {
@@ -312,7 +312,7 @@ proseWrap: "always"
- &d d - &d d
=====================================output===================================== =====================================output=====================================
- !!str &a a - &a !!str a
- !!int 2 - !!int 2
- !!int &c 4 - !!int &c 4
- &d d - &d d
@@ -332,7 +332,7 @@ useTabs: true
- &d d - &d d
=====================================output===================================== =====================================output=====================================
- !!str &a a - &a !!str a
- !!int 2 - !!int 2
- !!int &c 4 - !!int &c 4
- &d d - &d d
@@ -2139,7 +2139,7 @@ key: &anchor
a: b a: b
=====================================output===================================== =====================================output=====================================
key: !!map &anchor key: &anchor !!map
a: b a: b
================================================================================ ================================================================================
@@ -2156,7 +2156,7 @@ key: &anchor
a: b a: b
=====================================output===================================== =====================================output=====================================
key: !!map &anchor key: &anchor !!map
a: b a: b
================================================================================ ================================================================================
@@ -7840,20 +7840,20 @@ value11
=====================================output===================================== =====================================output=====================================
--- ---
!!str &a1 scalar1 &a1 !!str scalar1
--- ---
!!str &a2 scalar2 !!str &a2 scalar2
--- ---
!!str &a3 scalar3 &a3 !!str scalar3
--- ---
!!map &a4 &a4 !!map
!!str &a5 key5: value4 &a5 !!str key5: value4
--- ---
a6: 1 a6: 1
&anchor6 b6: 2 &anchor6 b6: 2
--- ---
!!map !!map
!!str &a8 key8: value7 &a8 !!str key8: value7
--- ---
!!map !!map
!!str &a10 key10: value9 !!str &a10 key10: value9
@@ -7898,20 +7898,20 @@ value11
=====================================output===================================== =====================================output=====================================
--- ---
!!str &a1 scalar1 &a1 !!str scalar1
--- ---
!!str &a2 scalar2 !!str &a2 scalar2
--- ---
!!str &a3 scalar3 &a3 !!str scalar3
--- ---
!!map &a4 &a4 !!map
!!str &a5 key5: value4 &a5 !!str key5: value4
--- ---
a6: 1 a6: 1
&anchor6 b6: 2 &anchor6 b6: 2
--- ---
!!map !!map
!!str &a8 key8: value7 &a8 !!str key8: value7
--- ---
!!map !!map
!!str &a10 key10: value9 !!str &a10 key10: value9
@@ -1592,20 +1592,20 @@ value11
=====================================output===================================== =====================================output=====================================
--- ---
!!str &a1 scalar1 &a1 !!str scalar1
--- ---
!!str &a2 scalar2 !!str &a2 scalar2
--- ---
!!str &a3 scalar3 &a3 !!str scalar3
--- ---
!!map &a4 &a4 !!map
!!str &a5 key5: value4 &a5 !!str key5: value4
--- ---
a6: 1 a6: 1
&anchor6 b6: 2 &anchor6 b6: 2
--- ---
!!map !!map
!!str &a8 key8: value7 &a8 !!str key8: value7
--- ---
!!map !!map
!!str &a10 key10: value9 !!str &a10 key10: value9
@@ -2420,7 +2420,7 @@ key: &anchor
a: b a: b
=====================================output===================================== =====================================output=====================================
key: !!map &anchor key: &anchor !!map
a: b a: b
================================================================================ ================================================================================
@@ -3007,7 +3007,7 @@ parsers: ["yaml"]
- &d d - &d d
=====================================output===================================== =====================================output=====================================
- !!str &a a - &a !!str a
- !!int 2 - !!int 2
- !!int &c 4 - !!int &c 4
- &d d - &d d