Skip to content

Commit

Permalink
fix(eslint-plugin-query): handle first argument reference edge case (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 committed Nov 4, 2022
1 parent 292cc54 commit 17c642d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -40,6 +40,14 @@ ruleTester.run(name, rule, {
useQuery(queryKey, queryFn, { enabled });
`,
},
{
code: normalizeIndent`
import { useQuery } from "@tanstack/react-query";
const getPosts = async () => Promise.resolve([]);
const postsQuery = { queryKey: ["posts"], queryFn: () => getPosts() };
const usePosts = () => useQuery(postsQuery);
`,
},
],

invalid: [
Expand Down
Expand Up @@ -34,11 +34,22 @@ export const rule = createRule({
return
}

const firstArgument = node.arguments[0]
let firstArgument = node.arguments[0]
if (!firstArgument) {
return
}

const reference = context
.getScope()
.references.find((ref) => ref.identifier === firstArgument)

if (
reference?.resolved?.defs[0]?.node.type === 'VariableDeclarator' &&
reference.resolved.defs[0].node.init?.type === 'ObjectExpression'
) {
firstArgument = reference.resolved.defs[0].node.init
}

const hasFirstObjectArgument = firstArgument.type === 'ObjectExpression'
if (hasFirstObjectArgument) {
return
Expand Down

0 comments on commit 17c642d

Please sign in to comment.