-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(eslint-plugin): prefer-object-syntax support fetchQuery #5301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(eslint-plugin): prefer-object-syntax support fetchQuery #5301
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 47ef424:
|
@@ -7,6 +7,7 @@ import { objectKeys } from '../../utils/object-utils' | |||
const QUERY_CALLS = { | |||
useQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' }, | |||
createQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' }, | |||
fetchQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the difference is: fetchQuery
comes from queryClient
, so it would need to be queryClient.fetchQuery
. I'd want to avoid false-positives from other, random methods called fetchQuery
. @Newbie012 FYI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your answer @TkDodo !
I could write the eslint rule to use the Identifier
node listener instead, but indeed we might encounter some false positives if someone handles functions also called fetchQuery
:/
I could do a rule on queryClient.fetchQuery
, but maybe they use a different variable name for their queryClient
😅
I am not sure what's the best way to implement this rule then. I will try to think about it ! Thanks again!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this rule was initially written in mind to support only useQuery
. Since then, other hooks needed to be supported, so I refactored the rule in a way of identifying only hooks, which is not the case for queryClient.fetchQuery
. The identification of fetchQuery
should be different than the other ways:
- Look for
fetchQuery
call expression. - Track down its source and check if it came from
useQueryClient
(I already wrote a function that tells if a given reference is a queryClient. It's not exactly the same issue as here, but it's a good starting point):
query/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts
Lines 31 to 40 in b369b6c
isQueryClientReference(reference: TSESLint.Scope.Reference) { | |
const declarator = reference.resolved?.defs[0]?.node | |
return ( | |
declarator?.type === AST_NODE_TYPES.VariableDeclarator && | |
declarator.init?.type === AST_NODE_TYPES.CallExpression && | |
declarator.init.callee.type === AST_NODE_TYPES.Identifier && | |
declarator.init.callee.name === 'useQueryClient' | |
) | |
}, |
- Apply rule checks if positive.
This way you don't have any false positives. Doesn't matter if you have a different name than queryClient
or destructure fetchQuery
from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clear thanks! I will try to give it a try in the upcoming days!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gterral any updates here? This is pretty stale and I'd close it if you're not working on this anymore, since the rule won't exist in v5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, sorry didn't took the time to work on it yet as this wasn't really urgent on my side.
(And just noticed it's been almost 2 months already 😱).
I don't think I will able to work on it during the upcoming week, so I can understand if you prefer to close the issue rather keeping it open, but if you do let it open, I promise that I will do it at one point!
Hopefully in the 3/4 weeks. (July should be quieter on my side).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could very well be that we have v5 out by then 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok!
Sorry and good luck for the v5 release then ;)!
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 47ef424. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this branch ✅ Successfully ran 4 targets
Sent with 💌 from NxCloud. |
Codecov ReportPatch and project coverage have no change.
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## main #5301 +/- ##
=======================================
Coverage 97.12% 97.12%
=======================================
Files 11 11
Lines 383 383
Branches 140 140
=======================================
Hits 372 372
Misses 11 11
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
This pull request updates the
prefer-object-syntax
eslint rule to add support for the fetchQuery methodI tried to follow the Contributing guide, please let me know if I need to change something.
Related discussion => #5300
Thank you for the awesome library ❤️