Skip to content

Commit a64f614

Browse files
committed
fix(eslint-plugin): validate hook import
1 parent 38b4008 commit a64f614

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ const baseTestCases = {
6363
}
6464
`,
6565
},
66+
])
67+
.concat([
68+
{
69+
name: `should pass when useQuery is imported from non-TanStack source and used with ${reactHookAlias}`,
70+
code: `
71+
${reactHookImport}
72+
import { useQuery } from "./router";
73+
74+
function Component() {
75+
const query = useQuery();
76+
const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
77+
return;
78+
}
79+
`,
80+
},
81+
{
82+
name: `should pass when useMutation is imported from non-TanStack source and used with ${reactHookAlias}`,
83+
code: `
84+
${reactHookImport}
85+
import { useMutation } from "./api";
86+
87+
function Component() {
88+
const mutation = useMutation();
89+
const callback = ${reactHookInvocation}(() => { mutation.mutate() }, [mutation]);
90+
return;
91+
}
92+
`,
93+
},
6694
]),
6795
invalid: ({
6896
reactHookImport,

packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const rule = createRule({
3434
},
3535
defaultOptions: [],
3636

37-
create: detectTanstackQueryImports((context) => {
37+
create: detectTanstackQueryImports((context, _options, helpers) => {
3838
const trackedVariables: Record<string, string> = {}
3939
const hookAliasMap: Record<string, string> = {}
4040

@@ -109,7 +109,8 @@ export const rule = createRule({
109109
node.init !== null &&
110110
node.init.type === AST_NODE_TYPES.CallExpression &&
111111
node.init.callee.type === AST_NODE_TYPES.Identifier &&
112-
allHookNames.includes(node.init.callee.name)
112+
allHookNames.includes(node.init.callee.name) &&
113+
helpers.isTanstackQueryImport(node.init.callee)
113114
) {
114115
// Special case for useQueries with combine property - it's stable
115116
if (

0 commit comments

Comments
 (0)