From 5f07e2ba2da00a56ce3b4ed905fe149131b44437 Mon Sep 17 00:00:00 2001 From: Sebastian Lorenz Date: Wed, 21 Oct 2026 08:01:48 +0000 Subject: [PATCH] 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. --- .changeset/yaml-multiline-flow-collections.md | 2 +- packages/effect/src/encoding/Yaml.ts | 6 ++---- packages/effect/test/encoding/Yaml.test.ts | 10 +++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.changeset/yaml-multiline-flow-collections.md b/.changeset/yaml-multiline-flow-collections.md index 14764e8138..b0f8ec2dd9 100644 --- a/.changeset/yaml-multiline-flow-collections.md +++ b/.changeset/yaml-multiline-flow-collections.md @@ -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. diff --git a/packages/effect/src/encoding/Yaml.ts b/packages/effect/src/encoding/Yaml.ts index 79368a92b7..9a812af87f 100644 --- a/packages/effect/src/encoding/Yaml.ts +++ b/packages/effect/src/encoding/Yaml.ts @@ -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) diff --git a/packages/effect/test/encoding/Yaml.test.ts b/packages/effect/test/encoding/Yaml.test.ts index 14ca8d1a0a..6f8225da8f 100644 --- a/packages/effect/test/encoding/Yaml.test.ts +++ b/packages/effect/test/encoding/Yaml.test.ts @@ -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"]