Skip to content

Commit 0cbb471

Browse files
committed
Fix whitespace-only block scalar followed by a less indented line, close #802
1 parent 30108cc commit 0cbb471

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎src/parser/parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ function readBlockScalar (state: ParserState, parentIndent: number, props: NodeP
709709
state.position = linePosition + column
710710
throwError(state, 'tab characters must not be used in indentation')
711711
}
712-
if (column < maxLeadingIndent) {
712+
if (column >= parentIndent && column < maxLeadingIndent) {
713713
state.position = linePosition + column
714714
throwError(state, 'bad indentation of a mapping entry')
715715
}

‎test/core/parser/parser.test.mjs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it } from 'node:test'
22
import assert from 'node:assert/strict'
3-
import { EVENT_ID, getScalarValue, loadAll, parseEvents } from 'js-yaml'
3+
import { EVENT_ID, getScalarValue, load, loadAll, parseEvents } from 'js-yaml'
44

55
describe('parser', () => {
66
it('keeps an implicit null mapping value before a document marker', () => {
@@ -22,4 +22,11 @@ describe('parser', () => {
2222
[{ a: null }, { x: 1 }]
2323
)
2424
})
25+
26+
it('ends a whitespace-only block scalar at a less indented line', () => {
27+
assert.deepEqual(load('a: |+\n \nb: 1\n'), { a: '\n', b: 1 })
28+
assert.deepEqual(load('a:\n b: |+\n \n c: 1\n'), { a: { b: '\n', c: 1 } })
29+
// Leading empty lines longer than the first content line are still an error
30+
assert.throws(() => load('a: |\n \n b\n'), /bad indentation/)
31+
})
2532
})

0 commit comments

Comments
 (0)