Skip to content

fix(eslint-plugin-query): detect rest destructuring on custom query hooks#10775

Draft
Newbie012 wants to merge 1 commit into
TanStack:mainfrom
Newbie012:fix-no-rest-destructuring-custom-hooks
Draft

fix(eslint-plugin-query): detect rest destructuring on custom query hooks#10775
Newbie012 wants to merge 1 commit into
TanStack:mainfrom
Newbie012:fix-no-rest-destructuring-custom-hooks

Conversation

@Newbie012
Copy link
Copy Markdown
Contributor

@Newbie012 Newbie012 commented May 24, 2026

Closes #8951

🎯 Changes

The lint rule no-rest-destructuring now also flags rest destructuring on custom hooks that return a TanStack Query result. Detection uses the TypeScript type checker and runs opportunistically, only when parser services are available, so untyped projects see no change.

const useTodos = () =>
  useQuery({ queryKey: ['todos'], queryFn: () => api.getTodos() })

// 🔴 Before: not reported.
// 🟢 After: reports "Object rest destructuring on a query will observe all
//    changes to the query, leading to excessive re-renders."
const { data, ...rest } = useTodos()

Direct calls to useQuery / useInfiniteQuery / useSuspenseQuery / useSuspenseInfiniteQuery keep reporting via the existing AST path. The type-aware path handles wrappers by checking whether the call result resolves to known TanStack Query result type names.

// 🔴 Before: not reported.
// 🟢 After: reports on the spread.
const todosQuery = useTodos()
return { ...todosQuery, data: todosQuery.data?.[0] }

Matched return types: UseQueryResult, UseSuspenseQueryResult, UseInfiniteQueryResult, UseSuspenseInfiniteQueryResult, their Defined* variants, and the underlying QueryObserverResult / InfiniteQueryObserverResult names.

A note on the recommendedTypeChecked preset from #8966: I would still like to land that preset and graduate type-aware rules into it, but the conversation has been stalled and I did not want to block this user-facing bug. Happy to follow up by moving this rule (and other type-aware rules) behind a dedicated preset whenever the maintainers want to take that direction.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 24, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5ca7b791-d214-4445-9bec-ec6217f4072b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Newbie012 Newbie012 force-pushed the fix-no-rest-destructuring-custom-hooks branch 2 times, most recently from ae64db4 to bded2a5 Compare May 24, 2026 15:06
…ooks

Adds an opportunistic type-aware path to no-rest-destructuring. When
TypeScript parser services are available, the rule resolves the call
expression's return type and reports rest destructuring on custom hooks
that return a TanStack Query result. Untyped projects keep the existing
AST-only behavior unchanged.

Closes TanStack#8951
@Newbie012 Newbie012 force-pushed the fix-no-rest-destructuring-custom-hooks branch from bded2a5 to fc06643 Compare May 24, 2026 15:16
>
type Type = ReturnType<TypeChecker['getTypeAtLocation']>

const QUERY_RESULT_TYPE_NAMES = new Set([
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this arbitrary list. I'm open to suggestions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[lint]: no-rest-destructuring does not report on custom hooks

1 participant