prettier/prettier

YAML: Prettier should put anchors before tags

クローズ

#19,524 opened on 2026/07/02

 (0 件のコメント) (0 件のリアクション) (1 人の担当者)JavaScript (4,988 件のフォーク)batch import
help wantedlang:yaml

Repository metrics

Stars
 (52,211 個のスター)
PR merge metrics
 (PR metrics pending)

説明

Prettier v3.9.4 Playground link

--parser yaml

Input:

---
One: &anchor !custom Value
Two: *anchor

Output:

---
One: !custom &anchor Value
Two: *anchor

Expected output:

---
One: !custom &anchor Value
Two: *anchor

Why?

The !custom tag (or !!str, or be it any other Node Tags) forms part of the &anchor definition and thus would be more logically and readably formatted first on the line.

Per the spec Node Properties may be specified in any order before the node’s content`, but in YAML, Flow Nodes that are alias nodes refer to the anchored node's properties:

A complete flow node also has optional node properties, except for alias nodes which refer to the anchored node properties.

This means in the example above, the node Two: *anchor actually resolves to !custom Value, which reads differently when formatted with Prettier:

One: !custom &anchor Value

with anchor in the middle. Formatting it like so, with tag and node following &anchor clearly denotes them as being part of that anchor:

One: &anchor !custom Value

Additionally, when displayed with the resolved structure, such as those visible in Node Anchors in YAML 1.2 spec, the anchors are defined before the tags -- other examples in that spec are consistently resolved in the same manner:

First occurrence: &anchor Value
Second occurrence: *anchor
%YAML 1.2
---
!!map {
  ? !!str "First occurrence"
  : &A !!str "Value",
  ? !!str "Second occurrence"
  : *A,
}```

コントリビューターガイド