Skip to content

Commit

Permalink
handle descriptors only containing a getter
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Mar 20, 2023
1 parent 0ad1acc commit c673476
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/utils/src/helpers.ts
Expand Up @@ -88,7 +88,7 @@ export function clone<T>(val: T, seen: WeakMap<any, any>): T {
const props = getOwnProperties(val)
for (const k of props) {
Object.defineProperty(out, k, {
...Object.getOwnPropertyDescriptor(val, k),
enumerable: true,
value: clone((val as any)[k], seen),
})
}
Expand Down
14 changes: 10 additions & 4 deletions test/core/test/utils.spec.ts
Expand Up @@ -152,9 +152,10 @@ describe('deepClone', () => {

test('can clone classes with proxied enumerable getters', () => {
const obj = Symbol.for('aClass')
interface TestShape { a: number; b: string }
class A {
[obj]: { a: number; b: string }
constructor(data: { a: number; b: string }) {
[obj]: TestShape
constructor(data: TestShape) {
this[obj] = data
return new Proxy(this, {
ownKeys() {
Expand All @@ -164,7 +165,6 @@ describe('deepClone', () => {
return {
...Reflect.getOwnPropertyDescriptor(data, p),
enumerable: true,
writable: false,
}
},
})
Expand All @@ -178,7 +178,13 @@ describe('deepClone', () => {
return this[obj].b
}
}
const aClass = new A({ a: 1, b: 'B' })
const shape = { a: 1 } as TestShape
Object.defineProperty(shape, 'b', {
configurable: true,
enumerable: true,
get: () => 'B',
})
const aClass = new A(shape)
expect(aClass.a).toEqual(1)
expect(aClass.b).toEqual('B')
expect(Object.keys(aClass)).toEqual(['a', 'b'])
Expand Down

0 comments on commit c673476

Please sign in to comment.