fix(eslint-plugin-query): detect rest destructuring on custom query hooks#10775
fix(eslint-plugin-query): detect rest destructuring on custom query hooks#10775Newbie012 wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThe PR extends the ChangesCustom Hook Detection in no-rest-destructuring Rule
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
ae64db4 to
bded2a5
Compare
…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
bded2a5 to
fc06643
Compare
| > | ||
| type Type = ReturnType<TypeChecker['getTypeAtLocation']> | ||
|
|
||
| const QUERY_RESULT_TYPE_NAMES = new Set([ |
There was a problem hiding this comment.
Not a fan of this arbitrary list. I'm open to suggestions
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts`:
- Around line 25-33: isQueryResultType currently returns true solely by matching
type.aliasSymbol.name / type.getSymbol()?.name in QUERY_RESULT_TYPE_NAMES,
causing false positives for same-named local types; update isQueryResultType to
also verify the symbol comes from TanStack query packages by inspecting
aliasSymbol.declarations / symbol.declarations and ensuring their source
module/file indicates `@tanstack/`*-query or query-core (e.g., via
declaration.getSourceFile() or module specifier checks) before accepting the
match, and preserve the existing union recursion (type.isUnion() &&
type.types.some(isQueryResultType)); you can extract a small helper like
isFromTanstackModule(declarations) to encapsulate the module-origin check and
use it in the aliasSymbol and symbol branches.
- Around line 51-53: The current code queries all overloads via
checker.getTypeAtLocation(tsNode).getCallSignatures(), which can misreport for
overloaded functions; instead obtain the TypeScript CallExpression node from
parserServices.esTreeNodeToTSNodeMap using the entire ESLint CallExpression node
(not node.callee), call checker.getResolvedSignature(...) to get the selected
Signature, and then check that signature's return type with
isQueryResultType(sig.getReturnType()); if getResolvedSignature returns
undefined, optionally fall back to the existing signatures scan to preserve
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e478a6dd-8ee1-4973-b061-4228b2c3889c
📒 Files selected for processing (6)
.changeset/no-rest-destructuring-custom-hooks.mddocs/eslint/no-rest-destructuring.mdpackages/eslint-plugin-query/src/__tests__/no-rest-destructuring.test.tspackages/eslint-plugin-query/src/__tests__/ts-fixture/react-query.d.tspackages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.rule.tspackages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts
Only run the type checker on non-direct hook calls when the binding can actually report (rest destructure or identifier), avoiding type lookups on every variable declarator. Add tests for cross-statement rest destructuring and interface-typed (non-alias) query results.
|
Actionable comments posted: 0 |
Closes #8951
🎯 Changes
The lint rule
no-rest-destructuringnow 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.Direct calls to
useQuery/useInfiniteQuery/useSuspenseQuery/useSuspenseInfiniteQuerykeep 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.Matched return types:
UseQueryResult,UseSuspenseQueryResult,UseInfiniteQueryResult,UseSuspenseInfiniteQueryResult, theirDefined*variants, and the underlyingQueryObserverResult/InfiniteQueryObserverResultnames.A note on the
recommendedTypeCheckedpreset 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
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
New Features
Documentation
Tests