Skip to content

Commit 750adbe

Browse files
committed
fix: Stringify flow collection comments in parent (fixes #528)
1 parent e07998c commit 750adbe

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/stringify/stringifyCollection.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ function stringifyBlockCollection(
8787
}
8888

8989
function stringifyFlowCollection(
90-
{ comment, items }: Readonly<Collection>,
90+
{ items }: Readonly<Collection>,
9191
ctx: StringifyContext,
92-
{ flowChars, itemIndent, onComment }: StringifyCollectionOptions
92+
{ flowChars, itemIndent }: StringifyCollectionOptions
9393
) {
9494
const {
9595
indent,
@@ -141,30 +141,23 @@ function stringifyFlowCollection(
141141
linesAtValue = lines.length
142142
}
143143

144-
let str: string
145144
const { start, end } = flowChars
146145
if (lines.length === 0) {
147-
str = start + end
146+
return start + end
148147
} else {
149148
if (!reqNewline) {
150149
const len = lines.reduce((sum, line) => sum + line.length + 2, 2)
151150
reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth
152151
}
153152
if (reqNewline) {
154-
str = start
153+
let str = start
155154
for (const line of lines)
156155
str += line ? `\n${indentStep}${indent}${line}` : '\n'
157-
str += `\n${indent}${end}`
156+
return `${str}\n${indent}${end}`
158157
} else {
159-
str = `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`
158+
return `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`
160159
}
161160
}
162-
163-
if (comment) {
164-
str += lineComment(str, indent, commentString(comment))
165-
if (onComment) onComment()
166-
}
167-
return str
168161
}
169162

170163
function addCommentBefore(

tests/doc/comments.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,19 @@ describe('stringify comments', () => {
540540
`)
541541
})
542542

543+
test('line comment after [], in seq', () => {
544+
const doc = YAML.parseDocument(source`
545+
[ [a], #c0
546+
[b] #c1
547+
]`)
548+
expect(String(doc)).toBe(source`
549+
[
550+
[ a ], #c0
551+
[ b ] #c1
552+
]
553+
`)
554+
})
555+
543556
test('line comment after , in map', () => {
544557
const doc = YAML.parseDocument(source`
545558
{ a, #c0
@@ -555,6 +568,21 @@ describe('stringify comments', () => {
555568
`)
556569
})
557570

571+
test('line comment after {}, in map', () => {
572+
const doc = YAML.parseDocument(source`
573+
{ {a: 1}, #c0
574+
b: {c: 2}, #c1
575+
d #c2
576+
}`)
577+
expect(String(doc)).toBe(source`
578+
{
579+
? { a: 1 }, #c0
580+
b: { c: 2 }, #c1
581+
d #c2
582+
}
583+
`)
584+
})
585+
558586
test('line comment after flow collection (eemeli/yaml#443)', () => {
559587
const doc = YAML.parseDocument(source`
560588
[ value1, value2 ] # comment

0 commit comments

Comments
 (0)