fix(parsing): treat leading '#' in block scalars as literal content
This commit is contained in:
parent
c13b7c97fd
commit
5d76d184cb
@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.1.3] - 2026-06-23
|
||||
|
||||
### Fixed
|
||||
|
||||
- Parse a leading `#` in block scalar content (`>`, `>-`, `|`, `|-`) as literal
|
||||
text instead of a comment. A block scalar whose first content line began with
|
||||
`#`, such as an OpenAPI `$ref: >-` folded onto `#/components/...`, was
|
||||
previously dropped to an empty string.
|
||||
|
||||
## [0.1.2] - 2026-06-06
|
||||
|
||||
### Added
|
||||
|
||||
@ -54,7 +54,14 @@ extension PureYAML.Parsing.Scanner {
|
||||
guard !state.reader.isAtEnd, !isLineBreak(state.reader.peek()) else {
|
||||
return false
|
||||
}
|
||||
guard state.reader.peek() != "#" else {
|
||||
// A leading '#' normally starts a comment line, which must not drive
|
||||
// indent/dedent processing. Inside a block scalar it is different: when
|
||||
// the header is still pending or the content is active, a leading '#' is
|
||||
// literal scalar content, so indentation must be processed so the block
|
||||
// scalar's indentation resolves and its content lines are consumed.
|
||||
// Bailing here would leave `blockScalarIndentation` unset and the main
|
||||
// loop would mis-scan the line as a comment, dropping the content.
|
||||
if state.reader.peek() == "#", !state.pendingBlockScalarIndentation, state.blockScalarIndentation == nil {
|
||||
return false
|
||||
}
|
||||
return state.flowDepth == 0
|
||||
|
||||
@ -196,4 +196,58 @@ struct ParsingMultilineScalarTests {
|
||||
""",
|
||||
))
|
||||
}
|
||||
|
||||
@Test("Block scalars keep a leading '#' as literal content, not a comment")
|
||||
func test_blockScalarLeadingHashIsLiteralContent() throws {
|
||||
let root = try requireMapping(PureYAML.parse(
|
||||
"""
|
||||
folded:
|
||||
$ref: >-
|
||||
#/components/schemas/foo
|
||||
literal:
|
||||
$ref: |-
|
||||
#/components/schemas/bar
|
||||
""",
|
||||
))
|
||||
|
||||
#expect(root?.mapping("folded")?["$ref"] == .string("#/components/schemas/foo"))
|
||||
#expect(root?.mapping("literal")?["$ref"] == .string("#/components/schemas/bar"))
|
||||
}
|
||||
|
||||
@Test("A leading '#' is preserved across every line of a block scalar")
|
||||
func test_blockScalarLeadingHashAcrossMultipleLines() throws {
|
||||
let root = try requireMapping(PureYAML.parse(
|
||||
"""
|
||||
doc: |-
|
||||
#/components/schemas/foo
|
||||
plain line
|
||||
#/components/schemas/bar
|
||||
after: tail
|
||||
""",
|
||||
))
|
||||
|
||||
#expect(root?["doc"] == .string(
|
||||
"""
|
||||
#/components/schemas/foo
|
||||
plain line
|
||||
#/components/schemas/bar
|
||||
""",
|
||||
))
|
||||
// The block scalar ends at the dedent; the following key is unaffected.
|
||||
#expect(root?["after"] == .string("tail"))
|
||||
}
|
||||
|
||||
@Test("A genuine trailing comment after a plain value is still ignored")
|
||||
func test_plainValueTrailingCommentStillIgnored() throws {
|
||||
let root = try requireMapping(PureYAML.parse(
|
||||
"""
|
||||
kind: widget # this is a comment
|
||||
# whole-line comment
|
||||
count: 3
|
||||
""",
|
||||
))
|
||||
|
||||
#expect(root?["kind"] == .string("widget"))
|
||||
#expect(root?["count"] == .int(3))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user