File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ export function stringifyPair(
22
22
if ( keyComment ) {
23
23
throw new Error ( 'With simple keys, key nodes cannot have comments' )
24
24
}
25
- if ( isCollection ( key ) ) {
25
+ if ( isCollection ( key ) || ( ! isNode ( key ) && typeof key === 'object' ) ) {
26
26
const msg = 'With simple keys, collection cannot be used as a key value'
27
27
throw new Error ( msg )
28
28
}
Original file line number Diff line number Diff line change @@ -723,6 +723,24 @@ describe('simple keys', () => {
723
723
)
724
724
} )
725
725
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
+ / W i t h s i m p l e k e y s , c o l l e c t i o n c a n n o t b e u s e d a s a k e y v a l u e /
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
+ / W i t h s i m p l e k e y s , c o l l e c t i o n c a n n o t b e u s e d a s a k e y v a l u e /
741
+ )
742
+ } )
743
+
726
744
test ( 'key value lingth > 1024' , ( ) => {
727
745
const str = `
728
746
? ${ new Array ( 1026 ) . join ( 'a' ) }
You can’t perform that action at this time.
0 commit comments