prettier/prettier

YAML: Prettier should put anchors before tags

已关闭

#19,524 创建于 2026年7月2日

 (0 条评论) (0 个反应) (1 位负责人)JavaScript (4,988 个派生)batch import
help wantedlang:yaml

仓库指标

星标
 (52,211 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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,
}```

贡献者指南