From c97f6cd6105bb17c0db9d8953aaf2bab510b0d6b Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Fri, 29 Sep 2023 11:31:22 +0300 Subject: [PATCH 1/3] fix: handle both of property descriptor types in masker --- src/main/ts/utils/deepmap.ts | 8 +++++++- src/test/ts/utils/deepmap.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/ts/utils/deepmap.ts b/src/main/ts/utils/deepmap.ts index 71c6c2c..1871a1f 100644 --- a/src/main/ts/utils/deepmap.ts +++ b/src/main/ts/utils/deepmap.ts @@ -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), }) }) diff --git a/src/test/ts/utils/deepmap.ts b/src/test/ts/utils/deepmap.ts index fd99b4d..42d5a6e 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 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() From 925daf4304e66e3c900ec883a0538759c2cf8b79 Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Fri, 29 Sep 2023 11:33:57 +0300 Subject: [PATCH 2/3] test: rename suite --- src/test/ts/utils/deepmap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ts/utils/deepmap.ts b/src/test/ts/utils/deepmap.ts index 42d5a6e..d9d8dbd 100644 --- a/src/test/ts/utils/deepmap.ts +++ b/src/test/ts/utils/deepmap.ts @@ -49,7 +49,7 @@ test('deepMap handle error objects', () => { ) }) -test('deepMap handles accessor property descriptor', async () => { +test('deepMap handles accessor and data property descriptors', async () => { const objectWithDataPropertyDescriptor = {} Object.defineProperty(objectWithDataPropertyDescriptor, 'bar', { value: 42 }) assert.equal( From b78f9e823e8e0288dffa9dd7c3e6da34f997fc5a Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Fri, 29 Sep 2023 15:01:04 +0300 Subject: [PATCH 3/3] fix: do not mutate descriptor --- src/main/ts/utils/deepmap.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/ts/utils/deepmap.ts b/src/main/ts/utils/deepmap.ts index 1871a1f..50fff89 100644 --- a/src/main/ts/utils/deepmap.ts +++ b/src/main/ts/utils/deepmap.ts @@ -20,14 +20,14 @@ 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 + // 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, + ...rest, value: deepMap(value, fn, refs, key), }) })