Skip to content

Commit

Permalink
fix: Check for non-node complex keys when stringifying with simpleKeys (
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed May 17, 2024
1 parent f792d1b commit ba89ff2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/stringify/stringifyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function stringifyPair(
if (keyComment) {
throw new Error('With simple keys, key nodes cannot have comments')
}
if (isCollection(key)) {
if (isCollection(key) || (!isNode(key) && typeof key === 'object')) {
const msg = 'With simple keys, collection cannot be used as a key value'
throw new Error(msg)
}
Expand Down
18 changes: 18 additions & 0 deletions tests/doc/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,24 @@ describe('simple keys', () => {
)
})

test('key with JS object value', () => {
const doc = YAML.parseDocument<any>('[foo]: bar')
doc.contents.items[0].key = { foo: 42 }
expect(doc.toString()).toBe('? foo: 42\n: bar\n')
expect(() => doc.toString({ simpleKeys: true })).toThrow(
/With simple keys, collection cannot be used as a key value/
)
})

test('key with JS null value', () => {
const doc = YAML.parseDocument<any>('[foo]: bar')
doc.contents.items[0].key = null
expect(doc.toString()).toBe('? null\n: bar\n')
expect(() => doc.toString({ simpleKeys: true })).toThrow(
/With simple keys, collection cannot be used as a key value/
)
})

test('key value lingth > 1024', () => {
const str = `
? ${new Array(1026).join('a')}
Expand Down

0 comments on commit ba89ff2

Please sign in to comment.