From a64f614b62fa2a0554085681770ca7a3f7aaa059 Mon Sep 17 00:00:00 2001 From: Eliya Cohen Date: Wed, 29 Oct 2025 18:12:31 +0200 Subject: [PATCH] fix(eslint-plugin): validate hook import --- .../src/__tests__/no-unstable-deps.test.ts | 28 +++++++++++++++++++ .../no-unstable-deps/no-unstable-deps.rule.ts | 5 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts b/packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts index 1c97152add..b111fc0ed7 100644 --- a/packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts +++ b/packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts @@ -63,6 +63,34 @@ const baseTestCases = { } `, }, + ]) + .concat([ + { + name: `should pass when useQuery is imported from non-TanStack source and used with ${reactHookAlias}`, + code: ` + ${reactHookImport} + import { useQuery } from "./router"; + + function Component() { + const query = useQuery(); + const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]); + return; + } + `, + }, + { + name: `should pass when useMutation is imported from non-TanStack source and used with ${reactHookAlias}`, + code: ` + ${reactHookImport} + import { useMutation } from "./api"; + + function Component() { + const mutation = useMutation(); + const callback = ${reactHookInvocation}(() => { mutation.mutate() }, [mutation]); + return; + } + `, + }, ]), invalid: ({ reactHookImport, diff --git a/packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts b/packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts index 7d643f4ec1..32e6846468 100644 --- a/packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts +++ b/packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts @@ -34,7 +34,7 @@ export const rule = createRule({ }, defaultOptions: [], - create: detectTanstackQueryImports((context) => { + create: detectTanstackQueryImports((context, _options, helpers) => { const trackedVariables: Record = {} const hookAliasMap: Record = {} @@ -109,7 +109,8 @@ export const rule = createRule({ node.init !== null && node.init.type === AST_NODE_TYPES.CallExpression && node.init.callee.type === AST_NODE_TYPES.Identifier && - allHookNames.includes(node.init.callee.name) + allHookNames.includes(node.init.callee.name) && + helpers.isTanstackQueryImport(node.init.callee) ) { // Special case for useQueries with combine property - it's stable if (