Skip to content

Commit 5e1e819

Browse files
committed
Use Object.create() to create null prototype objects
The use of `__proto__` is deprecated.
1 parent 792cbe7 commit 5e1e819

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/nodes/Pair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Pair<
3636
doc: Document<DocValue, boolean>,
3737
ctx: ToJSContext
3838
): ReturnType<typeof addPairToJSMap> {
39-
const pair = ctx.mapAsMap ? new Map() : { __proto__: null }
39+
const pair = ctx.mapAsMap ? new Map() : Object.create(null)
4040
return addPairToJSMap(doc, ctx, pair, this)
4141
}
4242

src/nodes/YAMLMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class YAMLMap<
239239
? new Type()
240240
: ctx?.mapAsMap
241241
? new Map()
242-
: { __proto__: null }
242+
: Object.create(null)
243243
if (this.anchor) ctx.setAnchor(this, map)
244244
for (const pair of this.values.values()) addPairToJSMap(doc, ctx, map, pair)
245245
return map

tests/node-to-js.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ describe('scalars', () => {
1717
describe('collections', () => {
1818
test('map', () => {
1919
const doc = parseDocument('key: 42')
20-
expect(doc.value.toJS(doc)).toStrictEqual({ __proto__: null, key: 42 })
20+
expect(doc.value.toJS(doc)).toStrictEqual(
21+
Object.assign(Object.create(null), { key: 42 })
22+
)
2123
})
2224

2325
test('map in seq', () => {
2426
const doc = parseDocument<YAMLSeq, false>('- key: 42')
25-
expect(doc.get(0).toJS(doc)).toStrictEqual({ __proto__: null, key: 42 })
27+
expect(doc.get(0).toJS(doc)).toStrictEqual(
28+
Object.assign(Object.create(null), { key: 42 })
29+
)
2630
})
2731
})
2832

@@ -51,10 +55,11 @@ describe('alias', () => {
5155
two: &b { key: *a }
5256
three: *b
5357
`)
54-
expect(doc.get('three').toJS(doc)).toStrictEqual({
55-
__proto__: null,
56-
key: 42
57-
})
58+
expect(doc.get('three').toJS(doc)).toStrictEqual(
59+
Object.assign(Object.create(null), {
60+
key: 42
61+
})
62+
)
5863
})
5964

6065
test('missing anchor', () => {

tests/properties.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function nullifyProto(value: unknown): unknown {
99
if (Array.isArray(value)) {
1010
return value.map(nullifyProto)
1111
}
12-
const result: Record<string, unknown> = { __proto__: null }
12+
const result: Record<string, unknown> = Object.create(null)
1313
for (const key in value) {
1414
if (Object.hasOwn(value, key)) {
1515
result[key] = nullifyProto((value as Record<string, unknown>)[key])

0 commit comments

Comments
 (0)