|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import { defineRule, type ESTree } from "@oxlint/plugins"; |
| 3 | +import * as Option from "effect/Option"; |
| 4 | +import * as NodeFS from "node:fs"; |
| 5 | +import * as NodePath from "node:path"; |
| 6 | + |
| 7 | +import { getPropertyName, unwrapExpression } from "../utils.ts"; |
| 8 | + |
| 9 | +export type EffectBudget = Readonly<Record<string, number>>; |
| 10 | + |
| 11 | +export const EFFECT_BUDGET_FILE = "scripts/effect-budget.json"; |
| 12 | +export const REPO_ROOT = NodePath.resolve(import.meta.dirname, "..", ".."); |
| 13 | + |
| 14 | +const EFFECT_HOOKS = new Set(["useEffect", "useLayoutEffect", "useInsertionEffect"]); |
| 15 | +const EFFECT_HOOK_TEXT = /use(?:Layout|Insertion)?Effect/; |
| 16 | +const UNBUDGETED_FILE = /\.test\.[cm]?[jt]sx?$|(?:^|\/)(?:test|__tests__)\//; |
| 17 | + |
| 18 | +/** Matches `useEffect(...)` and member calls such as `React.useEffect(...)`. */ |
| 19 | +export function isEffectHookCall(node: { readonly callee: unknown }): boolean { |
| 20 | + const callee = unwrapExpression(node.callee); |
| 21 | + if (Option.isNone(callee)) return false; |
| 22 | + const expression = callee.value; |
| 23 | + if (expression.type === "Identifier") return EFFECT_HOOKS.has(expression.name); |
| 24 | + if (expression.type === "MemberExpression" && !expression.computed) { |
| 25 | + return Option.exists(getPropertyName(expression.property), (name) => EFFECT_HOOKS.has(name)); |
| 26 | + } |
| 27 | + return false; |
| 28 | +} |
| 29 | + |
| 30 | +export function isBudgetedFile(repoPath: string): boolean { |
| 31 | + return !repoPath.startsWith("..") && !UNBUDGETED_FILE.test(repoPath); |
| 32 | +} |
| 33 | + |
| 34 | +export function repoPathOf(root: string, fileName: string): string { |
| 35 | + return NodePath.relative(root, fileName).split(NodePath.sep).join("/"); |
| 36 | +} |
| 37 | + |
| 38 | +export function readEffectBudget(root: string): EffectBudget { |
| 39 | + return JSON.parse(NodeFS.readFileSync(NodePath.join(root, EFFECT_BUDGET_FILE), "utf8")); |
| 40 | +} |
| 41 | + |
| 42 | +const budgets = new Map<string, EffectBudget>(); |
| 43 | +const cachedBudget = (root: string): EffectBudget => { |
| 44 | + let budget = budgets.get(root); |
| 45 | + if (budget === undefined) { |
| 46 | + budget = readEffectBudget(root); |
| 47 | + budgets.set(root, budget); |
| 48 | + } |
| 49 | + return budget; |
| 50 | +}; |
| 51 | + |
| 52 | +const readRoot = (options: ReadonlyArray<unknown>): string => { |
| 53 | + const [first] = options; |
| 54 | + return typeof first === "object" && |
| 55 | + first !== null && |
| 56 | + "root" in first && |
| 57 | + typeof first.root === "string" |
| 58 | + ? first.root |
| 59 | + : REPO_ROOT; |
| 60 | +}; |
| 61 | + |
| 62 | +export default defineRule({ |
| 63 | + meta: { |
| 64 | + type: "suggestion", |
| 65 | + docs: { |
| 66 | + description: `Hold each file to the React effect hooks it is allowed in ${EFFECT_BUDGET_FILE}; a missing entry allows none.`, |
| 67 | + }, |
| 68 | + schema: [ |
| 69 | + { |
| 70 | + type: "object", |
| 71 | + properties: { |
| 72 | + root: { |
| 73 | + type: "string", |
| 74 | + description: `Directory that budget keys are relative to and that holds ${EFFECT_BUDGET_FILE}. Defaults to the repository root.`, |
| 75 | + }, |
| 76 | + }, |
| 77 | + additionalProperties: false, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + create(context) { |
| 82 | + const root = readRoot(context.options); |
| 83 | + const repoPath = repoPathOf(root, context.filename); |
| 84 | + if (!isBudgetedFile(repoPath) || !EFFECT_HOOK_TEXT.test(context.sourceCode.text)) return {}; |
| 85 | + |
| 86 | + const calls: Array<ESTree.CallExpression> = []; |
| 87 | + return { |
| 88 | + CallExpression(node) { |
| 89 | + if (isEffectHookCall(node)) calls.push(node); |
| 90 | + }, |
| 91 | + "Program:exit"() { |
| 92 | + const budget = cachedBudget(root)[repoPath] ?? 0; |
| 93 | + if (calls.length <= budget) return; |
| 94 | + const message = |
| 95 | + `This file has ${calls.length} React ${calls.length === 1 ? "effect" : "effects"} against a budget of ${budget}. ` + |
| 96 | + "Prefer deriving in render, event handlers, a `key` reset, an element-owned ref callback, or a store. " + |
| 97 | + `If an effect is genuinely right, raise this file's entry in ${EFFECT_BUDGET_FILE} in the same PR.`; |
| 98 | + for (const node of calls.slice(budget)) context.report({ node, message }); |
| 99 | + }, |
| 100 | + }; |
| 101 | + }, |
| 102 | +}); |
0 commit comments