Skip to content

Commit

Permalink
Dump long keys properly (#195)
Browse files Browse the repository at this point in the history
* Dump the simple key which is over 1024 properly
  • Loading branch information
dota17 authored and eemeli committed Mar 13, 2021
1 parent 54dac4f commit 83b0e54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ast/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Pair extends Node {
throw new Error(msg)
}
}
const explicitKey =
let explicitKey =
!simpleKeys &&
(!key ||
keyComment ||
Expand All @@ -117,6 +117,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 @@ -469,6 +469,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

0 comments on commit 83b0e54

Please sign in to comment.