fix(yaml): require flow closing brackets past the parent indentation

YAML 1.2.2 requires every line of a flow collection inside a block
collection, including the closing bracket, to be indented more than the
parent node. Drop the leniency copied from yaml 2.9.1 that accepted a
closing bracket at the parent's indentation.
This commit is contained in:
Sebastian Lorenz
2026-10-21 08:01:48 +00:00
parent 3beb5824b2
commit 5f07e2ba2d
3 changed files with 8 additions and 10 deletions
@@ -2,7 +2,7 @@
"effect": patch
---
Support multiline flow collections in `Yaml.parse`, such as a JSON-style mapping whose entries and closing bracket are on separate lines. Continuation lines must be indented more than the parent node, but the closing bracket may sit at the parent's indentation, as in the `yaml` package. Plain and quoted scalars inside flow collections may span lines and are folded, and comments may follow any entry.
Support multiline flow collections in `Yaml.parse`, such as a JSON-style mapping whose entries and closing bracket are on separate lines. Continuation lines, including the closing bracket, must be indented more than the parent node, as YAML 1.2 specifies. Plain and quoted scalars inside flow collections may span lines and are folded, and comments may follow any entry.
Support compact nested block sequences such as `- - value`, and parse a compact mapping in a sequence entry at the column where its first key starts.
+2 -4
View File
@@ -763,10 +763,8 @@ class YamlParser {
return new FlowParser(text, this.anchors, this.handles, () => {
const next = this.lines[this.index]
if (next === undefined || isDocumentMarker(next)) return undefined
// Continuation lines are more indented than the parent; a closing bracket may sit at its indentation.
const content = next.text.trim().length > 0
if (content && next.indent < parentIndent) return undefined
if (content && next.indent === parentIndent && !/^[\]}]/.test(next.text)) return undefined
// Continuation lines, including the closing bracket, are more indented than the parent node.
if (next.text.trim().length > 0 && next.indent <= parentIndent) return undefined
this.index++
// The flow parser skips comments itself, since quoted scalars may span lines.
return next.raw.slice(next.indent)
+5 -5
View File
@@ -335,7 +335,7 @@ development:
// YAML 1.2.2 §§7.4–7.5: flow collections may span lines indented past the parent node.
// Expected values and rejection fixtures were checked with yaml 2.9.1, which also
// accepts a closing bracket at the parent indentation.
// accepts a closing bracket at the parent indentation, against the spec.
describe("multiline flow collections", () => {
it.each(
[
@@ -345,9 +345,7 @@ development:
{ example: { active_anon: 1572864, active_file: 5115904 } }
],
["mapping starting on its key line", "a: {\n b: 1,\n c: 2\n }\n", { a: { b: 1, c: 2 } }],
["closing bracket at the parent indentation", "a: {\n b: 1\n}\n", { a: { b: 1 } }],
["closing bracket below the value indentation", "a:\n {\n b: 1\n}\n", { a: { b: 1 } }],
["sequence item", "- [\n 1,\n 2\n]\n", [[1, 2]]],
["sequence item", "- [\n 1,\n 2\n ]\n", [[1, 2]]],
["root collection", "{\n\"a\": [\n1\n]\n}\n", { a: [1] }],
["nested collections", "a: {\n b: [\n 1\n ],\n c: {d: 2}\n }\n", { a: { b: [1], c: { d: 2 } } }],
["sibling key after the collection", "a: [\n 1\n ]\nb: 2\n", { a: [1], b: 2 }],
@@ -372,7 +370,9 @@ development:
["tab indentation", "a: [1,\n\t2]\n"],
["unterminated collection before a sibling key", "a: [1,\n 2\nb: 3\n"],
["content after the closing bracket", "a: [1,\n 2] extra\n"],
["content after a closing bracket at the parent indentation", "a: [\n 1\n], b: 2\n"],
["closing bracket at the parent indentation", "a: {\n b: 1\n}\n"],
["closing bracket below the value indentation", "a:\n {\n b: 1\n}\n"],
["closing bracket at the sequence indentation", "- [\n 1\n]\n"],
["document marker inside a root collection", "[\n1,\n---\n]\n"],
["unterminated multiline quoted scalar", "a: [\"b\n c]\n"],
["quoted continuation at the parent indentation", "a: [\"b\nc\"]\n"]