Skip to content

Commit

Permalink
fix(eslint-plugin-query): handle callable params within exaustive-dep…
Browse files Browse the repository at this point in the history
…s rule (#8150)

Closes #8093

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
pawel-twardziak and TkDodo authored Oct 9, 2024
1 parent f1f2529 commit eb2f9d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,44 @@ ruleTester.run('exhaustive-deps', rule, {
});
`,
},
{
name: 'should not fail when queryKey uses arrow function to produce a key',
code: normalizeIndent`
const obj = reactive<{ boo?: string }>({});
const query = useQuery({
queryKey: ['foo', () => obj.boo],
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
enable: () => !!obj.boo,
});
`,
},
{
name: 'should not fail when queryKey uses arrow function to produce a key as the body return',
code: normalizeIndent`
const obj = reactive<{ boo?: string }>({});
const query = useQuery({
queryKey: ['foo', () => { return obj.boo }],
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
enable: () => !!obj.boo,
});
`,
},
{
name: 'should not fail when queryKey uses function expression to produce a key as the body return',
code: normalizeIndent`
const obj = reactive<{ boo?: string }>({});
const query = useQuery({
queryKey: ['foo', function() {
return obj.boo
}],
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
enable: () => !!obj.boo,
});
`,
},
],
invalid: [
{
Expand Down
18 changes: 18 additions & 0 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ export const ASTUtils = {
identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))
}

if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))
}

if (node.type === AST_NODE_TYPES.FunctionExpression) {
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))
}

if (node.type === AST_NODE_TYPES.BlockStatement) {
identifiers.push(
...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat(),
)
}

if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {
identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))
}

return identifiers
},
isAncestorIsCallee(identifier: TSESTree.Node) {
Expand Down

0 comments on commit eb2f9d3

Please sign in to comment.