fix: allow useQueries with combine property in no-unstable-deps rule #9720
main fromrefs/pull/9720/head·merged by TkDodo View on GitHub- Add hasCombineProperty function to detect combine property in useQueries calls
- Skip tracking useQueries with combine as unstable since combine makes result stable
- Add tests for useQueries with and without combine property
- Fixes #9718
Changes#
Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm test:pr.
Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Linting now treats useQueries calls that include a combine option as stable, avoiding false-positive no-unstable-deps warnings in React hooks.
Tests
- Added tests validating useQueries scenarios with and without combine, confirming correct diagnostics for stable and unstable dependency patterns (including when the queries object is used as a hook dependency).
- 1
Caution
Review failed#
The pull request is closed.
Walkthrough#
Adds handling so
useQueriescalls with acombineproperty are treated as stable (skipped) by the no-unstable-deps rule, and extends tests with valid and invalid cases foruseQuerieswith and withoutcombine.Changes#
Cohort / File(s) Summary Rule logic: handle combine in useQueries packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.tsAdds hasCombineProperty(callExpression)and an early-return inVariableDeclaratorhandling whenuseQueriesis called with acombineproperty so the resulting variable is not tracked as unstable.Tests: extend valid/invalid cases packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.tsAppends new valid and invalid test cases covering useQuerieswith acombinefunction (treated as stable) anduseQuerieswithoutcombine(still reported), updating concatenation logic to include these scenarios.Changelog/metadata .changeset/ten-crabs-glow.mdAdds a changeset bump documenting "allow useQueries with combine property in no-unstable-deps rule." Sequence Diagram(s)#
sequenceDiagram autonumber actor Dev as Developer Code participant ESLint as no-unstable-deps Rule participant AST as AST Walker Dev->>ESLint: Run lint on file ESLint->>AST: Visit VariableDeclarator (init is CallExpression) alt callee is useQueries with combine note right of ESLint #a9d18e: hasCombineProperty -> treat as stable ESLint-->>AST: Skip tracking variable else callee is useQueries without combine note right of ESLint #f4c7c3: No combine -> treat as unstable ESLint->>AST: Track variable as unstable end AST->>ESLint: Visit React Hook call with deps alt deps reference tracked (unstable) variable ESLint-->>Dev: Report noUnstableDeps diagnostic else deps reference skipped/stable variable ESLint-->>Dev: No diagnostic endEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested reviewers#
- Newbie012
Poem#
A whisker twitch, a combine switch, hooray! I skip the chase where stable things lay. No noisy lint, just calm delight— I hop and nibble through the night. Carrots for code that’s doing right. 🥕
Pre-merge checks and finishing touches#
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution Description Check ⚠️ Warning The pull request description does not follow the repository’s template because the “## 🎯 Changes” section is missing or empty, the placeholder under "## Changes" remains unfilled, and section headings do not match the required template structure. Please restructure the description to use the “## 🎯 Changes” header with a filled summary of changes, remove or replace template placeholders under “## Changes,” and ensure all required sections and headings match the repository’s description template. 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 title clearly and concisely summarizes the main change by stating that the no-unstable-deps rule is fixed to allow useQueries with the combine property, matching the PR’s purpose and following conventional commit style. Linked Issues Check ✅ Passed The changes implement a helper to detect the combine property and skip tracking for useQueries with combine, update the ESLint rule accordingly, and include tests for both combine and non-combine scenarios, fully addressing the objectives from issue #9718. Out of Scope Changes Check ✅ Passed All modifications are confined to the no-unstable-deps ESLint rule and its test file to support useQueries with combine, and there are no unrelated or extraneous changes outside the linked issue’s scope.
📜 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 28e98fac718fdea890dd9e29844d1a217dfb9686 and 8517ada8a0fa481fd13e04ec9c9b7b54e4a7557f.
📒 Files selected for processing (1)
.changeset/ten-crabs-glow.md(1 hunks)
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.github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: eslint-plugin-query
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts (1)
70-81: Consider edge cases in combine property detection.The function correctly identifies standard
combineproperties, but may not handle all edge cases:- Spread properties:
{ ...config, queries: [...] }won't be detected ifcombineis in the spread - Computed property names:
{ ['combine']: fn }won't match since you checkprop.key.type === AST_NODE_TYPES.Identifier - Property shorthand: While
{ combine }should work, the function assumes the property key name is what matters
For robustness, consider adding a check for computed properties:
function hasCombineProperty(callExpression: TSESTree.CallExpression): boolean { if (callExpression.arguments.length === 0) return false const firstArg = callExpression.arguments[0] if (!firstArg || firstArg.type !== AST_NODE_TYPES.ObjectExpression) return false return firstArg.properties.some(prop => prop.type === AST_NODE_TYPES.Property && + !prop.computed && prop.key.type === AST_NODE_TYPES.Identifier && prop.key.name === 'combine' ) }This ensures computed properties like
{ ['combine']: fn }are excluded (though they're unlikely in practice).packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts (1)
110-134: Consider adding invalid test case foruseSuspenseQuerieswithout combine.The invalid test case correctly verifies that
useQuerieswithoutcombinetriggers the rule. For completeness and symmetry with the valid test cases, consider adding a corresponding invalid test foruseSuspenseQuerieswithoutcombine.Add this test case to the invalid array:
{ name: `result of useSuspenseQueries without combine is passed to ${reactHookInvocation} as dependency`, code: ` ${reactHookImport} import { useSuspenseQueries } from "@tanstack/react-query"; function Component() { const queries = useSuspenseQueries({ queries: [ { queryKey: ['test'], queryFn: () => 'test' } ] }); const callback = ${reactHookInvocation}(() => { queries[0]?.data }, [queries]); return; } `, errors: [ { messageId: 'noUnstableDeps', data: { reactHook: reactHookAlias, queryHook: 'useSuspenseQueries' }, }, ], },📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 9f82d8e56f00889db76729db03393bf972bef5c0 and c6cfbf03120790ae4d91d5686a7f6423bb9f4617.
📒 Files selected for processing (2)
packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts(2 hunks)packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts(2 hunks)
packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts lines 45 to 64OutdatedResolvedby alimttth(GitHub profile, opens in a new tab)@@ -45 +45,20 @@45- ),45+ ).concat([46+ {47+ name: `should pass when useQueries with combine is passed to ${reactHookAlias} as dependency`,48+ code: `49+ ${reactHookImport}50+ import { useQueries } from "@tanstack/react-query";51+52+ function Component() {53+ const queries = useQueries({54+ queries: [55+ { queryKey: ['test'], queryFn: () => 'test' }56+ ],57+ combine: (results) => ({ data: results[0]?.data })58+ });59+ const callback = ${reactHookInvocation}(() => { queries.data }, [queries]);60+ return;61+ }62+ `,63+ },64+ ]),🛠️ Refactor suggestion | 🟠 Major
Add test coverage for
useSuspenseQuerieswith combine.The valid test case correctly covers
useQuerieswithcombine, but there's no corresponding test foruseSuspenseQuerieswithcombine, which should also be treated as stable per the PR objectives.Add a test case for
useSuspenseQueries:{ name: `should pass when useSuspenseQueries with combine is passed to ${reactHookAlias} as dependency`, code: ` ${reactHookImport} import { useSuspenseQueries } from "@tanstack/react-query"; function Component() { const queries = useSuspenseQueries({ queries: [ { queryKey: ['test'], queryFn: () => 'test' } ], combine: (results) => ({ data: results[0]?.data }) }); const callback = ${reactHookInvocation}(() => { queries.data }, [queries]); return; } `, },🤖 Prompt for AI Agents
In packages/eslint-plugin-query/src/__tests__/no-unstable-deps.test.ts around lines 45 to 64, the test suite adds a valid case for useQueries with combine but is missing the equivalent case for useSuspenseQueries; add a new test object mirroring the useQueries case but importing and calling useSuspenseQueries (same queries array and combine function) and set the test name to "should pass when useSuspenseQueries with combine is passed to ${reactHookAlias} as dependency" so the rule treats useSuspenseQueries with combine as stable.packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts lines 114 to 121OutdatedResolvedby alimttth(GitHub profile, opens in a new tab)@@ -94,3 +107,8 @@94107 node.init.callee.type === AST_NODE_TYPES.Identifier &&95108 allHookNames.includes(node.init.callee.name)96109 ) {110+ // Special case for useQueries with combine property - it's stable111+ if (node.init.callee.name === 'useQueries' && hasCombineProperty(node.init)) {112+ // Don't track useQueries with combine as unstable113+ return114+ }⚠️ Potential issue | 🟠 Major
Missing support for
useSuspenseQuerieswith combine.The PR objectives and issue #9718 mention that both
useQueriesanduseSuspenseQuerieswith acombineproperty should be treated as stable. However, this implementation only checks foruseQueries.Apply this diff to include
useSuspenseQueries:- // Special case for useQueries with combine property - it's stable - if (node.init.callee.name === 'useQueries' && hasCombineProperty(node.init)) { - // Don't track useQueries with combine as unstable + // Special case for useQueries/useSuspenseQueries with combine property - it's stable + if ((node.init.callee.name === 'useQueries' || node.init.callee.name === 'useSuspenseQueries') && hasCombineProperty(node.init)) { + // Don't track useQueries/useSuspenseQueries with combine as unstable return }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change114+ // Special case for useQueries/useSuspenseQueries with combine property - it's stable115+ if (116+ (node.init.callee.name === 'useQueries' ||117+ node.init.callee.name === 'useSuspenseQueries') &&118+ hasCombineProperty(node.init)119+ ) {120+ // Don't track useQueries/useSuspenseQueries with combine as unstable121+ return122+ }🤖 Prompt for AI Agents
In packages/eslint-plugin-query/src/rules/no-unstable-deps/no-unstable-deps.rule.ts around lines 110 to 114, the special-case only checks for useQueries with a combine property but should also treat useSuspenseQueries with combine as stable; update the condition to check for either callee name 'useQueries' or 'useSuspenseQueries' (e.g. name === 'useQueries' || name === 'useSuspenseQueries') and keep the existing hasCombineProperty(node.init) check so both hooks with a combine property early-return and are not tracked as unstable.- Spread properties:
View your CI Pipeline Execution ↗ for commit 07a1be7c0732bc2ad217bd77e82cbd08f78fb597
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 7s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 6s View ↗
☁️ Nx Cloud last updated this comment at
2025-10-16 08:12:37UTCTkDodo approved these changes
More 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-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-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-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@9720@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9720@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9720@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9720@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9720@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9720@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9720@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9720@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9720@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9720@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9720@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9720@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9720@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9720@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9720@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9720@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9720@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9720@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9720@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9720commit:
07a1be7Codecov Report#
:x: Patch coverage is
92.30769%with1 linein your changes missing coverage. Please review. :white_check_mark: Project coverage is 83.56%. Comparing base (b59925e) to head (07a1be7).Additional details and impacted files
@@ Coverage Diff @@ ## main #9720 +/- ## =========================================== + Coverage 45.56% 83.56% +38.00% =========================================== Files 196 19 -177 Lines 8327 572 -7755 Branches 1895 212 -1683 =========================================== - Hits 3794 478 -3316 + Misses 4091 72 -4019 + Partials 442 22 -420Components Coverage Δ @tanstack/angular-query-experimental ∅ <ø> (∅)@tanstack/eslint-plugin-query 83.56% <92.30%> (+0.20%):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 merged this9595254
TkDodo closed this
@TkDodo is there a way to also update the recommendation to use
combineto make it a stable reference?The result of useQueries is not referentially stable, so don't pass it directly into the dependencies array of useCallback. Instead, destructure the return value of useQueries and pass the destructured values into the dependency array of useCallback.
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 8517ada8a0fa481fd13e04ec9c9b7b54e4a7557f
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR