Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the lineWidth option for line breaking in flow collections #522

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
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
Loading