Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/nodes/Pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class Pair<
doc: Document<DocValue, boolean>,
ctx: ToJSContext
): ReturnType<typeof addPairToJSMap> {
const pair = ctx.mapAsMap ? new Map() : {}
const pair = ctx.mapAsMap
? new Map()
: (Object.create(null) as Record<string, unknown>)
return addPairToJSMap(doc, ctx, pair, this)
}

Expand Down
6 changes: 5 additions & 1 deletion src/nodes/YAMLMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export class YAMLMap<
Type?: { new (): T }
) {
ctx ??= new ToJSContext()
const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {}
const map = Type
? new Type()
: ctx?.mapAsMap
? new Map()
: (Object.create(null) as Record<string, unknown>)
if (this.anchor) ctx.setAnchor(this, map)
for (const pair of this.values.values()) addPairToJSMap(doc, ctx, map, pair)
return map
Expand Down
14 changes: 11 additions & 3 deletions tests/node-to-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ describe('scalars', () => {
describe('collections', () => {
test('map', () => {
const doc = parseDocument('key: 42')
expect(doc.value.toJS(doc)).toMatchObject({ key: 42 })
expect(doc.value.toJS(doc)).toStrictEqual(
Object.assign(Object.create(null), { key: 42 })
)
})

test('map in seq', () => {
const doc = parseDocument<YAMLSeq, false>('- key: 42')
expect(doc.get(0).toJS(doc)).toMatchObject({ key: 42 })
expect(doc.get(0).toJS(doc)).toStrictEqual(
Object.assign(Object.create(null), { key: 42 })
)
})
})

Expand Down Expand Up @@ -51,7 +55,11 @@ describe('alias', () => {
two: &b { key: *a }
three: *b
`)
expect(doc.get('three').toJS(doc)).toMatchObject({ key: 42 })
expect(doc.get('three').toJS(doc)).toStrictEqual(
Object.assign(Object.create(null), {
key: 42
})
)
})

test('missing anchor', () => {
Expand Down
21 changes: 20 additions & 1 deletion tests/properties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import * as fc from 'fast-check'
import { parse, stringify } from 'yaml'

/** Set pojo prototypes to `null` recursively */
function nullifyProto(value: unknown): unknown {
if (typeof value !== 'object' || value == null) {
return value
}
if (Array.isArray(value)) {
return value.map(nullifyProto)
}
const result: Record<string, unknown> = Object.create(null)
for (const key in value) {
if (Object.hasOwn(value, key)) {
result[key] = nullifyProto((value as Record<string, unknown>)[key])
}
}
return result
}

describe('properties', () => {
test('parse stringified object', () => {
const key = fc.fullUnicodeString()
Expand All @@ -25,7 +42,9 @@ describe('properties', () => {

fc.assert(
fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
expect(parse(stringify(obj, opts), opts)).toStrictEqual(obj)
expect(parse(stringify(obj, opts), opts)).toStrictEqual(
nullifyProto(obj)
)
})
)
})
Expand Down
Loading