-
Notifications
You must be signed in to change notification settings - Fork 7
/
require-usememo-children.ts
93 lines (87 loc) · 3.15 KB
/
require-usememo-children.ts
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
import { Rule } from "eslint";
import * as ESTree from "estree";
import { TSESTree } from "@typescript-eslint/types";
import {
getExpressionMemoStatus,
isComplexComponent,
MemoStatus,
} from "./common";
const componentNameRegex = /^[^a-z]/;
const hookNameRegex = /^use[A-Z0-9].*$/;
const messages = {
"object-usememo-children":
"Object literal should be wrapped in React.useMemo() when used as children",
"array-usememo-children":
"Array literal should be wrapped in React.useMemo() when used as children",
"instance-usememo-children":
"Object instantiation should be wrapped in React.useMemo() when used as children",
"jsx-usememo-children":
"JSX should be wrapped in React.useMemo() when used as children",
"function-usecallback-children":
"Function definition should be wrapped in React.useCallback() when used as children",
"unknown-usememo-children":
"Unknown value may need to be wrapped in React.useMemo() when used as children",
"usememo-const":
"useMemo/useCallback return value should be assigned to a const to prevent reassignment",
};
const rule: Rule.RuleModule = {
meta: {
messages,
schema: [
{
type: "object",
properties: { strict: { type: "boolean" } },
additionalProperties: false,
},
],
},
create: (context) => {
function report(node: Rule.Node, messageId: keyof typeof messages) {
context.report({ node, messageId: messageId as string });
}
return {
JSXElement: (node: ESTree.Node & Rule.NodeParentExtension) => {
const {
children,
openingElement,
} = (node as unknown) as TSESTree.JSXElement & Rule.NodeParentExtension;
if (!isComplexComponent(openingElement)) return;
for (const child of children) {
if (child.type === "JSXElement" || child.type === "JSXFragment") {
report(node, "jsx-usememo-children");
return;
}
if (child.type === "JSXExpressionContainer") {
const { expression } = child;
if (expression.type !== "JSXEmptyExpression") {
switch (getExpressionMemoStatus(context, expression)) {
case MemoStatus.UnmemoizedObject:
report(node, "object-usememo-children");
break;
case MemoStatus.UnmemoizedArray:
report(node, "array-usememo-children");
break;
case MemoStatus.UnmemoizedNew:
report(node, "instance-usememo-children");
break;
case MemoStatus.UnmemoizedFunction:
report(node, "function-usecallback-children");
break;
case MemoStatus.UnmemoizedFunctionCall:
case MemoStatus.UnmemoizedOther:
if (context.options?.[0]?.strict) {
report(node, "unknown-usememo-children");
}
break;
case MemoStatus.UnmemoizedJSX:
report(node, "jsx-usememo-children");
break;
}
}
}
}
},
};
},
};
export default rule;