Skip to content
Merged
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
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
Loading