Skip to content

Commit d9e2f3f

Browse files
committed
fix: compare paths against Regex instances in ignoredPaths
1 parent 78781fd commit d9e2f3f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

packages/toolkit/src/immutableStateInvariantMiddleware.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export function isImmutableDefault(value: unknown): boolean {
1515

1616
export function trackForMutations(
1717
isImmutable: IsImmutableFunc,
18-
ignorePaths: IgnorePaths | undefined,
18+
ignoredPaths: IgnorePaths | undefined,
1919
obj: any,
2020
) {
21-
const trackedProperties = trackProperties(isImmutable, ignorePaths, obj)
21+
const trackedProperties = trackProperties(isImmutable, ignoredPaths, obj)
2222
return {
2323
detectMutations() {
24-
return detectMutations(isImmutable, ignorePaths, trackedProperties, obj)
24+
return detectMutations(isImmutable, ignoredPaths, trackedProperties, obj)
2525
},
2626
}
2727
}
@@ -33,7 +33,7 @@ interface TrackedProperty {
3333

3434
function trackProperties(
3535
isImmutable: IsImmutableFunc,
36-
ignorePaths: IgnorePaths = [],
36+
ignoredPaths: IgnorePaths = [],
3737
obj: Record<string, any>,
3838
path: string = '',
3939
checkedObjects: Set<Record<string, any>> = new Set(),
@@ -44,17 +44,28 @@ function trackProperties(
4444
checkedObjects.add(obj)
4545
tracked.children = {}
4646

47+
const hasIgnoredPaths = ignoredPaths.length > 0
48+
4749
for (const key in obj) {
48-
const childPath = path ? path + '.' + key : key
49-
if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
50-
continue
50+
const nestedPath = path ? path + '.' + key : key
51+
52+
if (hasIgnoredPaths) {
53+
const hasMatches = ignoredPaths.some((ignored) => {
54+
if (ignored instanceof RegExp) {
55+
return ignored.test(nestedPath)
56+
}
57+
return nestedPath === ignored
58+
})
59+
if (hasMatches) {
60+
continue
61+
}
5162
}
5263

5364
tracked.children[key] = trackProperties(
5465
isImmutable,
55-
ignorePaths,
66+
ignoredPaths,
5667
obj[key],
57-
childPath,
68+
nestedPath,
5869
)
5970
}
6071
}

0 commit comments

Comments
 (0)