Skip to content
Closed
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
9 changes: 8 additions & 1 deletion packages/fiber/src/core/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,16 @@ export function resolve(root: any, key: string): { root: any; key: string; targe
let target: unknown = root[key]
if (!key.includes('-')) return { root, key, target }

const chain = key.split('-')

// If the first part of the chain is not a property of the root, there is no need to continue processing the remaining chained queries.
if (!root.hasOwnProperty(chain[0])) {
return { root, key, target }
}

// Resolve pierced target
target = root
for (const part of key.split('-')) {
for (const part of chain) {
key = part
root = target
target = (target as any)?.[key]
Expand Down
9 changes: 9 additions & 0 deletions packages/fiber/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ describe('resolve', () => {
expect(key).toBe('bar')
expect(target).toBe(root[key])
})

it ('should return the original root and key when the first part of the chain is not a property of the root', () => {
const object = { foo: { bar: 1 } }
const { root, key, target } = resolve(object, 'foo2-bar')

expect(root).toBe(object)
expect(key).toBe('foo2-bar')
expect(target).toBe(undefined)
})
})

describe('attach / detach', () => {
Expand Down
Loading