Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): augmentObject should use existing property descriptors whenever possible #5872

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/workflow/src/AugmentObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { IDataObject } from './Interfaces';

const defaultPropertyDescriptor = Object.freeze({ enumerable: true, configurable: true });

const augmentedObjects = new WeakSet<object>();

function augment<T>(value: T): T {
if (
typeof value !== 'object' ||
Expand Down Expand Up @@ -54,7 +57,8 @@ export function augmentArray<T>(data: T[]): T[] {
if (key === 'length') {
return Reflect.getOwnPropertyDescriptor(newData, key);
}
return { configurable: true, enumerable: true };

return Object.getOwnPropertyDescriptor(data, key) ?? defaultPropertyDescriptor;
},
has(target, key) {
return Reflect.has(newData !== undefined ? newData : target, key);
Expand Down Expand Up @@ -130,18 +134,17 @@ export function augmentObject<T extends object>(data: T): T {

return true;
},

ownKeys(target) {
return [...new Set([...Reflect.ownKeys(target), ...Object.keys(newData)])].filter(
const originalKeys = Reflect.ownKeys(target);
const newKeys = Object.keys(newData);
return [...new Set([...originalKeys, ...newKeys])].filter(
(key) => deletedProperties.indexOf(key) === -1,
);
},

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getOwnPropertyDescriptor(k) {
return {
enumerable: true,
configurable: true,
};
getOwnPropertyDescriptor(target, key) {
return Object.getOwnPropertyDescriptor(data, key) ?? defaultPropertyDescriptor;
},
});
}
8 changes: 8 additions & 0 deletions packages/workflow/test/AugmentObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,13 @@ describe('AugmentObject', () => {

expect(timeAugmented).toBeLessThan(timeCopied);
});

test('should ignore non-enumerable keys', () => {
const originalObject = { a: 1, b: 2 };
Object.defineProperty(originalObject, '__hiddenProp', { enumerable: false });

const augmentedObject = augmentObject(originalObject);
expect(Object.keys(augmentedObject)).toEqual(['a', 'b']);
});
});
});