-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(eslint-plugin-query): add prefer-query-options rule #10184
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
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
79b459a
dump
danielpza 683f291
dump
danielpza ee810e5
fix
danielpza 9025a63
update rules
danielpza 9efb25f
combine rules
danielpza bfacdaf
small cleanup
danielpza 2723b55
update messages
danielpza cc7aaf4
fix test
danielpza 5e35442
dump
danielpza a5c65ec
update changeset
danielpza d61a0f4
Update rule description
danielpza 08dd540
move utils to the same folder
danielpza cbcf084
Merge branch 'main' into eslint-query-options-plugin
danielpza df68e8b
add recommendedStrict config
danielpza 85fb0de
Fix eslint warnings
danielpza a2f221c
Fix coderabbit suggestion
danielpza 1b25ed1
fix coderabbit warning
danielpza 3fd7765
remove use*Queries check since it's not working
danielpza cfaec67
add more tests
danielpza e2e7f1b
remove rule from recommended config
danielpza 3029fcf
fix
danielpza 69d48ea
enable rule as error in recommended strict config
danielpza 15c05dd
update
danielpza 54b108f
apply coderabbit suggestionGeneralize invalidate
danielpza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/eslint-plugin-query': minor | ||
| --- | ||
|
|
||
| Add prefer-query-options rule |
76 changes: 76 additions & 0 deletions
76
packages/eslint-plugin-query/src/__tests__/prefer-query-options.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester' | ||
| import { afterAll, describe, it } from 'vitest' | ||
| import { rule } from '../rules/prefer-query-options/prefer-query-options.rule' | ||
|
|
||
| const ruleTester = new RuleTester() | ||
|
|
||
| RuleTester.afterAll = afterAll | ||
| RuleTester.describe = describe | ||
| RuleTester.it = it | ||
|
|
||
| // useQuery hooks | ||
| ruleTester.run(rule.name, rule, { | ||
| valid: [ | ||
| { code: `useQuery(usersQuery)` }, | ||
| { code: `useQuery({ ...usersQuery })` }, | ||
| { code: `useQuery({ ...usersQuery() })` }, | ||
| { code: `useQuery({ ...usersQuery, meta: {} })` }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `useQuery({ queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `const users = useQuery({ ...queryOptions, queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `const users = useQuery({ queryFn: () => {} })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `const users = useQuery({ ...queryOptions, queryFn: () => {} })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `useInfiniteQuery({ queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `useSuspenseQuery({ queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| { | ||
| code: `useSuspenseInfiniteQuery({ queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-hook' }], | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| // queryClient.invalidateQueries expressions | ||
| ruleTester.run(rule.name, rule, { | ||
| valid: [ | ||
| { code: `queryClient.invalidateQueries(usersQuery)` }, | ||
| { code: `queryClient.invalidateQueries({ ...usersQuery })` }, | ||
| { code: `queryClient.invalidateQueries({ ...usersQuery() })` }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `queryClient.invalidateQueries({ queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-invalidate' }], | ||
| }, | ||
| { | ||
| code: `queryClient.invalidateQueries({ ...queryOptions, queryKey: [] })`, | ||
| errors: [{ messageId: 'no-inline-query-invalidate' }], | ||
| }, | ||
| { | ||
| code: `queryClient.invalidateQueries({ queryFn: () => {} })`, | ||
| errors: [{ messageId: 'no-inline-query-invalidate' }], | ||
| }, | ||
| { | ||
| code: `queryClient.invalidateQueries({ ...queryOptions, queryFn: () => {} })`, | ||
| errors: [{ messageId: 'no-inline-query-invalidate' }], | ||
| }, | ||
| ], | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/eslint-plugin-query/src/rules/prefer-query-options/prefer-query-options.rule.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils' | ||
| import { getDocsUrl } from '../../utils/get-docs-url' | ||
| import { detectQueryOptionsInObject } from './prefer-query-options.utils' | ||
| import type { TSESTree } from '@typescript-eslint/utils' | ||
| import type { ExtraRuleDocs } from '../../types' | ||
|
|
||
| export const name = 'prefer-query-options' | ||
|
|
||
| const useQueryHooks = [ | ||
| // see https://tanstack.com/query/latest/docs/framework/react/reference/useQuery | ||
| 'useQuery', | ||
| // 'useQueries', // only works for single queries for now | ||
| 'useInfiniteQuery', | ||
| 'useSuspenseQuery', | ||
| // 'useSuspenseQueries', | ||
| 'useSuspenseInfiniteQuery', | ||
| ] | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl) | ||
|
|
||
| /** @returns true if it's a `useQuery` hook call expression node */ | ||
| function isQueryHookCallExpression(node: TSESTree.CallExpression) { | ||
| if (node.callee.type !== AST_NODE_TYPES.Identifier) return false | ||
| if (!useQueryHooks.includes(node.callee.name)) return false | ||
| return true | ||
| } | ||
|
|
||
| /** @returns true if it's a call to `*.invalidateQueries` */ | ||
| function isInvalidateQueriesCallExpression(node: TSESTree.CallExpression) { | ||
| return ( | ||
| node.callee.type === AST_NODE_TYPES.MemberExpression && | ||
| node.callee.property.type === AST_NODE_TYPES.Identifier && | ||
| node.callee.property.name === 'invalidateQueries' | ||
| ) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| export const rule = createRule({ | ||
| name, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Ensures queryOptions constructor pattern is used when calling query apis', | ||
| recommended: 'strict', | ||
| }, | ||
| messages: { | ||
| 'no-inline-query-hook': 'Expected query hook to use queryOptions pattern', | ||
| 'no-inline-query-invalidate': | ||
| 'Expected query invalidate call to use queryOptions pattern', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| return { | ||
| CallExpression(node) { | ||
| // use*Query hook call | ||
| if ( | ||
| isQueryHookCallExpression(node) && | ||
| node.arguments[0] && | ||
| detectQueryOptionsInObject(node.arguments[0]) | ||
| ) { | ||
| context.report({ messageId: 'no-inline-query-hook', node }) | ||
| } | ||
|
|
||
| // queryClient.invalidateQueries call | ||
| if ( | ||
| isInvalidateQueriesCallExpression(node) && | ||
| node.arguments[0] && | ||
| detectQueryOptionsInObject(node.arguments[0]) | ||
| ) { | ||
| context.report({ messageId: 'no-inline-query-invalidate', node }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| }) | ||
23 changes: 23 additions & 0 deletions
23
packages/eslint-plugin-query/src/rules/prefer-query-options/prefer-query-options.utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/utils' | ||
| import type { TSESTree } from '@typescript-eslint/utils' | ||
|
|
||
| const MAIN_QUERY_PROPERTIES = ['queryKey', 'queryFn'] | ||
|
|
||
| /** | ||
| * @returns true if the node is an object that has main query options (ie queryKey or queryFn). | ||
| * This is used for detecting inline query options in hooks and functions | ||
| */ | ||
| export function detectQueryOptionsInObject(queryNode: TSESTree.Node): boolean { | ||
| // skip if it's not an object | ||
| if (queryNode.type !== AST_NODE_TYPES.ObjectExpression) return false | ||
|
|
||
| // check if any of the properties is queryKey or queryFn | ||
| const hasMainQueryProperties = queryNode.properties.find( | ||
| (property) => | ||
| property.type === AST_NODE_TYPES.Property && | ||
| property.key.type === AST_NODE_TYPES.Identifier && | ||
| MAIN_QUERY_PROPERTIES.includes(property.key.name), | ||
| ) | ||
|
|
||
| return !!hasMainQueryProperties | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.