Skip to content

Commit

Permalink
fix: use the lineWidth option for line breaking in flow collections
Browse files Browse the repository at this point in the history
This should address issues #288 and #507.
  • Loading branch information
no92 committed Feb 13, 2024
1 parent b7696fc commit 2ce67c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/stringify/stringifyCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function stringifyFlowCollection(
} else {
if (!reqNewline) {
const len = lines.reduce((sum, line) => sum + line.length + 2, 2)
reqNewline = len > Collection.maxFlowStringSingleLineLength
reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth
}
if (reqNewline) {
str = start
Expand Down
32 changes: 31 additions & 1 deletion tests/doc/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ describe('scalar styles', () => {
y,
n
]\n`
expect(String(doc)).toBe(str)
expect(doc.toString({ lineWidth: 1 })).toBe(str)
expect(YAML.parse(str)).toEqual([
true,
false,
Expand Down Expand Up @@ -868,6 +868,36 @@ describe('indentSeq: false', () => {
})
})

describe('lineWidth', () => {
const doc = YAML.parseDocument(
"[ 'Sed', 'ut', 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error', 'sit', 'voluptatem', 'accusantium', 'doloremque', 'laudantium,', 'totam' ]"
)

test('limit to 80 with overlong flow collection', () => {
expect(doc.toString({lineWidth: 80})).toBe(`[
'Sed',
'ut',
'perspiciatis',
'unde',
'omnis',
'iste',
'natus',
'error',
'sit',
'voluptatem',
'accusantium',
'doloremque',
'laudantium,',
'totam'
]
`)
})

test('limit > flow collection length', () => {
expect(doc.toString({lineWidth: 162})).toBe("[ 'Sed', 'ut', 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error', 'sit', 'voluptatem', 'accusantium', 'doloremque', 'laudantium,', 'totam' ]\n")
})
})

describe('collectionStyle', () => {
test('collectionStyle: undefined', () => {
const doc = new YAML.Document<YAML.YAMLMap, false>({ foo: ['bar'] })
Expand Down

0 comments on commit 2ce67c7

Please sign in to comment.