Skip to content

Commit ba89ff2

Browse files
committed
fix: Check for non-node complex keys when stringifying with simpleKeys (#541)
1 parent f792d1b commit ba89ff2

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/stringify/stringifyPair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function stringifyPair(
2222
if (keyComment) {
2323
throw new Error('With simple keys, key nodes cannot have comments')
2424
}
25-
if (isCollection(key)) {
25+
if (isCollection(key) || (!isNode(key) && typeof key === 'object')) {
2626
const msg = 'With simple keys, collection cannot be used as a key value'
2727
throw new Error(msg)
2828
}

tests/doc/stringify.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,24 @@ describe('simple keys', () => {
723723
)
724724
})
725725

726+
test('key with JS object value', () => {
727+
const doc = YAML.parseDocument<any>('[foo]: bar')
728+
doc.contents.items[0].key = { foo: 42 }
729+
expect(doc.toString()).toBe('? foo: 42\n: bar\n')
730+
expect(() => doc.toString({ simpleKeys: true })).toThrow(
731+
/With simple keys, collection cannot be used as a key value/
732+
)
733+
})
734+
735+
test('key with JS null value', () => {
736+
const doc = YAML.parseDocument<any>('[foo]: bar')
737+
doc.contents.items[0].key = null
738+
expect(doc.toString()).toBe('? null\n: bar\n')
739+
expect(() => doc.toString({ simpleKeys: true })).toThrow(
740+
/With simple keys, collection cannot be used as a key value/
741+
)
742+
})
743+
726744
test('key value lingth > 1024', () => {
727745
const str = `
728746
? ${new Array(1026).join('a')}

0 commit comments

Comments
 (0)