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

Dump the simple key which is over 1024 properly #195

Merged
merged 2 commits into from
Sep 17, 2020
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
10 changes: 9 additions & 1 deletion src/ast/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Pair extends Node {
throw new Error(msg)
}
}
const explicitKey =
let explicitKey =
!simpleKeys &&
(!key ||
keyComment ||
Expand All @@ -124,6 +124,14 @@ export class Pair extends Node {
} else if (chompKeep && !keyComment && onChompKeep) onChompKeep()
return ctx.inFlow ? str : `? ${str}`
}
if (!explicitKey && str.length > 1024) {
if (!simpleKeys) {
explicitKey = true
} else {
const msg = 'With simple keys, single line scalar must not span more than 1024 characters'
throw new Error(msg)
}
}
str = explicitKey ? `? ${str}\n${indent}:` : `${str}:`
if (this.comment) {
// expected (but not strictly required) to be a single-line comment
Expand Down
12 changes: 12 additions & 0 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,18 @@ describe('simple keys', () => {
/With simple keys, collection cannot be used as a key value/
)
})

test('key value lingth > 1024', () => {
let str = `
? ${new Array(1026).join('a')}
: longkey`
const doc = YAML.parseDocument(str)
expect(String(doc)).toBe(`? ${new Array(1026).join('a')}\n: longkey\n`)
doc.options.simpleKeys = true
expect(() => String(doc)).toThrow(
/With simple keys, single line scalar must not span more than 1024 characters/
)
})
})

test('eemeli/yaml#128: YAML node inside object', () => {
Expand Down