|
| 1 | +import { is, NodeType, traverseUp } from "@eslint-react/ast"; |
| 2 | +import { isReactHookCallWithNameLoose, isUseEffectCall, isUseStateCall } from "@eslint-react/core"; |
| 3 | +import { getESLintReactSettings } from "@eslint-react/shared"; |
| 4 | +import { F, O } from "@eslint-react/tools"; |
| 5 | +import type { RuleContext } from "@eslint-react/types"; |
| 6 | +import { findVariable, getVariableInit } from "@eslint-react/var"; |
| 7 | +import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils"; |
| 8 | +import { getStaticValue } from "@typescript-eslint/utils/ast-utils"; |
| 9 | +import type { ConstantCase } from "string-ts"; |
| 10 | +import { match } from "ts-pattern"; |
| 11 | + |
| 12 | +import { createRule } from "../utils"; |
| 13 | + |
| 14 | +export const RULE_NAME = "no-direct-set-state-in-use-effect"; |
| 15 | + |
| 16 | +export type MessageID = ConstantCase<typeof RULE_NAME>; |
| 17 | + |
| 18 | +export default createRule<[], MessageID>({ |
| 19 | + meta: { |
| 20 | + type: "problem", |
| 21 | + docs: { |
| 22 | + description: "disallow direct calls to the 'set' function of 'useState' in 'useEffect'.", |
| 23 | + }, |
| 24 | + messages: { |
| 25 | + NO_DIRECT_SET_STATE_IN_USE_EFFECT: "Do not call the set function of 'useState' directly in 'useEffect'.", |
| 26 | + }, |
| 27 | + schema: [], |
| 28 | + }, |
| 29 | + name: RULE_NAME, |
| 30 | + create(context) { |
| 31 | + const settings = getESLintReactSettings(context.settings); |
| 32 | + const { useEffect: useEffectAlias = [], useState: useStateAlias = [] } = settings.additionalHooks ?? {}; |
| 33 | + function isUseEffectCallWithAlias(node: TSESTree.CallExpression, context: RuleContext) { |
| 34 | + return (isUseEffectCall(node, context) || useEffectAlias.some(F.flip(isReactHookCallWithNameLoose)(node))); |
| 35 | + } |
| 36 | + function isUseStateCallWithAlias(node: TSESTree.CallExpression, context: RuleContext) { |
| 37 | + return (isUseStateCall(node, context) || useStateAlias.some(F.flip(isReactHookCallWithNameLoose)(node))); |
| 38 | + } |
| 39 | + return { |
| 40 | + CallExpression(node) { |
| 41 | + const effectFunction = traverseUp( |
| 42 | + node, |
| 43 | + (n) => |
| 44 | + n.parent?.type === NodeType.CallExpression |
| 45 | + && isUseEffectCallWithAlias(n.parent, context), |
| 46 | + ); |
| 47 | + // TODO: support detecting effect cleanup functions as well or add a separate rule for that called `no-direct-set-state-in-use-effect-cleanup` |
| 48 | + if (O.isNone(effectFunction)) return; |
| 49 | + const scope = context.sourceCode.getScope(node); |
| 50 | + if (scope.block !== effectFunction.value) return; |
| 51 | + const name = match(node.callee) |
| 52 | + // const [data, setData] = useState(); |
| 53 | + // setData(); |
| 54 | + .with({ type: NodeType.Identifier }, (n) => O.some(n.name)) |
| 55 | + // const data = useState(); |
| 56 | + // data[1](); |
| 57 | + .with({ type: NodeType.MemberExpression }, (n) => { |
| 58 | + if (!("name" in n.object)) return O.none(); |
| 59 | + const initialScope = context.sourceCode.getScope(n); |
| 60 | + const property = getStaticValue(n.property, initialScope); |
| 61 | + if (property?.value === 1) return O.fromNullable(n.object.name); |
| 62 | + return O.none(); |
| 63 | + }) |
| 64 | + // const data = useState(); |
| 65 | + // data.at(1)(); |
| 66 | + .with({ type: NodeType.CallExpression }, (n) => { |
| 67 | + if (!is(NodeType.MemberExpression)(n.callee)) return O.none(); |
| 68 | + if (!("name" in n.callee.object)) return O.none(); |
| 69 | + const isAt = match(n.callee) |
| 70 | + .with( |
| 71 | + { |
| 72 | + type: NodeType.MemberExpression, |
| 73 | + property: { |
| 74 | + type: NodeType.Identifier, |
| 75 | + name: "at", |
| 76 | + }, |
| 77 | + }, |
| 78 | + F.constTrue, |
| 79 | + ) |
| 80 | + .otherwise(F.constFalse); |
| 81 | + if (!isAt) return O.none(); |
| 82 | + const [index] = n.arguments; |
| 83 | + if (!index) return O.none(); |
| 84 | + const initialScope = context.sourceCode.getScope(n); |
| 85 | + const value = getStaticValue(index, initialScope); |
| 86 | + if (value?.value === 1) return O.fromNullable(n.callee.object.name); |
| 87 | + return O.none(); |
| 88 | + }) |
| 89 | + .otherwise(O.none); |
| 90 | + F.pipe( |
| 91 | + name, |
| 92 | + O.flatMap(findVariable(scope)), |
| 93 | + O.flatMap(getVariableInit(0)), |
| 94 | + O.filter(is(NodeType.CallExpression)), |
| 95 | + O.filter(name => isUseStateCallWithAlias(name, context)), |
| 96 | + O.map(name => ({ |
| 97 | + data: { |
| 98 | + setState: name, |
| 99 | + }, |
| 100 | + messageId: "NO_DIRECT_SET_STATE_IN_USE_EFFECT", |
| 101 | + node, |
| 102 | + } as const)), |
| 103 | + O.map(context.report), |
| 104 | + ); |
| 105 | + }, |
| 106 | + }; |
| 107 | + }, |
| 108 | + defaultOptions: [], |
| 109 | +}) satisfies ESLintUtils.RuleModule<MessageID>; |
0 commit comments