YAML: Preserve order of anchor and tag (#19599)
This commit is contained in:
1 parent
b35943fa92
commit
351e399362
4 files changed
+51
-30
No files matched your search
@@ -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
|
||||
```
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user