Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
79b459a683f291ee810e59025a639efb25fbfacdaf2723b55cc7aaf45e35442a5c65ecd61a0f408dd540cbcf084df68e8b85fb0dea2f221c1b25ed13fd7765cfaec67e2e7f1b3029fcf69d48ea15c05dd54b108fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/eslint-plugin-query': minor | ||
| --- | ||
| Add prefer-query-options rule |
| 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' }], | ||
| }, | ||
| ], | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,6 +9,7 @@ export interface Plugin extends Omit<ESLint.Plugin, 'rules'> { | ||
| configs: { | ||
| recommended: ESLint.ConfigData | ||
| 'flat/recommended': Array<Linter.Config> | ||
| 'flat/recommendedStrict': Array<Linter.Config> | ||
| } | ||
| } | ||
| @@ -46,10 +47,29 @@ export const plugin = { | ||
| }, | ||
| }, | ||
| ], | ||
| 'flat/recommendedStrict': [ | ||
| { | ||
| name: 'tanstack/query/flat/recommendedStrict', | ||
| plugins: { | ||
| '@tanstack/query': {}, // Assigned after plugin object created | ||
| }, | ||
| rules: { | ||
| '@tanstack/query/exhaustive-deps': 'error', | ||
| '@tanstack/query/no-rest-destructuring': 'warn', | ||
| '@tanstack/query/stable-query-client': 'error', | ||
| '@tanstack/query/no-unstable-deps': 'error', | ||
| '@tanstack/query/infinite-query-property-order': 'error', | ||
| '@tanstack/query/no-void-query-fn': 'error', | ||
| '@tanstack/query/mutation-property-order': 'error', | ||
| '@tanstack/query/prefer-query-options': 'error', | ||
| }, | ||
| }, | ||
| ], | ||
danielpza marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| rules, | ||
| } satisfies Plugin | ||
| plugin.configs['flat/recommended'][0]!.plugins['@tanstack/query'] = plugin | ||
| plugin.configs['flat/recommendedStrict'][0]!.plugins['@tanstack/query'] = plugin | ||
| export default plugin | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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 }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| }) | ||
| 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 | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.