|
| 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-mutation-in-deps' |
| 8 | + |
| 9 | +export const reactHookNames = ['useEffect', 'useCallback', 'useMemo'] |
| 10 | + |
| 11 | +const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl) |
| 12 | + |
| 13 | +export const rule = createRule({ |
| 14 | + name, |
| 15 | + meta: { |
| 16 | + type: 'problem', |
| 17 | + docs: { |
| 18 | + description: |
| 19 | + 'Disallow putting the result of useMutation directly in a React hook dependency array', |
| 20 | + recommended: 'error', |
| 21 | + }, |
| 22 | + messages: { |
| 23 | + mutationInDeps: `The result of useMutation is not referentially stable, so don't pass it directly into the dependencies array of a hook like useEffect, useMemo, or useCallback. Instead, destructure the return value of useMutation and pass the destructured values into the dependency array.`, |
| 24 | + }, |
| 25 | + schema: [], |
| 26 | + }, |
| 27 | + defaultOptions: [], |
| 28 | + |
| 29 | + create: detectTanstackQueryImports((context) => { |
| 30 | + const trackedVariables = new Set<string>() |
| 31 | + const hookAliasMap: Record<string, string> = {} |
| 32 | + |
| 33 | + function isReactHook(node: TSESTree.CallExpression): boolean { |
| 34 | + if (node.callee.type === 'Identifier') { |
| 35 | + const calleeName = node.callee.name |
| 36 | + // Check if the identifier is a known React hook or an alias |
| 37 | + return reactHookNames.includes(calleeName) || calleeName in hookAliasMap |
| 38 | + } else if ( |
| 39 | + node.callee.type === 'MemberExpression' && |
| 40 | + node.callee.object.type === 'Identifier' && |
| 41 | + node.callee.object.name === 'React' && |
| 42 | + node.callee.property.type === 'Identifier' && |
| 43 | + reactHookNames.includes(node.callee.property.name) |
| 44 | + ) { |
| 45 | + // Member expression case: `React.useCallback` |
| 46 | + return true |
| 47 | + } |
| 48 | + return false |
| 49 | + } |
| 50 | + |
| 51 | + function collectVariableNames(pattern: TSESTree.BindingName) { |
| 52 | + if (pattern.type === AST_NODE_TYPES.Identifier) { |
| 53 | + trackedVariables.add(pattern.name) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + ImportDeclaration(node: TSESTree.ImportDeclaration) { |
| 59 | + if ( |
| 60 | + node.specifiers.length > 0 && |
| 61 | + node.importKind === 'value' && |
| 62 | + node.source.value === 'React' |
| 63 | + ) { |
| 64 | + node.specifiers.forEach((specifier) => { |
| 65 | + if ( |
| 66 | + specifier.type === AST_NODE_TYPES.ImportSpecifier && |
| 67 | + reactHookNames.includes(specifier.imported.name) |
| 68 | + ) { |
| 69 | + // Track alias or direct import |
| 70 | + hookAliasMap[specifier.local.name] = specifier.imported.name |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + VariableDeclarator(node) { |
| 77 | + if ( |
| 78 | + node.init !== null && |
| 79 | + node.init.type === AST_NODE_TYPES.CallExpression && |
| 80 | + node.init.callee.type === AST_NODE_TYPES.Identifier && |
| 81 | + node.init.callee.name === 'useMutation' |
| 82 | + ) { |
| 83 | + collectVariableNames(node.id) |
| 84 | + } |
| 85 | + }, |
| 86 | + CallExpression: (node) => { |
| 87 | + if ( |
| 88 | + isReactHook(node) && |
| 89 | + node.arguments.length > 1 && |
| 90 | + node.arguments[1]?.type === AST_NODE_TYPES.ArrayExpression |
| 91 | + ) { |
| 92 | + const depsArray = node.arguments[1].elements |
| 93 | + depsArray.forEach((dep) => { |
| 94 | + if ( |
| 95 | + dep !== null && |
| 96 | + dep.type === AST_NODE_TYPES.Identifier && |
| 97 | + trackedVariables.has(dep.name) |
| 98 | + ) { |
| 99 | + context.report({ |
| 100 | + node: dep, |
| 101 | + messageId: 'mutationInDeps', |
| 102 | + }) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + }, |
| 107 | + } |
| 108 | + }), |
| 109 | +}) |
0 commit comments