From 68724f4cee61b4a2e5f93a7203983e77452a3cf1 Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Tue, 20 Feb 2024 23:52:11 +0900 Subject: [PATCH] Fix deepEqual exceed maximum call stack size --- src/util/deepEqual.ts | 25 +++++++++++++++++++++++++ tests/jest/util/deepEqual.test.ts | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/src/util/deepEqual.ts b/src/util/deepEqual.ts index 0450bbdc..5caf9578 100644 --- a/src/util/deepEqual.ts +++ b/src/util/deepEqual.ts @@ -31,6 +31,31 @@ export function deepEqual(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); diff --git a/tests/jest/util/deepEqual.test.ts b/tests/jest/util/deepEqual.test.ts index 960ec253..d3c2c9c0 100644 --- a/tests/jest/util/deepEqual.test.ts +++ b/tests/jest/util/deepEqual.test.ts @@ -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', () => { @@ -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('Grid.js'), + html('Grid.js'), + ); + expect(result).toBeTrue(); + }); });