Skip to content

Commit baa7876

Browse files
committed
Tighten dispatcher object guards for code quality
1 parent af009ce commit baa7876

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

crates/js/lib/src/shared/dom_insertion_dispatcher.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ interface LegacyDomInsertionDispatcherState {
6060
version?: unknown;
6161
}
6262

63+
function isNonNullObject(value: unknown): value is Record<PropertyKey, unknown> {
64+
return typeof value === 'object' && value !== null;
65+
}
66+
6367
function isOptionalFunction(
6468
value: unknown
6569
): value is ((...args: unknown[]) => unknown) | undefined {
6670
return value === undefined || typeof value === 'function';
6771
}
6872

6973
function isRegisteredDomInsertionHandler(value: unknown): value is RegisteredDomInsertionHandler {
70-
if (typeof value !== 'object' || value === null) {
74+
if (!isNonNullObject(value)) {
7175
return false;
7276
}
7377

@@ -81,7 +85,7 @@ function isRegisteredDomInsertionHandler(value: unknown): value is RegisteredDom
8185
}
8286

8387
function isDispatcherState(value: unknown): value is DomInsertionDispatcherState {
84-
if (typeof value !== 'object' || value === null) {
88+
if (!isNonNullObject(value)) {
8589
return false;
8690
}
8791

@@ -116,17 +120,11 @@ function compareHandlers(
116120
}
117121

118122
function getStateVersion(state: unknown): unknown {
119-
return typeof state === 'object' && state !== null
120-
? (state as { version?: unknown }).version
121-
: undefined;
123+
return isNonNullObject(state) ? state.version : undefined;
122124
}
123125

124126
function restoreStaleDispatcherMethods(existingState: unknown): void {
125-
if (
126-
typeof Element === 'undefined' ||
127-
typeof existingState !== 'object' ||
128-
existingState === null
129-
) {
127+
if (typeof Element === 'undefined' || !isNonNullObject(existingState)) {
130128
return;
131129
}
132130

0 commit comments

Comments
 (0)