Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,26 @@ const baseTestCases = {
}
`,
})),
),
).concat([
{
name: `should pass when useQueries with combine is passed to ${reactHookAlias} as dependency`,
code: `
${reactHookImport}
import { useQueries } from "@tanstack/react-query";

function Component() {
const queries = useQueries({
queries: [
{ queryKey: ['test'], queryFn: () => 'test' }
],
combine: (results) => ({ data: results[0]?.data })
});
const callback = ${reactHookInvocation}(() => { queries.data }, [queries]);
return;
}
`,
},
]),
invalid: ({
reactHookImport,
reactHookInvocation,
Expand Down Expand Up @@ -88,7 +107,31 @@ const baseTestCases = {
},
],
})),
),
).concat([
{
name: `result of useQueries without combine is passed to ${reactHookInvocation} as dependency`,
code: `
${reactHookImport}
import { useQueries } from "@tanstack/react-query";

function Component() {
const queries = useQueries({
queries: [
{ queryKey: ['test'], queryFn: () => 'test' }
]
});
const callback = ${reactHookInvocation}(() => { queries[0]?.data }, [queries]);
return;
}
`,
errors: [
{
messageId: 'noUnstableDeps',
data: { reactHook: reactHookAlias, queryHook: 'useQueries' },
},
],
},
]),
}

const testCases = (reactHookName: string) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export const rule = createRule({
}
}

function hasCombineProperty(callExpression: TSESTree.CallExpression): boolean {
if (callExpression.arguments.length === 0) return false

const firstArg = callExpression.arguments[0]
if (!firstArg || firstArg.type !== AST_NODE_TYPES.ObjectExpression) return false

return firstArg.properties.some(prop =>
prop.type === AST_NODE_TYPES.Property &&
prop.key.type === AST_NODE_TYPES.Identifier &&
prop.key.name === 'combine'
)
}

return {
ImportDeclaration(node: TSESTree.ImportDeclaration) {
if (
Expand Down Expand Up @@ -94,6 +107,11 @@ export const rule = createRule({
node.init.callee.type === AST_NODE_TYPES.Identifier &&
allHookNames.includes(node.init.callee.name)
) {
// Special case for useQueries with combine property - it's stable
if (node.init.callee.name === 'useQueries' && hasCombineProperty(node.init)) {
// Don't track useQueries with combine as unstable
return
}
collectVariableNames(node.id, node.init.callee.name)
}
},
Expand Down
Loading