Skip to content

Commit

Permalink
fix: handle both of property descriptor types in masker
Browse files Browse the repository at this point in the history
  • Loading branch information
oljekechoro committed Sep 29, 2023
1 parent 3a308db commit c97f6cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/ts/utils/deepmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ export function deepMap(
return
}

const value = descriptor.get ? descriptor.get() : descriptor.value
// descriptor cannot contain both accessor method and value
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#description
delete descriptor.get
delete descriptor.set

Object.defineProperty(n, key, {
...descriptor,
value: deepMap(descriptor.value, fn, refs, key),
value: deepMap(value, fn, refs, key),
})
})

Expand Down
16 changes: 16 additions & 0 deletions src/test/ts/utils/deepmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ test('deepMap handle error objects', () => {
)
})

test('deepMap handles accessor property descriptor', async () => {
const objectWithDataPropertyDescriptor = {}
Object.defineProperty(objectWithDataPropertyDescriptor, 'bar', { value: 42 })
assert.equal(
deepMap(objectWithDataPropertyDescriptor, (el) => el + 'foo').bar,
'42foo',
)

const objectWithAccessorPropertyDescriptor = {}
Object.defineProperty(objectWithAccessorPropertyDescriptor, 'bar', { get: () => 42 })
assert.equal(
deepMap(objectWithAccessorPropertyDescriptor, (el) => el + 'foo').bar,
'42foo',
)
})

test.run()

0 comments on commit c97f6cd

Please sign in to comment.