@tanstack/eslint-plugin-query 5.72.1 → 5.73.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist/index.js → build/legacy/chunk-4ISZM335.js} +20 -51
- package/build/legacy/chunk-4ISZM335.js.map +1 -0
- package/build/legacy/index.cjs +1013 -0
- package/build/legacy/index.cjs.map +1 -0
- package/{dist → build/legacy}/index.d.cts +4 -11
- package/{dist → build/legacy}/index.d.ts +4 -11
- package/build/legacy/index.js +47 -0
- package/build/legacy/index.js.map +1 -0
- package/build/legacy/rules.cjs +973 -0
- package/build/legacy/rules.cjs.map +1 -0
- package/build/legacy/rules.d.cts +6 -0
- package/build/legacy/rules.d.ts +6 -0
- package/build/legacy/rules.js +7 -0
- package/build/legacy/rules.js.map +1 -0
- package/build/legacy/types.cjs +19 -0
- package/build/legacy/types.cjs.map +1 -0
- package/build/legacy/types.d.cts +5 -0
- package/build/legacy/types.d.ts +5 -0
- package/build/legacy/types.js +1 -0
- package/build/legacy/types.js.map +1 -0
- package/{dist/rules.js → build/modern/chunk-6ZHBB3IQ.js} +2 -1
- package/build/modern/chunk-6ZHBB3IQ.js.map +1 -0
- package/{dist → build/modern}/index.cjs +10 -5
- package/build/modern/index.cjs.map +1 -0
- package/build/modern/index.d.cts +17 -0
- package/build/modern/index.d.ts +17 -0
- package/build/modern/index.js +47 -0
- package/build/modern/index.js.map +1 -0
- package/build/modern/rules.cjs.map +1 -0
- package/build/modern/rules.d.cts +6 -0
- package/build/modern/rules.d.ts +6 -0
- package/build/modern/rules.js +7 -0
- package/build/modern/rules.js.map +1 -0
- package/build/modern/types.cjs +20 -0
- package/build/modern/types.cjs.map +1 -0
- package/build/modern/types.d.cts +5 -0
- package/build/modern/types.d.ts +5 -0
- package/build/modern/types.js +1 -0
- package/build/modern/types.js.map +1 -0
- package/package.json +12 -10
- package/src/index.ts +54 -0
- package/src/rules/exhaustive-deps/exhaustive-deps.rule.ts +184 -0
- package/src/rules/exhaustive-deps/exhaustive-deps.utils.ts +41 -0
- package/src/rules/infinite-query-property-order/constants.ts +17 -0
- package/src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts +107 -0
- package/src/rules/infinite-query-property-order/infinite-query-property-order.utils.ts +67 -0
- package/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts +115 -0
- package/src/rules/no-rest-destructuring/no-rest-destructuring.utils.ts +11 -0
- package/src/rules/no-unstable-deps/no-unstable-deps.rule.ts +129 -0
- package/src/rules/no-void-query-fn/no-void-query-fn.rule.ts +84 -0
- package/src/rules/stable-query-client/stable-query-client.rule.ts +88 -0
- package/src/rules.ts +25 -0
- package/src/types.ts +3 -0
- package/src/utils/ast-utils.ts +391 -0
- package/src/utils/detect-react-query-imports.ts +97 -0
- package/src/utils/get-docs-url.ts +2 -0
- package/src/utils/unique-by.ts +3 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/rules.cjs.map +0 -1
- package/dist/rules.js.map +0 -1
- /package/{dist → build/modern}/rules.cjs +0 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const infiniteQueryFunctions = [
|
|
2
|
+
'infiniteQueryOptions',
|
|
3
|
+
'useInfiniteQuery',
|
|
4
|
+
'useSuspenseInfiniteQuery',
|
|
5
|
+
] as const
|
|
6
|
+
|
|
7
|
+
export type InfiniteQueryFunctions = (typeof infiniteQueryFunctions)[number]
|
|
8
|
+
|
|
9
|
+
export const checkedProperties = [
|
|
10
|
+
'queryFn',
|
|
11
|
+
'getPreviousPageParam',
|
|
12
|
+
'getNextPageParam',
|
|
13
|
+
] as const
|
|
14
|
+
|
|
15
|
+
export const sortRules = [
|
|
16
|
+
[['queryFn'], ['getPreviousPageParam', 'getNextPageParam']],
|
|
17
|
+
] as const
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
|
|
2
|
+
|
|
3
|
+
import { getDocsUrl } from '../../utils/get-docs-url'
|
|
4
|
+
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
|
|
5
|
+
import { sortDataByOrder } from './infinite-query-property-order.utils'
|
|
6
|
+
import { infiniteQueryFunctions, sortRules } from './constants'
|
|
7
|
+
import type { InfiniteQueryFunctions } from './constants'
|
|
8
|
+
import type { ExtraRuleDocs } from '../../types'
|
|
9
|
+
|
|
10
|
+
const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
|
|
11
|
+
|
|
12
|
+
const infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions)
|
|
13
|
+
function isInfiniteQueryFunction(node: any): node is InfiniteQueryFunctions {
|
|
14
|
+
return infiniteQueryFunctionsSet.has(node)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const name = 'infinite-query-property-order'
|
|
18
|
+
|
|
19
|
+
export const rule = createRule({
|
|
20
|
+
name,
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'problem',
|
|
23
|
+
docs: {
|
|
24
|
+
description:
|
|
25
|
+
'Ensure correct order of inference sensitive properties for infinite queries',
|
|
26
|
+
recommended: 'error',
|
|
27
|
+
},
|
|
28
|
+
messages: {
|
|
29
|
+
invalidOrder: 'Invalid order of properties for `{{function}}`.',
|
|
30
|
+
},
|
|
31
|
+
schema: [],
|
|
32
|
+
hasSuggestions: true,
|
|
33
|
+
fixable: 'code',
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
|
|
37
|
+
create: detectTanstackQueryImports((context) => {
|
|
38
|
+
return {
|
|
39
|
+
CallExpression(node) {
|
|
40
|
+
if (node.callee.type !== AST_NODE_TYPES.Identifier) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
const infiniteQueryFunction = node.callee.name
|
|
44
|
+
if (!isInfiniteQueryFunction(infiniteQueryFunction)) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
const argument = node.arguments[0]
|
|
48
|
+
if (argument === undefined || argument.type !== 'ObjectExpression') {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const allProperties = argument.properties
|
|
53
|
+
|
|
54
|
+
// no need to sort if there is at max 1 property
|
|
55
|
+
if (allProperties.length < 2) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const properties = allProperties.flatMap((p, index) => {
|
|
60
|
+
if (
|
|
61
|
+
p.type === AST_NODE_TYPES.Property &&
|
|
62
|
+
p.key.type === AST_NODE_TYPES.Identifier
|
|
63
|
+
) {
|
|
64
|
+
return { name: p.key.name, property: p }
|
|
65
|
+
} else return { name: `_property_${index}`, property: p }
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const sortedProperties = sortDataByOrder(properties, sortRules, 'name')
|
|
69
|
+
if (sortedProperties === null) {
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
context.report({
|
|
73
|
+
node: argument,
|
|
74
|
+
data: { function: node.callee.name },
|
|
75
|
+
messageId: 'invalidOrder',
|
|
76
|
+
fix(fixer) {
|
|
77
|
+
const sourceCode = context.sourceCode
|
|
78
|
+
|
|
79
|
+
const reorderedText = sortedProperties.reduce(
|
|
80
|
+
(sourceText, specifier, index) => {
|
|
81
|
+
let textBetweenProperties = ''
|
|
82
|
+
if (index < allProperties.length - 1) {
|
|
83
|
+
textBetweenProperties = sourceCode
|
|
84
|
+
.getText()
|
|
85
|
+
.slice(
|
|
86
|
+
allProperties[index]!.range[1],
|
|
87
|
+
allProperties[index + 1]!.range[0],
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
return (
|
|
91
|
+
sourceText +
|
|
92
|
+
sourceCode.getText(specifier.property) +
|
|
93
|
+
textBetweenProperties
|
|
94
|
+
)
|
|
95
|
+
},
|
|
96
|
+
'',
|
|
97
|
+
)
|
|
98
|
+
return fixer.replaceTextRange(
|
|
99
|
+
[allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],
|
|
100
|
+
reorderedText,
|
|
101
|
+
)
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
}),
|
|
107
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function sortDataByOrder<T, TKey extends keyof T>(
|
|
2
|
+
data: Array<T> | ReadonlyArray<T>,
|
|
3
|
+
orderRules: ReadonlyArray<
|
|
4
|
+
Readonly<[ReadonlyArray<T[TKey]>, ReadonlyArray<T[TKey]>]>
|
|
5
|
+
>,
|
|
6
|
+
key: TKey,
|
|
7
|
+
): Array<T> | null {
|
|
8
|
+
const getSubsetIndex = (
|
|
9
|
+
item: T[TKey],
|
|
10
|
+
subsets: ReadonlyArray<ReadonlyArray<T[TKey]> | Array<T[TKey]>>,
|
|
11
|
+
): number | null => {
|
|
12
|
+
for (let i = 0; i < subsets.length; i++) {
|
|
13
|
+
if (subsets[i]?.includes(item)) {
|
|
14
|
+
return i
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const orderSets = orderRules.reduce(
|
|
21
|
+
(sets, [A, B]) => [...sets, A, B],
|
|
22
|
+
[] as Array<ReadonlyArray<T[TKey]> | Array<T[TKey]>>,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
const inOrderArray = data.filter(
|
|
26
|
+
(item) => getSubsetIndex(item[key], orderSets) !== null,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
let wasResorted = false as boolean
|
|
30
|
+
|
|
31
|
+
// Sort by the relative order defined by the rules
|
|
32
|
+
const sortedArray = inOrderArray.sort((a, b) => {
|
|
33
|
+
const aKey = a[key],
|
|
34
|
+
bKey = b[key]
|
|
35
|
+
const aSubsetIndex = getSubsetIndex(aKey, orderSets)
|
|
36
|
+
const bSubsetIndex = getSubsetIndex(bKey, orderSets)
|
|
37
|
+
|
|
38
|
+
// If both items belong to different subsets, sort by their subset order
|
|
39
|
+
if (
|
|
40
|
+
aSubsetIndex !== null &&
|
|
41
|
+
bSubsetIndex !== null &&
|
|
42
|
+
aSubsetIndex !== bSubsetIndex
|
|
43
|
+
) {
|
|
44
|
+
return aSubsetIndex - bSubsetIndex
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// If both items belong to the same subset or neither is in the subset, keep their relative order
|
|
48
|
+
return 0
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const inOrderIterator = sortedArray.values()
|
|
52
|
+
const result = data.map((item) => {
|
|
53
|
+
if (getSubsetIndex(item[key], orderSets) !== null) {
|
|
54
|
+
const sortedItem = inOrderIterator.next().value!
|
|
55
|
+
if (sortedItem[key] !== item[key]) {
|
|
56
|
+
wasResorted = true
|
|
57
|
+
}
|
|
58
|
+
return sortedItem
|
|
59
|
+
}
|
|
60
|
+
return item
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (!wasResorted) {
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
return result
|
|
67
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
|
|
2
|
+
import { getDocsUrl } from '../../utils/get-docs-url'
|
|
3
|
+
import { ASTUtils } from '../../utils/ast-utils'
|
|
4
|
+
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
|
|
5
|
+
import { NoRestDestructuringUtils } from './no-rest-destructuring.utils'
|
|
6
|
+
import type { ExtraRuleDocs } from '../../types'
|
|
7
|
+
|
|
8
|
+
export const name = 'no-rest-destructuring'
|
|
9
|
+
|
|
10
|
+
const queryHooks = [
|
|
11
|
+
'useQuery',
|
|
12
|
+
'useQueries',
|
|
13
|
+
'useInfiniteQuery',
|
|
14
|
+
'useSuspenseQuery',
|
|
15
|
+
'useSuspenseQueries',
|
|
16
|
+
'useSuspenseInfiniteQuery',
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
|
|
20
|
+
|
|
21
|
+
export const rule = createRule({
|
|
22
|
+
name,
|
|
23
|
+
meta: {
|
|
24
|
+
type: 'problem',
|
|
25
|
+
docs: {
|
|
26
|
+
description: 'Disallows rest destructuring in queries',
|
|
27
|
+
recommended: 'warn',
|
|
28
|
+
},
|
|
29
|
+
messages: {
|
|
30
|
+
objectRestDestructure: `Object rest destructuring on a query will observe all changes to the query, leading to excessive re-renders.`,
|
|
31
|
+
},
|
|
32
|
+
schema: [],
|
|
33
|
+
},
|
|
34
|
+
defaultOptions: [],
|
|
35
|
+
|
|
36
|
+
create: detectTanstackQueryImports((context, _, helpers) => {
|
|
37
|
+
const queryResultVariables = new Set<string>()
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
CallExpression: (node) => {
|
|
41
|
+
if (
|
|
42
|
+
!ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) ||
|
|
43
|
+
node.parent.type !== AST_NODE_TYPES.VariableDeclarator ||
|
|
44
|
+
!helpers.isTanstackQueryImport(node.callee)
|
|
45
|
+
) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const returnValue = node.parent.id
|
|
50
|
+
|
|
51
|
+
if (
|
|
52
|
+
node.callee.name !== 'useQueries' &&
|
|
53
|
+
node.callee.name !== 'useSuspenseQueries'
|
|
54
|
+
) {
|
|
55
|
+
if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
|
|
56
|
+
return context.report({
|
|
57
|
+
node: node.parent,
|
|
58
|
+
messageId: 'objectRestDestructure',
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (returnValue.type === AST_NODE_TYPES.Identifier) {
|
|
63
|
+
queryResultVariables.add(returnValue.name)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (returnValue.type !== AST_NODE_TYPES.ArrayPattern) {
|
|
70
|
+
if (returnValue.type === AST_NODE_TYPES.Identifier) {
|
|
71
|
+
queryResultVariables.add(returnValue.name)
|
|
72
|
+
}
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
returnValue.elements.forEach((queryResult) => {
|
|
77
|
+
if (queryResult === null) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
if (NoRestDestructuringUtils.isObjectRestDestructuring(queryResult)) {
|
|
81
|
+
context.report({
|
|
82
|
+
node: queryResult,
|
|
83
|
+
messageId: 'objectRestDestructure',
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
VariableDeclarator: (node) => {
|
|
90
|
+
if (
|
|
91
|
+
node.init?.type === AST_NODE_TYPES.Identifier &&
|
|
92
|
+
queryResultVariables.has(node.init.name) &&
|
|
93
|
+
NoRestDestructuringUtils.isObjectRestDestructuring(node.id)
|
|
94
|
+
) {
|
|
95
|
+
context.report({
|
|
96
|
+
node,
|
|
97
|
+
messageId: 'objectRestDestructure',
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
SpreadElement: (node) => {
|
|
103
|
+
if (
|
|
104
|
+
node.argument.type === AST_NODE_TYPES.Identifier &&
|
|
105
|
+
queryResultVariables.has(node.argument.name)
|
|
106
|
+
) {
|
|
107
|
+
context.report({
|
|
108
|
+
node,
|
|
109
|
+
messageId: 'objectRestDestructure',
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
2
|
+
import type { TSESTree } from '@typescript-eslint/utils'
|
|
3
|
+
|
|
4
|
+
export const NoRestDestructuringUtils = {
|
|
5
|
+
isObjectRestDestructuring(node: TSESTree.Node): boolean {
|
|
6
|
+
if (node.type !== AST_NODE_TYPES.ObjectPattern) {
|
|
7
|
+
return false
|
|
8
|
+
}
|
|
9
|
+
return node.properties.some((p) => p.type === AST_NODE_TYPES.RestElement)
|
|
10
|
+
},
|
|
11
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
|
|
2
|
+
import { getDocsUrl } from '../../utils/get-docs-url'
|
|
3
|
+
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
|
|
4
|
+
import type { TSESTree } from '@typescript-eslint/utils'
|
|
5
|
+
import type { ExtraRuleDocs } from '../../types'
|
|
6
|
+
|
|
7
|
+
export const name = 'no-unstable-deps'
|
|
8
|
+
|
|
9
|
+
export const reactHookNames = ['useEffect', 'useCallback', 'useMemo']
|
|
10
|
+
export const useQueryHookNames = [
|
|
11
|
+
'useQuery',
|
|
12
|
+
'useSuspenseQuery',
|
|
13
|
+
'useQueries',
|
|
14
|
+
'useSuspenseQueries',
|
|
15
|
+
'useInfiniteQuery',
|
|
16
|
+
'useSuspenseInfiniteQuery',
|
|
17
|
+
]
|
|
18
|
+
const allHookNames = ['useMutation', ...useQueryHookNames]
|
|
19
|
+
const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
|
|
20
|
+
|
|
21
|
+
export const rule = createRule({
|
|
22
|
+
name,
|
|
23
|
+
meta: {
|
|
24
|
+
type: 'problem',
|
|
25
|
+
docs: {
|
|
26
|
+
description:
|
|
27
|
+
'Disallow putting the result of query hooks directly in a React hook dependency array',
|
|
28
|
+
recommended: 'error',
|
|
29
|
+
},
|
|
30
|
+
messages: {
|
|
31
|
+
noUnstableDeps: `The result of {{queryHook}} is not referentially stable, so don't pass it directly into the dependencies array of {{reactHook}}. Instead, destructure the return value of {{queryHook}} and pass the destructured values into the dependency array of {{reactHook}}.`,
|
|
32
|
+
},
|
|
33
|
+
schema: [],
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
|
|
37
|
+
create: detectTanstackQueryImports((context) => {
|
|
38
|
+
const trackedVariables: Record<string, string> = {}
|
|
39
|
+
const hookAliasMap: Record<string, string> = {}
|
|
40
|
+
|
|
41
|
+
function getReactHook(node: TSESTree.CallExpression): string | undefined {
|
|
42
|
+
if (node.callee.type === 'Identifier') {
|
|
43
|
+
const calleeName = node.callee.name
|
|
44
|
+
// Check if the identifier is a known React hook or an alias
|
|
45
|
+
if (reactHookNames.includes(calleeName) || calleeName in hookAliasMap) {
|
|
46
|
+
return calleeName
|
|
47
|
+
}
|
|
48
|
+
} else if (
|
|
49
|
+
node.callee.type === 'MemberExpression' &&
|
|
50
|
+
node.callee.object.type === 'Identifier' &&
|
|
51
|
+
node.callee.object.name === 'React' &&
|
|
52
|
+
node.callee.property.type === 'Identifier' &&
|
|
53
|
+
reactHookNames.includes(node.callee.property.name)
|
|
54
|
+
) {
|
|
55
|
+
// Member expression case: `React.useCallback`
|
|
56
|
+
return node.callee.property.name
|
|
57
|
+
}
|
|
58
|
+
return undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function collectVariableNames(
|
|
62
|
+
pattern: TSESTree.BindingName,
|
|
63
|
+
queryHook: string,
|
|
64
|
+
) {
|
|
65
|
+
if (pattern.type === AST_NODE_TYPES.Identifier) {
|
|
66
|
+
trackedVariables[pattern.name] = queryHook
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
ImportDeclaration(node: TSESTree.ImportDeclaration) {
|
|
72
|
+
if (
|
|
73
|
+
node.specifiers.length > 0 &&
|
|
74
|
+
node.importKind === 'value' &&
|
|
75
|
+
node.source.value === 'React'
|
|
76
|
+
) {
|
|
77
|
+
node.specifiers.forEach((specifier) => {
|
|
78
|
+
if (
|
|
79
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
80
|
+
specifier.imported.type === AST_NODE_TYPES.Identifier &&
|
|
81
|
+
reactHookNames.includes(specifier.imported.name)
|
|
82
|
+
) {
|
|
83
|
+
// Track alias or direct import
|
|
84
|
+
hookAliasMap[specifier.local.name] = specifier.imported.name
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
VariableDeclarator(node) {
|
|
91
|
+
if (
|
|
92
|
+
node.init !== null &&
|
|
93
|
+
node.init.type === AST_NODE_TYPES.CallExpression &&
|
|
94
|
+
node.init.callee.type === AST_NODE_TYPES.Identifier &&
|
|
95
|
+
allHookNames.includes(node.init.callee.name)
|
|
96
|
+
) {
|
|
97
|
+
collectVariableNames(node.id, node.init.callee.name)
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
CallExpression: (node) => {
|
|
101
|
+
const reactHook = getReactHook(node)
|
|
102
|
+
if (
|
|
103
|
+
reactHook !== undefined &&
|
|
104
|
+
node.arguments.length > 1 &&
|
|
105
|
+
node.arguments[1]?.type === AST_NODE_TYPES.ArrayExpression
|
|
106
|
+
) {
|
|
107
|
+
const depsArray = node.arguments[1].elements
|
|
108
|
+
depsArray.forEach((dep) => {
|
|
109
|
+
if (
|
|
110
|
+
dep !== null &&
|
|
111
|
+
dep.type === AST_NODE_TYPES.Identifier &&
|
|
112
|
+
trackedVariables[dep.name] !== undefined
|
|
113
|
+
) {
|
|
114
|
+
const queryHook = trackedVariables[dep.name]
|
|
115
|
+
context.report({
|
|
116
|
+
node: dep,
|
|
117
|
+
messageId: 'noUnstableDeps',
|
|
118
|
+
data: {
|
|
119
|
+
queryHook,
|
|
120
|
+
reactHook,
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
}),
|
|
129
|
+
})
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils'
|
|
2
|
+
import ts from 'typescript'
|
|
3
|
+
import { ASTUtils } from '../../utils/ast-utils'
|
|
4
|
+
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
|
|
5
|
+
import { getDocsUrl } from '../../utils/get-docs-url'
|
|
6
|
+
import type { ExtraRuleDocs } from '../../types'
|
|
7
|
+
|
|
8
|
+
export const name = 'no-void-query-fn'
|
|
9
|
+
|
|
10
|
+
const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
|
|
11
|
+
|
|
12
|
+
export const rule = createRule({
|
|
13
|
+
name,
|
|
14
|
+
meta: {
|
|
15
|
+
type: 'problem',
|
|
16
|
+
docs: {
|
|
17
|
+
description: 'Ensures queryFn returns a non-undefined value',
|
|
18
|
+
recommended: 'error',
|
|
19
|
+
},
|
|
20
|
+
messages: {
|
|
21
|
+
noVoidReturn: 'queryFn must return a non-undefined value',
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
},
|
|
25
|
+
defaultOptions: [],
|
|
26
|
+
|
|
27
|
+
create: detectTanstackQueryImports((context) => {
|
|
28
|
+
return {
|
|
29
|
+
Property(node) {
|
|
30
|
+
if (
|
|
31
|
+
!ASTUtils.isObjectExpression(node.parent) ||
|
|
32
|
+
!ASTUtils.isIdentifierWithName(node.key, 'queryFn')
|
|
33
|
+
) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const parserServices = context.sourceCode.parserServices
|
|
38
|
+
|
|
39
|
+
if (
|
|
40
|
+
!parserServices ||
|
|
41
|
+
!parserServices.esTreeNodeToTSNodeMap ||
|
|
42
|
+
!parserServices.program
|
|
43
|
+
) {
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const checker = parserServices.program.getTypeChecker()
|
|
48
|
+
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.value)
|
|
49
|
+
const type = checker.getTypeAtLocation(tsNode)
|
|
50
|
+
|
|
51
|
+
// Get the return type of the function
|
|
52
|
+
if (type.getCallSignatures().length > 0) {
|
|
53
|
+
const returnType = type.getCallSignatures()[0]?.getReturnType()
|
|
54
|
+
|
|
55
|
+
if (!returnType) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Check if return type is void or undefined
|
|
60
|
+
if (isIllegalReturn(checker, returnType)) {
|
|
61
|
+
context.report({
|
|
62
|
+
node: node.value,
|
|
63
|
+
messageId: 'noVoidReturn',
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}),
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
function isIllegalReturn(checker: ts.TypeChecker, type: ts.Type): boolean {
|
|
73
|
+
const awaited = checker.getAwaitedType(type)
|
|
74
|
+
|
|
75
|
+
if (!awaited) return false
|
|
76
|
+
|
|
77
|
+
if (awaited.isUnion()) {
|
|
78
|
+
return awaited.types.some((t) => isIllegalReturn(checker, t))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return awaited.flags & (ts.TypeFlags.Void | ts.TypeFlags.Undefined)
|
|
82
|
+
? true
|
|
83
|
+
: false
|
|
84
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
|
|
2
|
+
import { ASTUtils } from '../../utils/ast-utils'
|
|
3
|
+
import { getDocsUrl } from '../../utils/get-docs-url'
|
|
4
|
+
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
|
|
5
|
+
import type { TSESLint } from '@typescript-eslint/utils'
|
|
6
|
+
import type { ExtraRuleDocs } from '../../types'
|
|
7
|
+
|
|
8
|
+
export const name = 'stable-query-client'
|
|
9
|
+
|
|
10
|
+
const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
|
|
11
|
+
|
|
12
|
+
export const rule = createRule({
|
|
13
|
+
name,
|
|
14
|
+
meta: {
|
|
15
|
+
type: 'problem',
|
|
16
|
+
docs: {
|
|
17
|
+
description: 'Makes sure that QueryClient is stable',
|
|
18
|
+
recommended: 'error',
|
|
19
|
+
},
|
|
20
|
+
messages: {
|
|
21
|
+
unstable: [
|
|
22
|
+
'QueryClient is not stable. It should be either extracted from the component or wrapped in React.useState.',
|
|
23
|
+
'See https://tkdodo.eu/blog/react-query-fa-qs#2-the-queryclient-is-not-stable',
|
|
24
|
+
].join('\n'),
|
|
25
|
+
fixTo: 'Fix to {{result}}',
|
|
26
|
+
},
|
|
27
|
+
hasSuggestions: true,
|
|
28
|
+
fixable: 'code',
|
|
29
|
+
schema: [],
|
|
30
|
+
},
|
|
31
|
+
defaultOptions: [],
|
|
32
|
+
|
|
33
|
+
create: detectTanstackQueryImports((context, _, helpers) => {
|
|
34
|
+
return {
|
|
35
|
+
NewExpression: (node) => {
|
|
36
|
+
if (
|
|
37
|
+
node.callee.type !== AST_NODE_TYPES.Identifier ||
|
|
38
|
+
node.callee.name !== 'QueryClient' ||
|
|
39
|
+
node.parent.type !== AST_NODE_TYPES.VariableDeclarator ||
|
|
40
|
+
!helpers.isSpecificTanstackQueryImport(
|
|
41
|
+
node.callee,
|
|
42
|
+
'@tanstack/react-query',
|
|
43
|
+
)
|
|
44
|
+
) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const fnAncestor = ASTUtils.getFunctionAncestor(
|
|
49
|
+
context.sourceCode,
|
|
50
|
+
node,
|
|
51
|
+
)
|
|
52
|
+
const isReactServerComponent = fnAncestor?.async === true
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
!ASTUtils.isValidReactComponentOrHookName(fnAncestor?.id) ||
|
|
56
|
+
isReactServerComponent
|
|
57
|
+
) {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
context.report({
|
|
62
|
+
node: node.parent,
|
|
63
|
+
messageId: 'unstable',
|
|
64
|
+
fix: (() => {
|
|
65
|
+
const { parent } = node
|
|
66
|
+
|
|
67
|
+
if (parent.id.type !== AST_NODE_TYPES.Identifier) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// we need the fallbacks for backwards compat with eslint < 8.37.0
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
73
|
+
const sourceCode = context.sourceCode ?? context.getSourceCode()
|
|
74
|
+
const nodeText = sourceCode.getText(node)
|
|
75
|
+
const variableName = parent.id.name
|
|
76
|
+
|
|
77
|
+
return (fixer: TSESLint.RuleFixer) => {
|
|
78
|
+
return fixer.replaceTextRange(
|
|
79
|
+
[parent.range[0], parent.range[1]],
|
|
80
|
+
`[${variableName}] = React.useState(() => ${nodeText})`,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
})(),
|
|
84
|
+
})
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}),
|
|
88
|
+
})
|
package/src/rules.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as exhaustiveDeps from './rules/exhaustive-deps/exhaustive-deps.rule'
|
|
2
|
+
import * as stableQueryClient from './rules/stable-query-client/stable-query-client.rule'
|
|
3
|
+
import * as noRestDestructuring from './rules/no-rest-destructuring/no-rest-destructuring.rule'
|
|
4
|
+
import * as noUnstableDeps from './rules/no-unstable-deps/no-unstable-deps.rule'
|
|
5
|
+
import * as infiniteQueryPropertyOrder from './rules/infinite-query-property-order/infinite-query-property-order.rule'
|
|
6
|
+
import * as noVoidQueryFn from './rules/no-void-query-fn/no-void-query-fn.rule'
|
|
7
|
+
import type { ESLintUtils } from '@typescript-eslint/utils'
|
|
8
|
+
import type { ExtraRuleDocs } from './types'
|
|
9
|
+
|
|
10
|
+
export const rules: Record<
|
|
11
|
+
string,
|
|
12
|
+
ESLintUtils.RuleModule<
|
|
13
|
+
string,
|
|
14
|
+
ReadonlyArray<unknown>,
|
|
15
|
+
ExtraRuleDocs,
|
|
16
|
+
ESLintUtils.RuleListener
|
|
17
|
+
>
|
|
18
|
+
> = {
|
|
19
|
+
[exhaustiveDeps.name]: exhaustiveDeps.rule,
|
|
20
|
+
[stableQueryClient.name]: stableQueryClient.rule,
|
|
21
|
+
[noRestDestructuring.name]: noRestDestructuring.rule,
|
|
22
|
+
[noUnstableDeps.name]: noUnstableDeps.rule,
|
|
23
|
+
[infiniteQueryPropertyOrder.name]: infiniteQueryPropertyOrder.rule,
|
|
24
|
+
[noVoidQueryFn.name]: noVoidQueryFn.rule,
|
|
25
|
+
}
|
package/src/types.ts
ADDED