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: deepEqual exceed maximum call stack size #1428

Merged
merged 1 commit into from
Mar 3, 2024
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
25 changes: 25 additions & 0 deletions src/util/deepEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ export function deepEqual<A, B>(obj1: A, obj2: B) {
}
return true;
}
// If objects are VNodes, compare their props only
if (
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('constructor') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('constructor') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('props') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('props') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('key') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('key') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('ref') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('ref') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('type') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('type')
) {
return deepEqual(obj1['props'], obj2['props']);
}
// If objects are both objects, compare their properties recursively
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
Expand Down
9 changes: 9 additions & 0 deletions tests/jest/util/deepEqual.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deepEqual } from '../../../src/util/deepEqual';
import { html } from '../../../src/util/html';

describe('deepEqual', () => {
it('should return true when objects are the same', () => {
Expand Down Expand Up @@ -31,4 +32,12 @@ describe('deepEqual', () => {
const result = deepEqual({ a: 42, c: fn }, { a: 42, c: fn });
expect(result).toBeTrue();
});

it('should return true when objects are VNodes', () => {
const result = deepEqual(
html('<span>Grid.js</span>'),
html('<span>Grid.js</span>'),
);
expect(result).toBeTrue();
});
});
Loading