-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathpurity.ts
More file actions
101 lines (96 loc) · 3.73 KB
/
Copy pathpurity.ts
File metadata and controls
101 lines (96 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { createRule } from "@/utils/create-rule";
import { Check, Extract, type TSESTreeFunction, Traverse } from "@eslint-react/ast";
import * as core from "@eslint-react/core";
import { type RuleContext, type RuleFeature, type RuleListener, merge } from "@eslint-react/eslint";
import { AST_NODE_TYPES as AST, type TSESTree } from "@typescript-eslint/types";
import { IMPURE_CTORS, IMPURE_FUNCS, resolveBuiltinObjectName } from "./lib";
export const RULE_NAME = "purity";
export const RULE_FEATURES = [] as const satisfies RuleFeature[];
export type MessageID = "default";
export default createRule<[], MessageID>({
meta: {
type: "problem",
docs: {
description: "Validates that components and hooks are pure by checking that they do not call known-impure functions during render.",
},
messages: {
default: "Do not call '{{name}}' during render. Components and hooks must be pure. Move this call into an event handler, effect, or state initializer.",
},
schema: [],
},
name: RULE_NAME,
create,
defaultOptions: [],
});
export function create(context: RuleContext<MessageID, []>): RuleListener {
const hc = core.getHookCollector(context);
const fc = core.getFunctionComponentCollector(context);
const cEntries: {
func: TSESTreeFunction;
node: TSESTree.CallExpression;
}[] = [];
const nEntries: {
func: TSESTreeFunction;
node: TSESTree.NewExpression;
}[] = [];
return merge(
hc.visitor,
fc.visitor,
{
CallExpression(node: TSESTree.CallExpression) {
const expr = Extract.unwrap(node.callee);
switch (true) {
case Check.isIdentifier(expr): {
const builtinName = resolveBuiltinObjectName(context, expr);
if (builtinName == null) return;
const globalThisImpure = IMPURE_FUNCS.get("globalThis");
if (globalThisImpure == null || !globalThisImpure.has(builtinName)) return;
const func = Traverse.findParent(node, Check.isFunction);
if (func == null) return;
cEntries.push({ func, node });
break;
}
case expr.type === AST.MemberExpression
&& Check.isIdentifier(expr.property): {
const rootId = Extract.getIdentifierAt(expr.object, 0);
if (rootId == null) return;
const objectName = resolveBuiltinObjectName(context, rootId);
if (objectName == null) return;
const propertyName = expr.property.name;
const objectImpure = IMPURE_FUNCS.get(objectName);
if (objectImpure == null || !objectImpure.has(propertyName)) return;
const func = Traverse.findParent(node, Check.isFunction);
if (func == null) return;
cEntries.push({ func, node });
break;
}
}
},
NewExpression(node: TSESTree.NewExpression) {
const expr = Extract.unwrap(node.callee);
if (!Check.isIdentifier(expr)) return;
const builtinName = resolveBuiltinObjectName(context, expr);
if (builtinName == null) return;
if (!IMPURE_CTORS.has(builtinName)) return;
const func = Traverse.findParent(node, Check.isFunction);
if (func == null) return;
nEntries.push({ func, node });
},
"Program:exit"(node) {
const comps = fc.api.getAllComponents(node);
const hooks = hc.api.getAllHooks(node);
const funcs = [...comps, ...hooks];
for (const { func, node } of [...cEntries, ...nEntries]) {
if (!funcs.some((f) => f.node === func)) continue;
context.report({
data: {
name: context.sourceCode.getText(node),
},
messageId: "default",
node,
});
}
},
},
);
}