Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/stringify/stringifyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ function blockString(
: type === Scalar.BLOCK_LITERAL
? true
: !lineLengthOverLimit(value, lineWidth, indent.length)
if (!value) return literal ? '|\n' : '>\n'
if (!value) {
if (ctx.forceBlockIndent) return literal ? '|2\n' : '>2\n'
return literal ? '|\n' : '>\n'
}

// determine chomping from whitespace at value end
let chomp: '' | '-' | '+'
Expand Down
14 changes: 14 additions & 0 deletions tests/doc/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ describe('stringify comments', () => {
string
`)
})

test('empty root block scalar keeps indent indicator (#687)', () => {
for (const style of ['|', '>'] as const) {
const doc = YAML.parseDocument(`${style}5\n#comment`)
expect(doc.toJSON()).toBe('')
expect(doc.comment).toBe('comment')
const out = String(doc)
expect(out.startsWith(`${style}2\n`)).toBe(true)
expect(out).toContain('#comment')
const rt = YAML.parseDocument(out)
expect(rt.toJSON()).toBe('')
expect(rt.comment).toBe('comment')
}
})
})

describe('seq comments', () => {
Expand Down