fix(eslint-plugin): validate hook import #9828
main fromrefs/pull/9828/head·merged by TkDodo View on GitHubfixes https://github.com/TanStack/query/issues/9776
Summary by CodeRabbit#
Bug Fixes#
- Improved accuracy of the ESLint plugin to correctly identify TanStack Query imports, preventing false positives when similarly-named hooks from other libraries are used.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: eslint-plugin-query
Walkthrough#
The
no-unstable-depsESLint rule was modified to only track hooks imported from Tanstack Query, eliminating false positives when users have identically-named hooks from other sources. The rule callback now receives helpers to validate Tanstack imports before tracking dependencies.Changes#
Cohort / File(s) Summary Rule Logic Enhancement
packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.tsUpdated rule creation callback signature to receive helpersparameter fromdetectTanstackQueryImports. Addedhelpers.isTanstackQueryImport()check when handlingVariableDeclaratorto ensure only hooks imported from Tanstack Query are tracked as unstable dependencies.Test Coverage Expansion
packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.tsAdded two new valid test cases verifying that useQueryanduseMutationimported from non-Tanstack sources do not trigger the rule, mirroring existing Tanstack-based test patterns.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~8 minutes
- The changes are focused and follow a clear pattern: narrowing scope by adding an import-source validation check
- Helper function usage pattern is straightforward and mirrors existing code
- Test additions are straightforward validation cases with no edge cases
- Main consideration: verify the
helpers.isTanstackQueryImport()API is correctly applied and that no legitimate Tanstack imports are inadvertently filtered
Possibly related PRs#
- #9720 — Modifies the same
VariableDeclaratorhandling in the rule but uses a different approach (combine-based skip foruseQueries), representing an alternative pattern for narrowing tracked hooks.
Suggested reviewers#
- TkDodo
- lachlancollins
Poem#
🐰 A query by another name, no longer brings shame, False alarms now silenced, the rule grows more sane, Only TanStack hooks trigger our careful inspection, Imports from elsewhere? They pass our reflection! ✨
Pre-merge checks and finishing touches#
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution Description Check ⚠️ Warning The pull request description is largely incomplete compared to the required template. The author provided only a single-line reference to the issue ("fixes https://github.com/TanStack/query/issues/9776") without completing any of the template sections. The required sections—including the "🎯 Changes" section with detailed description and motivation, the "✅ Checklist" with contributing guide and testing confirmation, and the "🚀 Release Impact" section—are all missing or empty. This minimal description does not follow the repository's template structure and lacks important information about the changes and their impact. Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstringsto improve docstring coverage.✅ Passed checks (3 passed)
Check name Status Explanation Title Check ✅ Passed The pull request title "fix(eslint-plugin): validate hook import" is concise, specific, and accurately describes the main change in the changeset. The title clearly communicates the primary objective: adding validation to check whether hooks are imported from TanStack Query. This aligns well with the code modifications that add a helpers check to verify hook imports, addressing the core issue of false positives in the no-unstable-deps rule. Linked Issues Check ✅ Passed The code changes directly address the requirements specified in linked issue #9776. The modifications validate that hooks like useQuery are specifically imported from TanStack Query by adding a helpers.isTanstackQueryImport check to the rule logic. This distinction between TanStack and non-TanStack imports eliminates the false positives reported in the issue. Additionally, two new test cases were added to verify that non-TanStack useQuery hooks are no longer flagged as unstable, while the rule continues to function correctly for actual TanStack Query imports. All coding-related objectives from the issue are met by these changes. Out of Scope Changes Check ✅ Passed All changes in this pull request are directly scoped to addressing the false positive issue in the no-unstable-deps eslint rule. The modifications are limited to the rule implementation file and its test file, both of which are necessary to fix and verify the specific problem. There are no unrelated changes, refactoring efforts, or modifications to other features or packages that would fall outside the scope of issue #9776. ✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
- Commit unit tests in branch `fix-issue-9776`
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 38b40081cedd207285ac0b4bb6818a2e84704b95 and a64f614b62fa2a0554085681770ca7a3f7aaa059.
📒 Files selected for processing (2)
packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts(1 hunks)packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts (1)
packages/eslint-plugin-query/src/utils/detect-react-query-imports.ts (1)
detectTanstackQueryImports(24-97)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (3)
packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts (2)
37-37: LGTM! Signature correctly updated to receive helpers.The addition of the
helpersparameter enables the rule to validate hook import sources, which is essential for fixing the false positive issue.
112-113: Excellent fix! Import validation eliminates false positives.The addition of
helpers.isTanstackQueryImport(node.init.callee)ensures the rule only tracks hooks actually imported from TanStack Query packages, directly addressing issue #9776. The condition ordering is efficient, checking the name first before the more expensive import validation.packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts (1)
66-94: Excellent test coverage for the fix!These test cases directly validate that the rule no longer produces false positives for non-TanStack hooks with identical names, which was the core issue in #9776. The tests cover both query and mutation hook types and follow the existing test patterns well.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands and usage tips.View your CI Pipeline Execution ↗ for commit a64f614b62fa2a0554085681770ca7a3f7aaa059
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 5s View ↗
☁️ Nx Cloud last updated this comment at
2025-10-29 16:14:45UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9828@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9828@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9828@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9828@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9828@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9828@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9828@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9828@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9828@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9828@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9828@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9828@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9828@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9828@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9828@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9828@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9828@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9828@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9828@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9828commit:
a64f614Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 83.59%. Comparing base (
38b4008) to head (a64f614). :warning: Report is 1 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9828 +/- ## =========================================== + Coverage 45.74% 83.59% +37.85% =========================================== Files 200 19 -181 Lines 8404 573 -7831 Branches 1919 213 -1706 =========================================== - Hits 3844 479 -3365 + Misses 4112 72 -4040 + Partials 448 22 -426Components Coverage Δ @tanstack/angular-query-experimental ∅ <ø> (∅)@tanstack/eslint-plugin-query 83.59% <100.00%> (+0.02%):arrow_up: @tanstack/query-async-storage-persister ∅ <ø> (∅)@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)@tanstack/query-codemods ∅ <ø> (∅)@tanstack/query-core ∅ <ø> (∅)@tanstack/query-devtools ∅ <ø> (∅)@tanstack/query-persist-client-core ∅ <ø> (∅)@tanstack/query-sync-storage-persister ∅ <ø> (∅)@tanstack/query-test-utils ∅ <ø> (∅)@tanstack/react-query ∅ <ø> (∅)@tanstack/react-query-devtools ∅ <ø> (∅)@tanstack/react-query-next-experimental ∅ <ø> (∅)@tanstack/react-query-persist-client ∅ <ø> (∅)@tanstack/solid-query ∅ <ø> (∅)@tanstack/solid-query-devtools ∅ <ø> (∅)@tanstack/solid-query-persist-client ∅ <ø> (∅)@tanstack/svelte-query ∅ <ø> (∅)@tanstack/svelte-query-devtools ∅ <ø> (∅)@tanstack/svelte-query-persist-client ∅ <ø> (∅)@tanstack/vue-query ∅ <ø> (∅)@tanstack/vue-query-devtools ∅ <ø> (∅):rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
TkDodo approved these changes
TkDodo merged this1986929
TkDodo closed this
Sign in to join this conversation.
⚠️ No Changeset found#
Latest commit: a64f614b62fa2a0554085681770ca7a3f7aaa059
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR