From d104f46897611dce2f5ccf08edc5e0dd59bfedbd Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Fri, 29 Sep 2023 18:08:26 +0600 Subject: [PATCH] fix: handle both of property descriptor types in masker (#68) --- src/main/ts/utils/deepmap.ts | 10 ++++++++-- src/test/ts/utils/deepmap.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/ts/utils/deepmap.ts b/src/main/ts/utils/deepmap.ts index 71c6c2c..50fff89 100644 --- a/src/main/ts/utils/deepmap.ts +++ b/src/main/ts/utils/deepmap.ts @@ -20,9 +20,15 @@ export function deepMap( return } + // descriptor cannot contain both accessor method and value + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#description + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { get, set, ...rest } = descriptor + const value = get ? get() : descriptor.value + Object.defineProperty(n, key, { - ...descriptor, - value: deepMap(descriptor.value, fn, refs, key), + ...rest, + value: deepMap(value, fn, refs, key), }) }) diff --git a/src/test/ts/utils/deepmap.ts b/src/test/ts/utils/deepmap.ts index fd99b4d..d9d8dbd 100644 --- a/src/test/ts/utils/deepmap.ts +++ b/src/test/ts/utils/deepmap.ts @@ -49,4 +49,20 @@ test('deepMap handle error objects', () => { ) }) +test('deepMap handles accessor and data property descriptors', 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()