@eslint-react/core 2.0.0-next.19 → 2.0.0-next.191
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +465 -288
- package/dist/index.js +1111 -886
- package/package.json +14 -14
package/dist/index.js
CHANGED
|
@@ -1,958 +1,1183 @@
|
|
|
1
|
-
import { AST_NODE_TYPES } from
|
|
2
|
-
import * as
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import * as
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
2
|
+
import * as AST from "@eslint-react/ast";
|
|
3
|
+
import { constFalse, constTrue, dual, flip, getOrElseUpdate, identity, unit } from "@eslint-react/eff";
|
|
4
|
+
import { RE_ANNOTATION_JSX, RE_ANNOTATION_JSX_FRAG, RE_ANNOTATION_JSX_IMPORT_SOURCE, RE_ANNOTATION_JSX_RUNTIME, RE_COMPONENT_NAME, RE_COMPONENT_NAME_LOOSE } from "@eslint-react/kit";
|
|
5
|
+
import { getId } from "@eslint-react/shared";
|
|
6
|
+
import * as VAR from "@eslint-react/var";
|
|
7
|
+
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
8
|
+
import { P, isMatching, match } from "ts-pattern";
|
|
9
|
+
import birecord from "birecord";
|
|
10
|
+
|
|
11
|
+
//#region src/utils/is-react-api.ts
|
|
11
12
|
function isReactAPI(api) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
const func = (context, node) => {
|
|
14
|
+
if (node == null) return false;
|
|
15
|
+
const getText = (n) => context.sourceCode.getText(n);
|
|
16
|
+
const name = AST.toStringFormat(node, getText);
|
|
17
|
+
if (name === api) return true;
|
|
18
|
+
if (name.substring(name.indexOf(".") + 1) === api) return true;
|
|
19
|
+
return false;
|
|
20
|
+
};
|
|
21
|
+
return dual(2, func);
|
|
21
22
|
}
|
|
22
23
|
function isReactAPICall(api) {
|
|
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
|
-
|
|
24
|
+
const func = (context, node) => {
|
|
25
|
+
if (node == null) return false;
|
|
26
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
27
|
+
return isReactAPI(api)(context, node.callee);
|
|
28
|
+
};
|
|
29
|
+
return dual(2, func);
|
|
30
|
+
}
|
|
31
|
+
const isCaptureOwnerStack = isReactAPI("captureOwnerStack");
|
|
32
|
+
const isChildrenCount = isReactAPI("Children.count");
|
|
33
|
+
const isChildrenForEach = isReactAPI("Children.forEach");
|
|
34
|
+
const isChildrenMap = isReactAPI("Children.map");
|
|
35
|
+
const isChildrenOnly = isReactAPI("Children.only");
|
|
36
|
+
const isChildrenToArray = isReactAPI("Children.toArray");
|
|
37
|
+
const isCloneElement = isReactAPI("cloneElement");
|
|
38
|
+
const isCreateContext = isReactAPI("createContext");
|
|
39
|
+
const isCreateElement = isReactAPI("createElement");
|
|
40
|
+
const isCreateRef = isReactAPI("createRef");
|
|
41
|
+
const isForwardRef = isReactAPI("forwardRef");
|
|
42
|
+
const isMemo = isReactAPI("memo");
|
|
43
|
+
const isLazy = isReactAPI("lazy");
|
|
44
|
+
const isCaptureOwnerStackCall = isReactAPICall("captureOwnerStack");
|
|
45
|
+
const isChildrenCountCall = isReactAPICall("Children.count");
|
|
46
|
+
const isChildrenForEachCall = isReactAPICall("Children.forEach");
|
|
47
|
+
const isChildrenMapCall = isReactAPICall("Children.map");
|
|
48
|
+
const isChildrenOnlyCall = isReactAPICall("Children.only");
|
|
49
|
+
const isChildrenToArrayCall = isReactAPICall("Children.toArray");
|
|
50
|
+
const isCloneElementCall = isReactAPICall("cloneElement");
|
|
51
|
+
const isCreateContextCall = isReactAPICall("createContext");
|
|
52
|
+
const isCreateElementCall = isReactAPICall("createElement");
|
|
53
|
+
const isCreateRefCall = isReactAPICall("createRef");
|
|
54
|
+
const isForwardRefCall = isReactAPICall("forwardRef");
|
|
55
|
+
const isMemoCall = isReactAPICall("memo");
|
|
56
|
+
const isLazyCall = isReactAPICall("lazy");
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/component/component-children.ts
|
|
60
|
+
/**
|
|
61
|
+
* Determines whether inside `createElement`'s children.
|
|
62
|
+
* @param context The rule context
|
|
63
|
+
* @param node The AST node to check
|
|
64
|
+
* @returns `true` if the node is inside createElement's children
|
|
65
|
+
*/
|
|
58
66
|
function isChildrenOfCreateElement(context, node) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
const parent = node.parent;
|
|
68
|
+
if (parent == null || parent.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
69
|
+
if (!isCreateElementCall(context, parent)) return false;
|
|
70
|
+
return parent.arguments.slice(2).some((arg) => arg === node);
|
|
63
71
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const { init } = node;
|
|
88
|
-
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) {
|
|
89
|
-
return isInitializedFromReact(init.object.name, importSource, initialScope);
|
|
90
|
-
}
|
|
91
|
-
if (init.type === AST_NODE_TYPES.Identifier) {
|
|
92
|
-
return isInitializedFromReact(init.name, importSource, initialScope);
|
|
93
|
-
}
|
|
94
|
-
const args = getRequireExpressionArguments(init);
|
|
95
|
-
const arg0 = args?.[0];
|
|
96
|
-
if (arg0 == null || !AST14.isLiteral(arg0, "string")) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
return arg0.value === importSource || arg0.value.startsWith(`${importSource}/`);
|
|
100
|
-
}
|
|
101
|
-
return parent?.type === AST_NODE_TYPES.ImportDeclaration && parent.source.value === importSource;
|
|
102
|
-
}
|
|
103
|
-
function isInstanceIdEqual(context, a, b) {
|
|
104
|
-
return AST14.isNodeEqual(a, b) || VAR3.isNodeValueEqual(a, b, [
|
|
105
|
-
context.sourceCode.getScope(a),
|
|
106
|
-
context.sourceCode.getScope(b)
|
|
107
|
-
]);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// src/hook/hook-name.ts
|
|
111
|
-
var REACT_BUILTIN_HOOK_NAMES = [
|
|
112
|
-
"use",
|
|
113
|
-
"useActionState",
|
|
114
|
-
"useCallback",
|
|
115
|
-
"useContext",
|
|
116
|
-
"useDebugValue",
|
|
117
|
-
"useDeferredValue",
|
|
118
|
-
"useEffect",
|
|
119
|
-
"useFormStatus",
|
|
120
|
-
"useId",
|
|
121
|
-
"useImperativeHandle",
|
|
122
|
-
"useInsertionEffect",
|
|
123
|
-
"useLayoutEffect",
|
|
124
|
-
"useMemo",
|
|
125
|
-
"useOptimistic",
|
|
126
|
-
"useReducer",
|
|
127
|
-
"useRef",
|
|
128
|
-
"useState",
|
|
129
|
-
"useSyncExternalStore",
|
|
130
|
-
"useTransition"
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/hook/hook-name.ts
|
|
75
|
+
const REACT_BUILTIN_HOOK_NAMES = [
|
|
76
|
+
"use",
|
|
77
|
+
"useActionState",
|
|
78
|
+
"useCallback",
|
|
79
|
+
"useContext",
|
|
80
|
+
"useDebugValue",
|
|
81
|
+
"useDeferredValue",
|
|
82
|
+
"useEffect",
|
|
83
|
+
"useFormStatus",
|
|
84
|
+
"useId",
|
|
85
|
+
"useImperativeHandle",
|
|
86
|
+
"useInsertionEffect",
|
|
87
|
+
"useLayoutEffect",
|
|
88
|
+
"useMemo",
|
|
89
|
+
"useOptimistic",
|
|
90
|
+
"useReducer",
|
|
91
|
+
"useRef",
|
|
92
|
+
"useState",
|
|
93
|
+
"useSyncExternalStore",
|
|
94
|
+
"useTransition"
|
|
131
95
|
];
|
|
96
|
+
/**
|
|
97
|
+
* Catch all identifiers that begin with "use" followed by an uppercase Latin
|
|
98
|
+
* character to exclude identifiers like "user".
|
|
99
|
+
* @param name The name of the identifier to check.
|
|
100
|
+
* @see https://github.com/facebook/react/blob/1d6c8168db1d82713202e842df3167787ffa00ed/packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts#L16
|
|
101
|
+
*/
|
|
132
102
|
function isReactHookName(name) {
|
|
133
|
-
|
|
103
|
+
return name === "use" || /^use[A-Z0-9]/.test(name);
|
|
134
104
|
}
|
|
135
105
|
|
|
136
|
-
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/hook/hook-is.ts
|
|
108
|
+
/**
|
|
109
|
+
* Determines if a function node is a React Hook based on its name.
|
|
110
|
+
* @param node The function node to check
|
|
111
|
+
* @returns True if the function is a React Hook, false otherwise
|
|
112
|
+
*/
|
|
137
113
|
function isReactHook(node) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
114
|
+
if (node == null) return false;
|
|
115
|
+
const id = AST.getFunctionId(node);
|
|
116
|
+
return id?.name != null && isReactHookName(id.name);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Check if the given node is a React Hook call by its name.
|
|
120
|
+
* @param node The node to check.
|
|
121
|
+
* @returns `true` if the node is a React Hook call, `false` otherwise.
|
|
122
|
+
*/
|
|
142
123
|
function isReactHookCall(node) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
function isReactHookCallWithName(
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
var isUseCall = flip(isReactHookCallWithName)("use");
|
|
218
|
-
var isUseActionStateCall = flip(isReactHookCallWithName)("useActionState");
|
|
219
|
-
var isUseCallbackCall = flip(isReactHookCallWithName)("useCallback");
|
|
220
|
-
var isUseContextCall = flip(isReactHookCallWithName)("useContext");
|
|
221
|
-
var isUseDebugValueCall = flip(isReactHookCallWithName)("useDebugValue");
|
|
222
|
-
var isUseDeferredValueCall = flip(isReactHookCallWithName)("useDeferredValue");
|
|
223
|
-
var isUseEffectCall = flip(isReactHookCallWithName)("useEffect");
|
|
224
|
-
var isUseFormStatusCall = flip(isReactHookCallWithName)("useFormStatus");
|
|
225
|
-
var isUseIdCall = flip(isReactHookCallWithName)("useId");
|
|
226
|
-
var isUseImperativeHandleCall = flip(isReactHookCallWithName)("useImperativeHandle");
|
|
227
|
-
var isUseInsertionEffectCall = flip(isReactHookCallWithName)("useInsertionEffect");
|
|
228
|
-
var isUseLayoutEffectCall = flip(isReactHookCallWithName)("useLayoutEffect");
|
|
229
|
-
var isUseMemoCall = flip(isReactHookCallWithName)("useMemo");
|
|
230
|
-
var isUseOptimisticCall = flip(isReactHookCallWithName)("useOptimistic");
|
|
231
|
-
var isUseReducerCall = flip(isReactHookCallWithName)("useReducer");
|
|
232
|
-
var isUseRefCall = flip(isReactHookCallWithName)("useRef");
|
|
233
|
-
var isUseStateCall = flip(isReactHookCallWithName)("useState");
|
|
234
|
-
var isUseSyncExternalStoreCall = flip(isReactHookCallWithName)("useSyncExternalStore");
|
|
235
|
-
var isUseTransitionCall = flip(isReactHookCallWithName)("useTransition");
|
|
236
|
-
|
|
237
|
-
// src/hook/hook-collector.ts
|
|
124
|
+
if (node == null) return false;
|
|
125
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
126
|
+
if (node.callee.type === AST_NODE_TYPES.Identifier) return isReactHookName(node.callee.name);
|
|
127
|
+
if (node.callee.type === AST_NODE_TYPES.MemberExpression) return node.callee.property.type === AST_NODE_TYPES.Identifier && isReactHookName(node.callee.property.name);
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Checks if a node is a call to a specific React hook.
|
|
132
|
+
* Returns a function that accepts a hook name to check against.
|
|
133
|
+
* @param node The AST node to check
|
|
134
|
+
* @returns A function that takes a hook name and returns boolean
|
|
135
|
+
*/
|
|
136
|
+
function isReactHookCallWithName(node) {
|
|
137
|
+
if (node == null || node.type !== AST_NODE_TYPES.CallExpression) return constFalse;
|
|
138
|
+
return (name) => {
|
|
139
|
+
switch (node.callee.type) {
|
|
140
|
+
case AST_NODE_TYPES.Identifier: return node.callee.name === name;
|
|
141
|
+
case AST_NODE_TYPES.MemberExpression: return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name;
|
|
142
|
+
default: return false;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Checks if a node is a call to a specific React hook or one of its aliases.
|
|
148
|
+
* @param name The primary hook name to check
|
|
149
|
+
* @param alias Optional array of alias names to also accept
|
|
150
|
+
* @returns Function that checks if a node matches the hook name or aliases
|
|
151
|
+
*/
|
|
152
|
+
function isReactHookCallWithNameAlias(name, alias = []) {
|
|
153
|
+
return (node) => {
|
|
154
|
+
switch (true) {
|
|
155
|
+
case node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === name: return true;
|
|
156
|
+
case node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object: return true;
|
|
157
|
+
default: return alias.some(isReactHookCallWithName(node));
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Detects useEffect calls and variations (useLayoutEffect, etc.) using regex pattern.
|
|
163
|
+
* @param node The AST node to check
|
|
164
|
+
* @returns True if the node is a useEffect-like call
|
|
165
|
+
*/
|
|
166
|
+
function isUseEffectLikeCall(node) {
|
|
167
|
+
if (node == null) return false;
|
|
168
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
169
|
+
switch (node.callee.type) {
|
|
170
|
+
case AST_NODE_TYPES.Identifier: return /^use\w*Effect$/u.test(node.callee.name);
|
|
171
|
+
case AST_NODE_TYPES.MemberExpression: return node.callee.property.type === AST_NODE_TYPES.Identifier && /^use\w*Effect$/u.test(node.callee.property.name);
|
|
172
|
+
default: return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const isUseCall = flip(isReactHookCallWithName)("use");
|
|
176
|
+
const isUseActionStateCall = flip(isReactHookCallWithName)("useActionState");
|
|
177
|
+
const isUseCallbackCall = flip(isReactHookCallWithName)("useCallback");
|
|
178
|
+
const isUseContextCall = flip(isReactHookCallWithName)("useContext");
|
|
179
|
+
const isUseDebugValueCall = flip(isReactHookCallWithName)("useDebugValue");
|
|
180
|
+
const isUseDeferredValueCall = flip(isReactHookCallWithName)("useDeferredValue");
|
|
181
|
+
const isUseEffectCall = flip(isReactHookCallWithName)("useEffect");
|
|
182
|
+
const isUseFormStatusCall = flip(isReactHookCallWithName)("useFormStatus");
|
|
183
|
+
const isUseIdCall = flip(isReactHookCallWithName)("useId");
|
|
184
|
+
const isUseImperativeHandleCall = flip(isReactHookCallWithName)("useImperativeHandle");
|
|
185
|
+
const isUseInsertionEffectCall = flip(isReactHookCallWithName)("useInsertionEffect");
|
|
186
|
+
const isUseLayoutEffectCall = flip(isReactHookCallWithName)("useLayoutEffect");
|
|
187
|
+
const isUseMemoCall = flip(isReactHookCallWithName)("useMemo");
|
|
188
|
+
const isUseOptimisticCall = flip(isReactHookCallWithName)("useOptimistic");
|
|
189
|
+
const isUseReducerCall = flip(isReactHookCallWithName)("useReducer");
|
|
190
|
+
const isUseRefCall = flip(isReactHookCallWithName)("useRef");
|
|
191
|
+
const isUseStateCall = flip(isReactHookCallWithName)("useState");
|
|
192
|
+
const isUseSyncExternalStoreCall = flip(isReactHookCallWithName)("useSyncExternalStore");
|
|
193
|
+
const isUseTransitionCall = flip(isReactHookCallWithName)("useTransition");
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/hook/hook-collector.ts
|
|
238
197
|
function useHookCollector() {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
198
|
+
const hooks = /* @__PURE__ */ new Map();
|
|
199
|
+
const functionEntries = [];
|
|
200
|
+
const onFunctionEnter = (node) => {
|
|
201
|
+
const id = AST.getFunctionId(node);
|
|
202
|
+
const key = getId();
|
|
203
|
+
const name = id?.name;
|
|
204
|
+
if (name != null && isReactHookName(name)) {
|
|
205
|
+
functionEntries.push({
|
|
206
|
+
key,
|
|
207
|
+
node,
|
|
208
|
+
isHook: true
|
|
209
|
+
});
|
|
210
|
+
hooks.set(key, {
|
|
211
|
+
id,
|
|
212
|
+
key,
|
|
213
|
+
kind: "function",
|
|
214
|
+
name,
|
|
215
|
+
node,
|
|
216
|
+
flag: 0n,
|
|
217
|
+
hint: 0n,
|
|
218
|
+
hookCalls: []
|
|
219
|
+
});
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
functionEntries.push({
|
|
223
|
+
key,
|
|
224
|
+
node,
|
|
225
|
+
isHook: false
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
const onFunctionExit = () => {
|
|
229
|
+
functionEntries.pop();
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
ctx: { getAllHooks(node) {
|
|
233
|
+
return hooks;
|
|
234
|
+
} },
|
|
235
|
+
listeners: {
|
|
236
|
+
":function[type]": onFunctionEnter,
|
|
237
|
+
":function[type]:exit": onFunctionExit,
|
|
238
|
+
"CallExpression[type]"(node) {
|
|
239
|
+
if (!isReactHookCall(node)) return;
|
|
240
|
+
const fEntry = functionEntries.at(-1);
|
|
241
|
+
if (fEntry?.key == null) return;
|
|
242
|
+
const hook = hooks.get(fEntry.key);
|
|
243
|
+
if (hook == null) return;
|
|
244
|
+
hook.hookCalls.push(node);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
};
|
|
289
248
|
}
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/hook/hook-id.ts
|
|
290
252
|
function isReactHookId(id) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
default:
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
253
|
+
switch (id.type) {
|
|
254
|
+
case AST_NODE_TYPES.Identifier: return isReactHookName(id.name);
|
|
255
|
+
case AST_NODE_TYPES.MemberExpression: return "name" in id.property && isReactHookName(id.property.name);
|
|
256
|
+
default: return false;
|
|
257
|
+
}
|
|
299
258
|
}
|
|
259
|
+
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/jsx/jsx-stringify.ts
|
|
262
|
+
/**
|
|
263
|
+
* Incomplete but sufficient stringification of JSX nodes for common use cases
|
|
264
|
+
*
|
|
265
|
+
* @param node - JSX node from TypeScript ESTree
|
|
266
|
+
* @returns String representation of the JSX node
|
|
267
|
+
*/
|
|
300
268
|
function stringifyJsx(node) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
// src/jsx/jsx-attribute-name.ts
|
|
269
|
+
switch (node.type) {
|
|
270
|
+
case AST_NODE_TYPES.JSXIdentifier: return node.name;
|
|
271
|
+
case AST_NODE_TYPES.JSXNamespacedName: return `${node.namespace.name}:${node.name.name}`;
|
|
272
|
+
case AST_NODE_TYPES.JSXMemberExpression: return `${stringifyJsx(node.object)}.${stringifyJsx(node.property)}`;
|
|
273
|
+
case AST_NODE_TYPES.JSXText: return node.value;
|
|
274
|
+
case AST_NODE_TYPES.JSXOpeningElement: return `<${stringifyJsx(node.name)}>`;
|
|
275
|
+
case AST_NODE_TYPES.JSXClosingElement: return `</${stringifyJsx(node.name)}>`;
|
|
276
|
+
case AST_NODE_TYPES.JSXOpeningFragment: return "<>";
|
|
277
|
+
case AST_NODE_TYPES.JSXClosingFragment: return "</>";
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/jsx/jsx-attribute-name.ts
|
|
283
|
+
/**
|
|
284
|
+
* Get the stringified name of a JSX attribute
|
|
285
|
+
* @param context The ESLint rule context
|
|
286
|
+
* @param node The JSX attribute node
|
|
287
|
+
* @returns The name of the attribute
|
|
288
|
+
*/
|
|
322
289
|
function getAttributeName(context, node) {
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
290
|
+
return stringifyJsx(node.name);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/jsx/jsx-attribute.ts
|
|
295
|
+
function getAttribute(context, attributes, initialScope) {
|
|
296
|
+
return (name) => {
|
|
297
|
+
return attributes.findLast((attr) => {
|
|
298
|
+
if (attr.type === AST_NODE_TYPES.JSXAttribute) return getAttributeName(context, attr) === name;
|
|
299
|
+
if (initialScope == null) return false;
|
|
300
|
+
switch (attr.argument.type) {
|
|
301
|
+
case AST_NODE_TYPES.Identifier: {
|
|
302
|
+
const variable = VAR.findVariable(attr.argument.name, initialScope);
|
|
303
|
+
const variableNode = VAR.getVariableDefinitionNode(variable, 0);
|
|
304
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) return VAR.findProperty(name, variableNode.properties, initialScope) != null;
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
case AST_NODE_TYPES.ObjectExpression: return VAR.findProperty(name, attr.argument.properties, initialScope) != null;
|
|
308
|
+
}
|
|
309
|
+
return false;
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/jsx/jsx-attribute-value.ts
|
|
316
|
+
function resolveAttributeValue(context, attribute) {
|
|
317
|
+
const initialScope = context.sourceCode.getScope(attribute);
|
|
318
|
+
function handleJsxAttribute(node) {
|
|
319
|
+
if (node.value == null) return {
|
|
320
|
+
kind: "boolean",
|
|
321
|
+
toStatic() {
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
switch (node.value.type) {
|
|
326
|
+
case AST_NODE_TYPES.Literal: {
|
|
327
|
+
const staticValue = node.value.value;
|
|
328
|
+
return {
|
|
329
|
+
kind: "literal",
|
|
330
|
+
node: node.value,
|
|
331
|
+
toStatic() {
|
|
332
|
+
return staticValue;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
case AST_NODE_TYPES.JSXExpressionContainer: {
|
|
337
|
+
const expr = node.value.expression;
|
|
338
|
+
return {
|
|
339
|
+
kind: "expression",
|
|
340
|
+
node: expr,
|
|
341
|
+
toStatic() {
|
|
342
|
+
return getStaticValue(expr, initialScope)?.value;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
case AST_NODE_TYPES.JSXElement: return {
|
|
347
|
+
kind: "element",
|
|
348
|
+
node: node.value,
|
|
349
|
+
toStatic() {
|
|
350
|
+
return unit;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
case AST_NODE_TYPES.JSXSpreadChild: return {
|
|
354
|
+
kind: "spreadChild",
|
|
355
|
+
node: node.value.expression,
|
|
356
|
+
toStatic() {
|
|
357
|
+
return unit;
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function handleJsxSpreadAttribute(node) {
|
|
363
|
+
return {
|
|
364
|
+
kind: "spreadProps",
|
|
365
|
+
node: node.argument,
|
|
366
|
+
toStatic(name) {
|
|
367
|
+
if (name == null) return unit;
|
|
368
|
+
return match(getStaticValue(node.argument, initialScope)?.value).with({ [name]: P.select(P.any) }, identity).otherwise(() => unit);
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
switch (attribute.type) {
|
|
373
|
+
case AST_NODE_TYPES.JSXAttribute: return handleJsxAttribute(attribute);
|
|
374
|
+
case AST_NODE_TYPES.JSXSpreadAttribute: return handleJsxSpreadAttribute(attribute);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/jsx/jsx-config.ts
|
|
380
|
+
const JsxEmit = {
|
|
381
|
+
None: 0,
|
|
382
|
+
Preserve: 1,
|
|
383
|
+
React: 2,
|
|
384
|
+
ReactNative: 3,
|
|
385
|
+
ReactJSX: 4,
|
|
386
|
+
ReactJSXDev: 5
|
|
403
387
|
};
|
|
404
|
-
|
|
388
|
+
/**
|
|
389
|
+
* Get JsxConfig from the rule context by reading compiler options.
|
|
390
|
+
* @param context The RuleContext.
|
|
391
|
+
* @returns JsxConfig derived from compiler options.
|
|
392
|
+
*/
|
|
393
|
+
function getJsxConfigFromContext(context) {
|
|
394
|
+
const options = context.sourceCode.parserServices?.program?.getCompilerOptions() ?? {};
|
|
395
|
+
return {
|
|
396
|
+
jsx: options.jsx ?? JsxEmit.ReactJSX,
|
|
397
|
+
jsxFactory: options.jsxFactory ?? "React.createElement",
|
|
398
|
+
jsxFragmentFactory: options.jsxFragmentFactory ?? "React.Fragment",
|
|
399
|
+
jsxImportSource: options.jsxImportSource ?? "react"
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
403
|
+
/**
|
|
404
|
+
* Get JsxConfig from pragma comments (annotations) in the source code.
|
|
405
|
+
* @param context The RuleContext.
|
|
406
|
+
* @returns JsxConfig derived from pragma comments.
|
|
407
|
+
*/
|
|
408
|
+
function getJsxConfigFromAnnotation(context) {
|
|
409
|
+
return getOrElseUpdate(cache, context.sourceCode, () => {
|
|
410
|
+
const options = {};
|
|
411
|
+
if (!context.sourceCode.text.includes("@jsx")) return options;
|
|
412
|
+
let jsx, jsxFrag, jsxRuntime, jsxImportSource;
|
|
413
|
+
for (const comment of context.sourceCode.getAllComments().reverse()) {
|
|
414
|
+
const value = comment.value;
|
|
415
|
+
jsx ??= value.match(RE_ANNOTATION_JSX)?.[1];
|
|
416
|
+
jsxFrag ??= value.match(RE_ANNOTATION_JSX_FRAG)?.[1];
|
|
417
|
+
jsxRuntime ??= value.match(RE_ANNOTATION_JSX_RUNTIME)?.[1];
|
|
418
|
+
jsxImportSource ??= value.match(RE_ANNOTATION_JSX_IMPORT_SOURCE)?.[1];
|
|
419
|
+
}
|
|
420
|
+
if (jsx != null) options.jsxFactory = jsx;
|
|
421
|
+
if (jsxFrag != null) options.jsxFragmentFactory = jsxFrag;
|
|
422
|
+
if (jsxRuntime != null) options.jsx = jsxRuntime === "classic" ? JsxEmit.React : JsxEmit.ReactJSX;
|
|
423
|
+
if (jsxImportSource != null) options.jsxImportSource = jsxImportSource;
|
|
424
|
+
return options;
|
|
425
|
+
});
|
|
426
|
+
}
|
|
405
427
|
|
|
406
|
-
|
|
428
|
+
//#endregion
|
|
429
|
+
//#region src/jsx/jsx-detection.ts
|
|
430
|
+
/**
|
|
431
|
+
* Flags to control JSX detection behavior:
|
|
432
|
+
* - Skip* flags: Ignore specific node types when detecting JSX
|
|
433
|
+
* - Strict* flags: Enforce stricter rules for container types
|
|
434
|
+
*/
|
|
435
|
+
const JSXDetectionHint = {
|
|
436
|
+
None: 0n,
|
|
437
|
+
SkipUndefined: 1n << 0n,
|
|
438
|
+
SkipNullLiteral: 1n << 1n,
|
|
439
|
+
SkipBooleanLiteral: 1n << 2n,
|
|
440
|
+
SkipStringLiteral: 1n << 3n,
|
|
441
|
+
SkipNumberLiteral: 1n << 4n,
|
|
442
|
+
SkipBigIntLiteral: 1n << 5n,
|
|
443
|
+
SkipEmptyArray: 1n << 6n,
|
|
444
|
+
SkipCreateElement: 1n << 7n,
|
|
445
|
+
StrictArray: 1n << 8n,
|
|
446
|
+
StrictLogical: 1n << 9n,
|
|
447
|
+
StrictConditional: 1n << 10n
|
|
448
|
+
};
|
|
449
|
+
/**
|
|
450
|
+
* Default JSX detection configuration
|
|
451
|
+
* Skips undefined and boolean literals (common in React)
|
|
452
|
+
*/
|
|
453
|
+
const DEFAULT_JSX_DETECTION_HINT = 0n | JSXDetectionHint.SkipUndefined | JSXDetectionHint.SkipBooleanLiteral;
|
|
454
|
+
/**
|
|
455
|
+
* Checks if a node is a `JSXText` or a `Literal` node
|
|
456
|
+
* @param node The AST node to check
|
|
457
|
+
* @returns `true` if the node is a `JSXText` or a `Literal` node
|
|
458
|
+
*/
|
|
407
459
|
function isJsxText(node) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
460
|
+
if (node == null) return false;
|
|
461
|
+
return node.type === AST_NODE_TYPES.JSXText || node.type === AST_NODE_TYPES.Literal;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Determines if a node represents JSX-like content based on heuristics
|
|
465
|
+
* Supports configuration through hint flags to customize detection behavior
|
|
466
|
+
*
|
|
467
|
+
* @param code The source code with scope lookup capability
|
|
468
|
+
* @param code.getScope The function to get the scope of a node
|
|
469
|
+
* @param node The AST node to analyze
|
|
470
|
+
* @param hint The configuration flags to adjust detection behavior
|
|
471
|
+
* @returns boolean Whether the node is considered JSX-like
|
|
472
|
+
*/
|
|
411
473
|
function isJsxLike(code, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
return isJsxLike(code, exp, hint);
|
|
469
|
-
}
|
|
470
|
-
case AST_NODE_TYPES.CallExpression: {
|
|
471
|
-
if (hint & JSXDetectionHint.SkipCreateElement) {
|
|
472
|
-
return false;
|
|
473
|
-
}
|
|
474
|
-
switch (node.callee.type) {
|
|
475
|
-
case AST_NODE_TYPES.Identifier:
|
|
476
|
-
return node.callee.name === "createElement";
|
|
477
|
-
case AST_NODE_TYPES.MemberExpression:
|
|
478
|
-
return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "createElement";
|
|
479
|
-
}
|
|
480
|
-
return false;
|
|
481
|
-
}
|
|
482
|
-
case AST_NODE_TYPES.Identifier: {
|
|
483
|
-
const { name } = node;
|
|
484
|
-
if (name === "undefined") {
|
|
485
|
-
return !(hint & JSXDetectionHint.SkipUndefined);
|
|
486
|
-
}
|
|
487
|
-
if (AST14.isJSXTagNameExpression(node)) {
|
|
488
|
-
return true;
|
|
489
|
-
}
|
|
490
|
-
const variable = VAR3.findVariable(name, code.getScope(node));
|
|
491
|
-
const variableNode = variable && VAR3.getVariableInitNode(variable, 0);
|
|
492
|
-
return !!variableNode && isJsxLike(code, variableNode, hint);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
return false;
|
|
474
|
+
if (node == null) return false;
|
|
475
|
+
if (AST.isJSX(node)) return true;
|
|
476
|
+
switch (node.type) {
|
|
477
|
+
case AST_NODE_TYPES.Literal:
|
|
478
|
+
switch (typeof node.value) {
|
|
479
|
+
case "boolean": return !(hint & JSXDetectionHint.SkipBooleanLiteral);
|
|
480
|
+
case "string": return !(hint & JSXDetectionHint.SkipStringLiteral);
|
|
481
|
+
case "number": return !(hint & JSXDetectionHint.SkipNumberLiteral);
|
|
482
|
+
case "bigint": return !(hint & JSXDetectionHint.SkipBigIntLiteral);
|
|
483
|
+
}
|
|
484
|
+
if (node.value == null) return !(hint & JSXDetectionHint.SkipNullLiteral);
|
|
485
|
+
return false;
|
|
486
|
+
case AST_NODE_TYPES.TemplateLiteral: return !(hint & JSXDetectionHint.SkipStringLiteral);
|
|
487
|
+
case AST_NODE_TYPES.ArrayExpression:
|
|
488
|
+
if (node.elements.length === 0) return !(hint & JSXDetectionHint.SkipEmptyArray);
|
|
489
|
+
if (hint & JSXDetectionHint.StrictArray) return node.elements.every((n) => isJsxLike(code, n, hint));
|
|
490
|
+
return node.elements.some((n) => isJsxLike(code, n, hint));
|
|
491
|
+
case AST_NODE_TYPES.LogicalExpression:
|
|
492
|
+
if (hint & JSXDetectionHint.StrictLogical) return isJsxLike(code, node.left, hint) && isJsxLike(code, node.right, hint);
|
|
493
|
+
return isJsxLike(code, node.left, hint) || isJsxLike(code, node.right, hint);
|
|
494
|
+
case AST_NODE_TYPES.ConditionalExpression: {
|
|
495
|
+
function leftHasJSX(node$1) {
|
|
496
|
+
if (Array.isArray(node$1.consequent)) {
|
|
497
|
+
if (node$1.consequent.length === 0) return !(hint & JSXDetectionHint.SkipEmptyArray);
|
|
498
|
+
if (hint & JSXDetectionHint.StrictArray) return node$1.consequent.every((n) => isJsxLike(code, n, hint));
|
|
499
|
+
return node$1.consequent.some((n) => isJsxLike(code, n, hint));
|
|
500
|
+
}
|
|
501
|
+
return isJsxLike(code, node$1.consequent, hint);
|
|
502
|
+
}
|
|
503
|
+
function rightHasJSX(node$1) {
|
|
504
|
+
return isJsxLike(code, node$1.alternate, hint);
|
|
505
|
+
}
|
|
506
|
+
if (hint & JSXDetectionHint.StrictConditional) return leftHasJSX(node) && rightHasJSX(node);
|
|
507
|
+
return leftHasJSX(node) || rightHasJSX(node);
|
|
508
|
+
}
|
|
509
|
+
case AST_NODE_TYPES.SequenceExpression: {
|
|
510
|
+
const exp = node.expressions.at(-1);
|
|
511
|
+
return isJsxLike(code, exp, hint);
|
|
512
|
+
}
|
|
513
|
+
case AST_NODE_TYPES.CallExpression:
|
|
514
|
+
if (hint & JSXDetectionHint.SkipCreateElement) return false;
|
|
515
|
+
switch (node.callee.type) {
|
|
516
|
+
case AST_NODE_TYPES.Identifier: return node.callee.name === "createElement";
|
|
517
|
+
case AST_NODE_TYPES.MemberExpression: return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "createElement";
|
|
518
|
+
}
|
|
519
|
+
return false;
|
|
520
|
+
case AST_NODE_TYPES.Identifier: {
|
|
521
|
+
const { name } = node;
|
|
522
|
+
if (name === "undefined") return !(hint & JSXDetectionHint.SkipUndefined);
|
|
523
|
+
if (AST.isJSXTagNameExpression(node)) return true;
|
|
524
|
+
const variable = VAR.findVariable(name, code.getScope(node));
|
|
525
|
+
const variableNode = variable && VAR.getVariableDefinitionNode(variable, 0);
|
|
526
|
+
return !!variableNode && isJsxLike(code, variableNode, hint);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return false;
|
|
496
530
|
}
|
|
531
|
+
|
|
532
|
+
//#endregion
|
|
533
|
+
//#region src/jsx/jsx-element-type.ts
|
|
534
|
+
/**
|
|
535
|
+
* Extracts the element type name from a JSX element or fragment
|
|
536
|
+
* For JSX elements, returns the stringified name (e.g., "div", "Button", "React.Fragment")
|
|
537
|
+
* For JSX fragments, returns an empty string
|
|
538
|
+
*
|
|
539
|
+
* @param context - ESLint rule context
|
|
540
|
+
* @param node - JSX element or fragment node
|
|
541
|
+
* @returns String representation of the element type
|
|
542
|
+
*/
|
|
497
543
|
function getElementType(context, node) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
return stringifyJsx(node.openingElement.name);
|
|
544
|
+
if (node.type === AST_NODE_TYPES.JSXFragment) return "";
|
|
545
|
+
return stringifyJsx(node.openingElement.name);
|
|
502
546
|
}
|
|
503
547
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/jsx/jsx-element-is.ts
|
|
550
|
+
/**
|
|
551
|
+
* Determines if a JSX element is a host element
|
|
552
|
+
* Host elements in React start with lowercase letters (e.g., div, span)
|
|
553
|
+
*
|
|
554
|
+
* @param context - ESLint rule context
|
|
555
|
+
* @param node - AST node to check
|
|
556
|
+
* @returns boolean indicating if the element is a host element
|
|
557
|
+
*/
|
|
558
|
+
function isHostElement(context, node) {
|
|
559
|
+
return node.type === AST_NODE_TYPES.JSXElement && node.openingElement.name.type === AST_NODE_TYPES.JSXIdentifier && /^[a-z]/u.test(node.openingElement.name.name);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Determines if a JSX element is a React Fragment
|
|
563
|
+
* Fragments can be imported from React and used like <Fragment> or <React.Fragment>
|
|
564
|
+
*
|
|
565
|
+
* @param context - ESLint rule context
|
|
566
|
+
* @param node - AST node to check
|
|
567
|
+
* @returns boolean indicating if the element is a Fragment with type narrowing
|
|
568
|
+
*/
|
|
569
|
+
function isFragmentElement(context, node) {
|
|
570
|
+
if (node.type !== AST_NODE_TYPES.JSXElement) return false;
|
|
571
|
+
return getElementType(context, node).split(".").at(-1) === "Fragment";
|
|
507
572
|
}
|
|
573
|
+
|
|
574
|
+
//#endregion
|
|
575
|
+
//#region src/jsx/jsx-has.ts
|
|
576
|
+
/**
|
|
577
|
+
* Checks if a JSX element has a specific attribute
|
|
578
|
+
*
|
|
579
|
+
* @param context - ESLint rule context
|
|
580
|
+
* @param name - Name of the attribute to check for
|
|
581
|
+
* @param attributes - List of JSX attributes from opening element
|
|
582
|
+
* @param initialScope - Optional scope for resolving variables in spread attributes
|
|
583
|
+
* @returns boolean indicating whether the attribute exists
|
|
584
|
+
*/
|
|
585
|
+
function hasAttribute(context, name, attributes, initialScope) {
|
|
586
|
+
return getAttribute(context, attributes, initialScope)(name) != null;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Checks if a JSX element has at least one of the specified attributes
|
|
590
|
+
*
|
|
591
|
+
* @param context - ESLint rule context
|
|
592
|
+
* @param names - Array of attribute names to check for
|
|
593
|
+
* @param attributes - List of JSX attributes from opening element
|
|
594
|
+
* @param initialScope - Optional scope for resolving variables in spread attributes
|
|
595
|
+
* @returns boolean indicating whether any of the attributes exist
|
|
596
|
+
*/
|
|
508
597
|
function hasAnyAttribute(context, names, attributes, initialScope) {
|
|
509
|
-
|
|
510
|
-
}
|
|
598
|
+
return names.some((n) => hasAttribute(context, n, attributes, initialScope));
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Checks if a JSX element has all of the specified attributes
|
|
602
|
+
*
|
|
603
|
+
* @param context - ESLint rule context
|
|
604
|
+
* @param names - Array of attribute names to check for
|
|
605
|
+
* @param attributes - List of JSX attributes from opening element
|
|
606
|
+
* @param initialScope - Optional scope for resolving variables in spread attributes
|
|
607
|
+
* @returns boolean indicating whether all of the attributes exist
|
|
608
|
+
*/
|
|
511
609
|
function hasEveryAttribute(context, names, attributes, initialScope) {
|
|
512
|
-
|
|
610
|
+
return names.every((n) => hasAttribute(context, n, attributes, initialScope));
|
|
513
611
|
}
|
|
612
|
+
|
|
613
|
+
//#endregion
|
|
614
|
+
//#region src/jsx/jsx-hierarchy.ts
|
|
615
|
+
/**
|
|
616
|
+
* Traverses up the AST to find a parent JSX attribute node that matches a given test
|
|
617
|
+
*
|
|
618
|
+
* @param node - The starting AST node
|
|
619
|
+
* @param test - Optional predicate function to test if the attribute meets criteria
|
|
620
|
+
* Defaults to always returning true (matches any attribute)
|
|
621
|
+
* @returns The first matching JSX attribute node found when traversing upwards, or undefined
|
|
622
|
+
*/
|
|
514
623
|
function findParentAttribute(node, test = constTrue) {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
624
|
+
const guard = (node$1) => {
|
|
625
|
+
return node$1.type === AST_NODE_TYPES.JSXAttribute && test(node$1);
|
|
626
|
+
};
|
|
627
|
+
return AST.findParentNode(node, guard);
|
|
519
628
|
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
var ComponentDetectionHint = {
|
|
535
|
-
/**
|
|
536
|
-
* 1n << 0n - 1n << 63n are reserved for JSXDetectionHint
|
|
537
|
-
*/
|
|
538
|
-
...JSXDetectionHint,
|
|
539
|
-
/**
|
|
540
|
-
* Skip function component created by React.memo
|
|
541
|
-
*/
|
|
542
|
-
SkipMemo: 1n << 64n,
|
|
543
|
-
/**
|
|
544
|
-
* Skip function component created by React.forwardRef
|
|
545
|
-
*/
|
|
546
|
-
SkipForwardRef: 1n << 65n,
|
|
547
|
-
/**
|
|
548
|
-
* Skip function component defined as array map argument
|
|
549
|
-
*/
|
|
550
|
-
SkipArrayMapArgument: 1n << 66n,
|
|
551
|
-
/**
|
|
552
|
-
* Skip function component defined on object method
|
|
553
|
-
*/
|
|
554
|
-
SkipObjectMethod: 1n << 67n,
|
|
555
|
-
/**
|
|
556
|
-
* Skip function component defined on class method
|
|
557
|
-
*/
|
|
558
|
-
SkipClassMethod: 1n << 68n,
|
|
559
|
-
/**
|
|
560
|
-
* Skip function component defined on class property
|
|
561
|
-
*/
|
|
562
|
-
SkipClassProperty: 1n << 69n
|
|
629
|
+
|
|
630
|
+
//#endregion
|
|
631
|
+
//#region src/component/component-detection-hint.ts
|
|
632
|
+
/**
|
|
633
|
+
* Hints for component collector
|
|
634
|
+
*/
|
|
635
|
+
const ComponentDetectionHint = {
|
|
636
|
+
...JSXDetectionHint,
|
|
637
|
+
SkipMemo: 1n << 64n,
|
|
638
|
+
SkipForwardRef: 1n << 65n,
|
|
639
|
+
SkipArrayMapArgument: 1n << 66n,
|
|
640
|
+
SkipObjectMethod: 1n << 67n,
|
|
641
|
+
SkipClassMethod: 1n << 68n,
|
|
642
|
+
SkipClassProperty: 1n << 69n
|
|
563
643
|
};
|
|
564
|
-
|
|
644
|
+
/**
|
|
645
|
+
* Default component detection hint
|
|
646
|
+
*/
|
|
647
|
+
const DEFAULT_COMPONENT_DETECTION_HINT = 0n | ComponentDetectionHint.SkipBooleanLiteral | ComponentDetectionHint.SkipEmptyArray | ComponentDetectionHint.SkipArrayMapArgument | ComponentDetectionHint.SkipNumberLiteral | ComponentDetectionHint.SkipStringLiteral | ComponentDetectionHint.SkipUndefined | ComponentDetectionHint.StrictArray | ComponentDetectionHint.StrictConditional | ComponentDetectionHint.StrictLogical;
|
|
648
|
+
|
|
649
|
+
//#endregion
|
|
650
|
+
//#region src/component/component-is.ts
|
|
651
|
+
/**
|
|
652
|
+
* Check if a node is a React class component
|
|
653
|
+
* @param node The AST node to check
|
|
654
|
+
* @returns `true` if the node is a class component, `false` otherwise
|
|
655
|
+
*/
|
|
565
656
|
function isClassComponent(node) {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
657
|
+
if ("superClass" in node && node.superClass != null) {
|
|
658
|
+
const re = /^(?:Pure)?Component$/u;
|
|
659
|
+
switch (true) {
|
|
660
|
+
case node.superClass.type === AST_NODE_TYPES.Identifier: return re.test(node.superClass.name);
|
|
661
|
+
case node.superClass.type === AST_NODE_TYPES.MemberExpression && node.superClass.property.type === AST_NODE_TYPES.Identifier: return re.test(node.superClass.property.name);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Check if a node is a React PureComponent
|
|
668
|
+
* @param node The AST node to check
|
|
669
|
+
* @returns `true` if the node is a pure component, `false` otherwise
|
|
670
|
+
*/
|
|
577
671
|
function isPureComponent(node) {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
}
|
|
587
|
-
return false;
|
|
672
|
+
if ("superClass" in node && node.superClass != null) {
|
|
673
|
+
const re = /^PureComponent$/u;
|
|
674
|
+
switch (true) {
|
|
675
|
+
case node.superClass.type === AST_NODE_TYPES.Identifier: return re.test(node.superClass.name);
|
|
676
|
+
case node.superClass.type === AST_NODE_TYPES.MemberExpression && node.superClass.property.type === AST_NODE_TYPES.Identifier: return re.test(node.superClass.property.name);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return false;
|
|
588
680
|
}
|
|
681
|
+
|
|
682
|
+
//#endregion
|
|
683
|
+
//#region src/component/component-render-method.ts
|
|
684
|
+
/**
|
|
685
|
+
* Check whether given node is a render method of a class component
|
|
686
|
+
* @example
|
|
687
|
+
* ```tsx
|
|
688
|
+
* class Component extends React.Component {
|
|
689
|
+
* renderHeader = () => <div />;
|
|
690
|
+
* renderFooter = () => <div />;
|
|
691
|
+
* }
|
|
692
|
+
* ```
|
|
693
|
+
* @param node The AST node to check
|
|
694
|
+
* @returns `true` if node is a render function, `false` if not
|
|
695
|
+
*/
|
|
589
696
|
function isRenderMethodLike(node) {
|
|
590
|
-
|
|
697
|
+
return AST.isMethodOrProperty(node) && node.key.type === AST_NODE_TYPES.Identifier && node.key.name.startsWith("render") && node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration;
|
|
591
698
|
}
|
|
592
699
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
700
|
+
//#endregion
|
|
701
|
+
//#region src/component/component-definition.ts
|
|
702
|
+
/**
|
|
703
|
+
* Function pattern matchers for different contexts
|
|
704
|
+
*/
|
|
705
|
+
const functionPatterns = {
|
|
706
|
+
classMethod: {
|
|
707
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
708
|
+
parent: AST_NODE_TYPES.MethodDefinition
|
|
709
|
+
},
|
|
710
|
+
classProperty: {
|
|
711
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
712
|
+
parent: AST_NODE_TYPES.Property
|
|
713
|
+
},
|
|
714
|
+
objectMethod: {
|
|
715
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
716
|
+
parent: {
|
|
717
|
+
type: AST_NODE_TYPES.Property,
|
|
718
|
+
parent: { type: AST_NODE_TYPES.ObjectExpression }
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
/**
|
|
723
|
+
* Check whether given node is a function of a render method of a class component
|
|
724
|
+
* @example
|
|
725
|
+
* ```tsx
|
|
726
|
+
* class Component extends React.Component {
|
|
727
|
+
* renderHeader = () => <div />;
|
|
728
|
+
* renderFooter = () => <div />;
|
|
729
|
+
* }
|
|
730
|
+
* ```
|
|
731
|
+
* @param node The AST node to check
|
|
732
|
+
* @returns `true` if node is a render function, `false` if not
|
|
733
|
+
*/
|
|
611
734
|
function isFunctionOfRenderMethod(node) {
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
735
|
+
return isRenderMethodLike(node.parent) && isClassComponent(node.parent.parent.parent);
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Checks if a function node should be excluded based on detection hints
|
|
739
|
+
* @param node The function node to check
|
|
740
|
+
* @param hint Component detection hints as bit flags
|
|
741
|
+
* @returns `true` if the function should be excluded, `false` otherwise
|
|
742
|
+
*/
|
|
743
|
+
function shouldExcludeBasedOnHint(node, hint) {
|
|
744
|
+
if (hint & ComponentDetectionHint.SkipObjectMethod && isMatching(functionPatterns.objectMethod)(node)) return true;
|
|
745
|
+
if (hint & ComponentDetectionHint.SkipClassMethod && isMatching(functionPatterns.classMethod)(node)) return true;
|
|
746
|
+
if (hint & ComponentDetectionHint.SkipClassProperty && isMatching(functionPatterns.classProperty)(node)) return true;
|
|
747
|
+
if (hint & ComponentDetectionHint.SkipArrayMapArgument && AST.isArrayMapCall(node.parent)) return true;
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Determines if a function node represents a valid React component definition
|
|
752
|
+
* @param context The rule context
|
|
753
|
+
* @param node The function node to check
|
|
754
|
+
* @param hint Component detection hints as bit flags
|
|
755
|
+
* @returns `true` if the node is a valid component definition, `false` otherwise
|
|
756
|
+
*/
|
|
757
|
+
function isComponentDefinition(context, node, hint) {
|
|
758
|
+
if (isChildrenOfCreateElement(context, node) || isFunctionOfRenderMethod(node)) return false;
|
|
759
|
+
if (shouldExcludeBasedOnHint(node, hint)) return false;
|
|
760
|
+
const significantParent = AST.findParentNode(node, AST.isOneOf([
|
|
761
|
+
AST_NODE_TYPES.JSXExpressionContainer,
|
|
762
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
763
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
764
|
+
AST_NODE_TYPES.Property,
|
|
765
|
+
AST_NODE_TYPES.ClassBody
|
|
766
|
+
]));
|
|
767
|
+
return significantParent == null || significantParent.type !== AST_NODE_TYPES.JSXExpressionContainer;
|
|
644
768
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
769
|
+
|
|
770
|
+
//#endregion
|
|
771
|
+
//#region src/utils/get-instance-id.ts
|
|
772
|
+
/**
|
|
773
|
+
* Gets the identifier node of an instance based on AST node relationships.
|
|
774
|
+
* Used for tracking where hooks or components are being assigned in the code.
|
|
775
|
+
* @param node The current AST node to evaluate
|
|
776
|
+
* @param prev The previous AST node in the traversal (used for context)
|
|
777
|
+
* @internal
|
|
778
|
+
*/
|
|
779
|
+
function getInstanceId(node, prev) {
|
|
780
|
+
switch (true) {
|
|
781
|
+
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
782
|
+
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
783
|
+
case node.type === AST_NODE_TYPES.PropertyDefinition && node.value === prev: return node.key;
|
|
784
|
+
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
785
|
+
default: return getInstanceId(node.parent, node);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
//#endregion
|
|
790
|
+
//#region src/utils/is-from-react.ts
|
|
791
|
+
/**
|
|
792
|
+
* Get the arguments of a require expression
|
|
793
|
+
* @param node The node to match
|
|
794
|
+
* @returns The require expression arguments or undefined if the node is not a require expression
|
|
795
|
+
*/
|
|
796
|
+
function getRequireExpressionArguments(node) {
|
|
797
|
+
return match(node).with({
|
|
798
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
799
|
+
arguments: P.select(),
|
|
800
|
+
callee: {
|
|
801
|
+
type: AST_NODE_TYPES.Identifier,
|
|
802
|
+
name: "require"
|
|
803
|
+
}
|
|
804
|
+
}, identity).with({
|
|
805
|
+
type: AST_NODE_TYPES.MemberExpression,
|
|
806
|
+
object: P.select()
|
|
807
|
+
}, getRequireExpressionArguments).otherwise(() => null);
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Check if an identifier name is initialized from react
|
|
811
|
+
* @param name The top-level identifier's name
|
|
812
|
+
* @param importSource The import source to check against
|
|
813
|
+
* @param initialScope Initial scope to search for the identifier
|
|
814
|
+
* @returns Whether the identifier name is initialized from react
|
|
815
|
+
* @internal
|
|
816
|
+
*/
|
|
817
|
+
function isInitializedFromReact(name, importSource, initialScope) {
|
|
818
|
+
if (name.toLowerCase() === "react") return true;
|
|
819
|
+
const latestDef = VAR.findVariable(name, initialScope)?.defs.at(-1);
|
|
820
|
+
if (latestDef == null) return false;
|
|
821
|
+
const { node, parent } = latestDef;
|
|
822
|
+
if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
|
|
823
|
+
const { init } = node;
|
|
824
|
+
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return isInitializedFromReact(init.object.name, importSource, initialScope);
|
|
825
|
+
if (init.type === AST_NODE_TYPES.Identifier) return isInitializedFromReact(init.name, importSource, initialScope);
|
|
826
|
+
const arg0 = getRequireExpressionArguments(init)?.[0];
|
|
827
|
+
if (arg0 == null || !AST.isLiteral(arg0, "string")) return false;
|
|
828
|
+
return arg0.value === importSource || arg0.value.startsWith(`${importSource}/`);
|
|
829
|
+
}
|
|
830
|
+
return parent?.type === AST_NODE_TYPES.ImportDeclaration && parent.source.value === importSource;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
//#endregion
|
|
834
|
+
//#region src/utils/is-instance-id-equal.ts
|
|
835
|
+
/** @internal */
|
|
836
|
+
function isInstanceIdEqual(context, a, b) {
|
|
837
|
+
return AST.isNodeEqual(a, b) || VAR.isNodeValueEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
|
|
648
838
|
}
|
|
839
|
+
|
|
840
|
+
//#endregion
|
|
841
|
+
//#region src/component/component-wrapper.ts
|
|
842
|
+
/**
|
|
843
|
+
* Check if the node is a call expression for a component wrapper
|
|
844
|
+
* @param context The ESLint rule context
|
|
845
|
+
* @param node The node to check
|
|
846
|
+
* @returns `true` if the node is a call expression for a component wrapper
|
|
847
|
+
*/
|
|
848
|
+
function isComponentWrapperCall(context, node) {
|
|
849
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
850
|
+
return isMemoCall(context, node) || isForwardRefCall(context, node);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Check if the node is a call expression for a component wrapper loosely
|
|
854
|
+
* @param context The ESLint rule context
|
|
855
|
+
* @param node The node to check
|
|
856
|
+
* @returns `true` if the node is a call expression for a component wrapper loosely
|
|
857
|
+
*/
|
|
649
858
|
function isComponentWrapperCallLoose(context, node) {
|
|
650
|
-
|
|
651
|
-
|
|
859
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
860
|
+
return isComponentWrapperCall(context, node) || isUseCallbackCall(node);
|
|
652
861
|
}
|
|
653
862
|
|
|
654
|
-
|
|
863
|
+
//#endregion
|
|
864
|
+
//#region src/component/component-id.ts
|
|
655
865
|
function getFunctionComponentId(context, node) {
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
PureComponent: 1n << 0n,
|
|
674
|
-
CreateElement: 1n << 1n,
|
|
675
|
-
Memo: 1n << 2n,
|
|
676
|
-
ForwardRef: 1n << 3n,
|
|
677
|
-
Async: 1n << 4n
|
|
866
|
+
const functionId = AST.getFunctionId(node);
|
|
867
|
+
if (functionId != null) return functionId;
|
|
868
|
+
const { parent } = node;
|
|
869
|
+
if (parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator && parent.parent.id.type === AST_NODE_TYPES.Identifier) return parent.parent.id;
|
|
870
|
+
if (parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent) && parent.parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent.parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator && parent.parent.parent.id.type === AST_NODE_TYPES.Identifier) return parent.parent.parent.id;
|
|
871
|
+
return unit;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
//#endregion
|
|
875
|
+
//#region src/component/component-flag.ts
|
|
876
|
+
const ComponentFlag = {
|
|
877
|
+
None: 0n,
|
|
878
|
+
PureComponent: 1n << 0n,
|
|
879
|
+
CreateElement: 1n << 1n,
|
|
880
|
+
Memo: 1n << 2n,
|
|
881
|
+
ForwardRef: 1n << 3n,
|
|
882
|
+
Async: 1n << 4n
|
|
678
883
|
};
|
|
679
884
|
|
|
680
|
-
|
|
885
|
+
//#endregion
|
|
886
|
+
//#region src/component/component-init-path.ts
|
|
681
887
|
function getComponentFlagFromInitPath(initPath) {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
if (initPath != null && AST14.hasCallInFunctionInitPath("forwardRef", initPath)) {
|
|
687
|
-
flag |= ComponentFlag.ForwardRef;
|
|
688
|
-
}
|
|
689
|
-
return flag;
|
|
888
|
+
let flag = ComponentFlag.None;
|
|
889
|
+
if (initPath != null && AST.hasCallInFunctionInitPath("memo", initPath)) flag |= ComponentFlag.Memo;
|
|
890
|
+
if (initPath != null && AST.hasCallInFunctionInitPath("forwardRef", initPath)) flag |= ComponentFlag.ForwardRef;
|
|
891
|
+
return flag;
|
|
690
892
|
}
|
|
893
|
+
|
|
894
|
+
//#endregion
|
|
895
|
+
//#region src/component/component-name.ts
|
|
691
896
|
function isComponentName(name) {
|
|
692
|
-
|
|
897
|
+
return RE_COMPONENT_NAME.test(name);
|
|
693
898
|
}
|
|
694
899
|
function isComponentNameLoose(name) {
|
|
695
|
-
|
|
900
|
+
return RE_COMPONENT_NAME_LOOSE.test(name);
|
|
696
901
|
}
|
|
697
902
|
function getComponentNameFromId(id) {
|
|
698
|
-
|
|
699
|
-
|
|
903
|
+
if (id == null) return unit;
|
|
904
|
+
return Array.isArray(id) ? id.map((n) => n.name).join(".") : id.name;
|
|
700
905
|
}
|
|
701
906
|
function hasNoneOrLooseComponentName(context, fn) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
907
|
+
const id = getFunctionComponentId(context, fn);
|
|
908
|
+
if (id == null) return true;
|
|
909
|
+
const name = Array.isArray(id) ? id.at(-1)?.name : id.name;
|
|
910
|
+
return name != null && isComponentNameLoose(name);
|
|
706
911
|
}
|
|
707
912
|
|
|
708
|
-
|
|
913
|
+
//#endregion
|
|
914
|
+
//#region src/component/component-collector.ts
|
|
915
|
+
/**
|
|
916
|
+
* Get a ctx and listeners for the rule to collect function components
|
|
917
|
+
* @param context The ESLint rule context
|
|
918
|
+
* @param options The options to use
|
|
919
|
+
* @returns The component collector
|
|
920
|
+
*/
|
|
709
921
|
function useComponentCollector(context, options = {}) {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
922
|
+
const { collectDisplayName = false, collectHookCalls = false, hint = DEFAULT_COMPONENT_DETECTION_HINT } = options;
|
|
923
|
+
const components = /* @__PURE__ */ new Map();
|
|
924
|
+
const functionEntries = [];
|
|
925
|
+
const getCurrentEntry = () => functionEntries.at(-1);
|
|
926
|
+
const onFunctionEnter = (node) => {
|
|
927
|
+
const key = getId();
|
|
928
|
+
functionEntries.push({
|
|
929
|
+
key,
|
|
930
|
+
node,
|
|
931
|
+
hookCalls: [],
|
|
932
|
+
isComponent: false
|
|
933
|
+
});
|
|
934
|
+
};
|
|
935
|
+
const onFunctionExit = () => {
|
|
936
|
+
const entry = functionEntries.at(-1);
|
|
937
|
+
if (entry == null) return;
|
|
938
|
+
if (!entry.isComponent) return functionEntries.pop();
|
|
939
|
+
const rets = AST.getNestedReturnStatements(entry.node.body);
|
|
940
|
+
for (let i = rets.length - 1; i >= 0; i--) {
|
|
941
|
+
const ret = rets[i];
|
|
942
|
+
if (ret == null) continue;
|
|
943
|
+
if (context.sourceCode.getScope(ret).block === entry.node && ret.argument != null && !isJsxLike(context.sourceCode, ret.argument, hint)) {
|
|
944
|
+
components.delete(entry.key);
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return functionEntries.pop();
|
|
949
|
+
};
|
|
950
|
+
const ctx = {
|
|
951
|
+
getAllComponents(node) {
|
|
952
|
+
return components;
|
|
953
|
+
},
|
|
954
|
+
getCurrentEntries() {
|
|
955
|
+
return [...functionEntries];
|
|
956
|
+
},
|
|
957
|
+
getCurrentEntry
|
|
958
|
+
};
|
|
959
|
+
const listeners = {
|
|
960
|
+
":function[type]": onFunctionEnter,
|
|
961
|
+
":function[type]:exit": onFunctionExit,
|
|
962
|
+
"ArrowFunctionExpression[body.type!='BlockStatement']"() {
|
|
963
|
+
const entry = getCurrentEntry();
|
|
964
|
+
if (entry == null) return;
|
|
965
|
+
const { body } = entry.node;
|
|
966
|
+
if (!(hasNoneOrLooseComponentName(context, entry.node) && isJsxLike(context.sourceCode, body, hint) && isComponentDefinition(context, entry.node, hint))) return;
|
|
967
|
+
const initPath = AST.getFunctionInitPath(entry.node);
|
|
968
|
+
const id = getFunctionComponentId(context, entry.node);
|
|
969
|
+
const name = getComponentNameFromId(id);
|
|
970
|
+
const key = getId();
|
|
971
|
+
components.set(key, {
|
|
972
|
+
id,
|
|
973
|
+
key,
|
|
974
|
+
kind: "function",
|
|
975
|
+
name,
|
|
976
|
+
node: entry.node,
|
|
977
|
+
displayName: unit,
|
|
978
|
+
flag: getComponentFlagFromInitPath(initPath),
|
|
979
|
+
hint,
|
|
980
|
+
hookCalls: entry.hookCalls,
|
|
981
|
+
initPath
|
|
982
|
+
});
|
|
983
|
+
},
|
|
984
|
+
...collectDisplayName ? { [AST.SEL_DISPLAY_NAME_ASSIGNMENT_EXPRESSION](node) {
|
|
985
|
+
const { left, right } = node;
|
|
986
|
+
if (left.type !== AST_NODE_TYPES.MemberExpression) return;
|
|
987
|
+
const componentName = left.object.type === AST_NODE_TYPES.Identifier ? left.object.name : unit;
|
|
988
|
+
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);
|
|
989
|
+
if (component == null) return;
|
|
990
|
+
component.displayName = right;
|
|
991
|
+
} } : {},
|
|
992
|
+
...collectHookCalls ? { "CallExpression[type]:exit"(node) {
|
|
993
|
+
if (!isReactHookCall(node)) return;
|
|
994
|
+
const entry = getCurrentEntry();
|
|
995
|
+
if (entry == null) return;
|
|
996
|
+
entry.hookCalls.push(node);
|
|
997
|
+
} } : {},
|
|
998
|
+
"ReturnStatement[type]"(node) {
|
|
999
|
+
const entry = getCurrentEntry();
|
|
1000
|
+
if (entry == null) return;
|
|
1001
|
+
if (!(hasNoneOrLooseComponentName(context, entry.node) && isJsxLike(context.sourceCode, node.argument, hint) && isComponentDefinition(context, entry.node, hint))) return;
|
|
1002
|
+
entry.isComponent = true;
|
|
1003
|
+
const initPath = AST.getFunctionInitPath(entry.node);
|
|
1004
|
+
const id = getFunctionComponentId(context, entry.node);
|
|
1005
|
+
const name = getComponentNameFromId(id);
|
|
1006
|
+
components.set(entry.key, {
|
|
1007
|
+
id,
|
|
1008
|
+
key: entry.key,
|
|
1009
|
+
kind: "function",
|
|
1010
|
+
name,
|
|
1011
|
+
node: entry.node,
|
|
1012
|
+
displayName: unit,
|
|
1013
|
+
flag: getComponentFlagFromInitPath(initPath),
|
|
1014
|
+
hint,
|
|
1015
|
+
hookCalls: entry.hookCalls,
|
|
1016
|
+
initPath
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
return {
|
|
1021
|
+
ctx,
|
|
1022
|
+
listeners
|
|
1023
|
+
};
|
|
812
1024
|
}
|
|
1025
|
+
|
|
1026
|
+
//#endregion
|
|
1027
|
+
//#region src/component/component-collector-legacy.ts
|
|
1028
|
+
/**
|
|
1029
|
+
* Get a ctx and listeners object for the rule to collect class components
|
|
1030
|
+
* @returns The context and listeners for the rule
|
|
1031
|
+
*/
|
|
813
1032
|
function useComponentCollectorLegacy() {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
843
|
-
);
|
|
844
|
-
};
|
|
845
|
-
const listeners = {
|
|
846
|
-
"ClassDeclaration[type]": collect,
|
|
847
|
-
"ClassExpression[type]": collect
|
|
848
|
-
};
|
|
849
|
-
return { ctx, listeners };
|
|
850
|
-
}
|
|
851
|
-
function isComponentDidCatch(node) {
|
|
852
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidCatch";
|
|
853
|
-
}
|
|
854
|
-
function isComponentDidMount(node) {
|
|
855
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidMount";
|
|
856
|
-
}
|
|
857
|
-
function isComponentDidUpdate(node) {
|
|
858
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidUpdate";
|
|
859
|
-
}
|
|
860
|
-
function isComponentWillMount(node) {
|
|
861
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillMount";
|
|
862
|
-
}
|
|
863
|
-
function isComponentWillReceiveProps(node) {
|
|
864
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillReceiveProps";
|
|
865
|
-
}
|
|
866
|
-
function isComponentWillUnmount(node) {
|
|
867
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillUnmount";
|
|
868
|
-
}
|
|
869
|
-
function isComponentWillUpdate(node) {
|
|
870
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillUpdate";
|
|
871
|
-
}
|
|
872
|
-
function isGetChildContext(node) {
|
|
873
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getChildContext";
|
|
874
|
-
}
|
|
875
|
-
function isGetDefaultProps(node) {
|
|
876
|
-
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDefaultProps";
|
|
877
|
-
}
|
|
878
|
-
function isGetInitialState(node) {
|
|
879
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getInitialState";
|
|
880
|
-
}
|
|
881
|
-
function isGetSnapshotBeforeUpdate(node) {
|
|
882
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getSnapshotBeforeUpdate";
|
|
883
|
-
}
|
|
884
|
-
function isShouldComponentUpdate(node) {
|
|
885
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "shouldComponentUpdate";
|
|
886
|
-
}
|
|
887
|
-
function isUnsafeComponentWillMount(node) {
|
|
888
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillMount";
|
|
889
|
-
}
|
|
890
|
-
function isUnsafeComponentWillReceiveProps(node) {
|
|
891
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillReceiveProps";
|
|
892
|
-
}
|
|
893
|
-
function isUnsafeComponentWillUpdate(node) {
|
|
894
|
-
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillUpdate";
|
|
895
|
-
}
|
|
896
|
-
function isGetDerivedStateFromProps(node) {
|
|
897
|
-
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromProps";
|
|
898
|
-
}
|
|
899
|
-
function isGetDerivedStateFromError(node) {
|
|
900
|
-
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromError";
|
|
901
|
-
}
|
|
902
|
-
var ComponentPhaseRelevance = birecord({
|
|
903
|
-
mount: "unmount",
|
|
904
|
-
setup: "cleanup"
|
|
905
|
-
});
|
|
906
|
-
var isInversePhase = dual(2, (a, b) => ComponentPhaseRelevance.get(a) === b);
|
|
907
|
-
function isRenderLike(node) {
|
|
908
|
-
return AST14.isMethodOrProperty(node) && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "render" && node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration;
|
|
909
|
-
}
|
|
910
|
-
function isFunctionOfRender(node) {
|
|
911
|
-
if (!isRenderLike(node.parent)) {
|
|
912
|
-
return false;
|
|
913
|
-
}
|
|
914
|
-
return isClassComponent(node.parent.parent.parent);
|
|
1033
|
+
const components = /* @__PURE__ */ new Map();
|
|
1034
|
+
const ctx = { getAllComponents(node) {
|
|
1035
|
+
return components;
|
|
1036
|
+
} };
|
|
1037
|
+
const collect = (node) => {
|
|
1038
|
+
if (!isClassComponent(node)) return;
|
|
1039
|
+
const id = AST.getClassId(node);
|
|
1040
|
+
const key = getId();
|
|
1041
|
+
const flag = isPureComponent(node) ? ComponentFlag.PureComponent : ComponentFlag.None;
|
|
1042
|
+
components.set(key, {
|
|
1043
|
+
id,
|
|
1044
|
+
key,
|
|
1045
|
+
kind: "class",
|
|
1046
|
+
name: id?.name,
|
|
1047
|
+
node,
|
|
1048
|
+
displayName: unit,
|
|
1049
|
+
flag,
|
|
1050
|
+
hint: 0n,
|
|
1051
|
+
methods: []
|
|
1052
|
+
});
|
|
1053
|
+
};
|
|
1054
|
+
return {
|
|
1055
|
+
ctx,
|
|
1056
|
+
listeners: {
|
|
1057
|
+
"ClassDeclaration[type]": collect,
|
|
1058
|
+
"ClassExpression[type]": collect
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
915
1061
|
}
|
|
1062
|
+
|
|
1063
|
+
//#endregion
|
|
1064
|
+
//#region src/component/component-method.ts
|
|
1065
|
+
/**
|
|
1066
|
+
* Create a lifecycle method checker function
|
|
1067
|
+
* @param methodName The lifecycle method name
|
|
1068
|
+
* @param isStatic Whether the method is static
|
|
1069
|
+
*/
|
|
1070
|
+
function createLifecycleChecker(methodName, isStatic) {
|
|
1071
|
+
return function(node) {
|
|
1072
|
+
return AST.isMethodOrProperty(node) && node.static === isStatic && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === methodName;
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
const isRender = createLifecycleChecker("render", false);
|
|
1076
|
+
const isComponentDidCatch = createLifecycleChecker("componentDidCatch", false);
|
|
1077
|
+
const isComponentDidMount = createLifecycleChecker("componentDidMount", false);
|
|
1078
|
+
const isComponentDidUpdate = createLifecycleChecker("componentDidUpdate", false);
|
|
1079
|
+
const isComponentWillMount = createLifecycleChecker("componentWillMount", false);
|
|
1080
|
+
const isComponentWillReceiveProps = createLifecycleChecker("componentWillReceiveProps", false);
|
|
1081
|
+
const isComponentWillUnmount = createLifecycleChecker("componentWillUnmount", false);
|
|
1082
|
+
const isComponentWillUpdate = createLifecycleChecker("componentWillUpdate", false);
|
|
1083
|
+
const isGetChildContext = createLifecycleChecker("getChildContext", false);
|
|
1084
|
+
const isGetInitialState = createLifecycleChecker("getInitialState", false);
|
|
1085
|
+
const isGetSnapshotBeforeUpdate = createLifecycleChecker("getSnapshotBeforeUpdate", false);
|
|
1086
|
+
const isShouldComponentUpdate = createLifecycleChecker("shouldComponentUpdate", false);
|
|
1087
|
+
const isUnsafeComponentWillMount = createLifecycleChecker("UNSAFE_componentWillMount", false);
|
|
1088
|
+
const isUnsafeComponentWillReceiveProps = createLifecycleChecker("UNSAFE_componentWillReceiveProps", false);
|
|
1089
|
+
const isUnsafeComponentWillUpdate = createLifecycleChecker("UNSAFE_componentWillUpdate", false);
|
|
1090
|
+
const isGetDefaultProps = createLifecycleChecker("getDefaultProps", true);
|
|
1091
|
+
const isGetDerivedStateFromProps = createLifecycleChecker("getDerivedStateFromProps", true);
|
|
1092
|
+
const isGetDerivedStateFromError = createLifecycleChecker("getDerivedStateFromError", true);
|
|
1093
|
+
|
|
1094
|
+
//#endregion
|
|
1095
|
+
//#region src/component/component-phase.ts
|
|
1096
|
+
const ComponentPhaseRelevance = birecord({
|
|
1097
|
+
mount: "unmount",
|
|
1098
|
+
setup: "cleanup"
|
|
1099
|
+
});
|
|
1100
|
+
const isInversePhase = dual(2, (a, b) => ComponentPhaseRelevance.get(a) === b);
|
|
1101
|
+
|
|
1102
|
+
//#endregion
|
|
1103
|
+
//#region src/component/component-render-prop.ts
|
|
1104
|
+
/**
|
|
1105
|
+
* Unsafe check whether given node is a render function
|
|
1106
|
+
* ```tsx
|
|
1107
|
+
* const renderRow = () => <div />
|
|
1108
|
+
* ` ^^^^^^^^^^^^`
|
|
1109
|
+
* _ = <Component renderRow={() => <div />} />
|
|
1110
|
+
* ` ^^^^^^^^^^^^^ `
|
|
1111
|
+
* ```
|
|
1112
|
+
* @param context The rule context
|
|
1113
|
+
* @param node The AST node to check
|
|
1114
|
+
* @returns `true` if node is a render function, `false` if not
|
|
1115
|
+
*/
|
|
916
1116
|
function isRenderFunctionLoose(context, node) {
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
1117
|
+
const { body, parent } = node;
|
|
1118
|
+
if (AST.getFunctionId(node)?.name.startsWith("render")) return parent.type === AST_NODE_TYPES.JSXExpressionContainer && parent.parent.type === AST_NODE_TYPES.JSXAttribute && parent.parent.name.type === AST_NODE_TYPES.JSXIdentifier && parent.parent.name.name.startsWith("render");
|
|
1119
|
+
return isJsxLike(context.sourceCode, body, JSXDetectionHint.SkipNullLiteral | JSXDetectionHint.SkipUndefined | JSXDetectionHint.StrictLogical | JSXDetectionHint.StrictConditional);
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Unsafe check whether given JSXAttribute is a render prop
|
|
1123
|
+
* ```tsx
|
|
1124
|
+
* _ = <Component renderRow={() => <div />} />
|
|
1125
|
+
* ` ^^^^^^^^^^^^^^^^^^^^^^^^^ `
|
|
1126
|
+
* ```
|
|
1127
|
+
* @param context The rule context
|
|
1128
|
+
* @param node The AST node to check
|
|
1129
|
+
* @returns `true` if node is a render prop, `false` if not
|
|
1130
|
+
*/
|
|
927
1131
|
function isRenderPropLoose(context, node) {
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
1132
|
+
if (node.name.type !== AST_NODE_TYPES.JSXIdentifier) return false;
|
|
1133
|
+
return node.name.name.startsWith("render") && node.value?.type === AST_NODE_TYPES.JSXExpressionContainer && AST.isFunction(node.value.expression) && isRenderFunctionLoose(context, node.value.expression);
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Unsafe check whether given node is declared directly inside a render property
|
|
1137
|
+
* ```tsx
|
|
1138
|
+
* const rows = { render: () => <div /> }
|
|
1139
|
+
* ` ^^^^^^^^^^^^^ `
|
|
1140
|
+
* _ = <Component rows={ [{ render: () => <div /> }] } />
|
|
1141
|
+
* ` ^^^^^^^^^^^^^ `
|
|
1142
|
+
* ```
|
|
1143
|
+
* @internal
|
|
1144
|
+
* @param node The AST node to check
|
|
1145
|
+
* @returns `true` if component is declared inside a render property, `false` if not
|
|
1146
|
+
*/
|
|
933
1147
|
function isDirectValueOfRenderPropertyLoose(node) {
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
}
|
|
1148
|
+
const matching = (node$1) => {
|
|
1149
|
+
return node$1.type === AST_NODE_TYPES.Property && node$1.key.type === AST_NODE_TYPES.Identifier && node$1.key.name.startsWith("render");
|
|
1150
|
+
};
|
|
1151
|
+
return matching(node) || node.parent != null && matching(node.parent);
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Unsafe check whether given node is declared inside a render prop
|
|
1155
|
+
* ```tsx
|
|
1156
|
+
* _ = <Component renderRow={"node"} />
|
|
1157
|
+
* ` ^^^^^^ `
|
|
1158
|
+
* _ = <Component rows={ [{ render: "node" }] } />
|
|
1159
|
+
* ` ^^^^^^ `
|
|
1160
|
+
* ```
|
|
1161
|
+
* @param node The AST node to check
|
|
1162
|
+
* @returns `true` if component is declared inside a render prop, `false` if not
|
|
1163
|
+
*/
|
|
939
1164
|
function isDeclaredInRenderPropLoose(node) {
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
if (parent?.type !== AST_NODE_TYPES.JSXAttribute) {
|
|
945
|
-
return false;
|
|
946
|
-
}
|
|
947
|
-
return parent.name.type === AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
1165
|
+
if (isDirectValueOfRenderPropertyLoose(node)) return true;
|
|
1166
|
+
const parent = AST.findParentNode(node, AST.is(AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
1167
|
+
if (parent?.type !== AST_NODE_TYPES.JSXAttribute) return false;
|
|
1168
|
+
return parent.name.type === AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
948
1169
|
}
|
|
1170
|
+
|
|
1171
|
+
//#endregion
|
|
1172
|
+
//#region src/component/component-state.ts
|
|
949
1173
|
function isThisSetState(node) {
|
|
950
|
-
|
|
951
|
-
|
|
1174
|
+
const { callee } = node;
|
|
1175
|
+
return callee.type === AST_NODE_TYPES.MemberExpression && AST.isThisExpression(callee.object) && callee.property.type === AST_NODE_TYPES.Identifier && callee.property.name === "setState";
|
|
952
1176
|
}
|
|
953
1177
|
function isAssignmentToThisState(node) {
|
|
954
|
-
|
|
955
|
-
|
|
1178
|
+
const { left } = node;
|
|
1179
|
+
return left.type === AST_NODE_TYPES.MemberExpression && AST.isThisExpression(left.object) && AST.getPropertyName(left.property) === "state";
|
|
956
1180
|
}
|
|
957
1181
|
|
|
958
|
-
|
|
1182
|
+
//#endregion
|
|
1183
|
+
export { ComponentDetectionHint, ComponentFlag, ComponentPhaseRelevance, DEFAULT_COMPONENT_DETECTION_HINT, DEFAULT_JSX_DETECTION_HINT, JSXDetectionHint, JsxEmit, REACT_BUILTIN_HOOK_NAMES, findParentAttribute, getAttribute, getAttributeName, getComponentFlagFromInitPath, getComponentNameFromId, getElementType, getFunctionComponentId, getInstanceId, getJsxConfigFromAnnotation, getJsxConfigFromContext, hasAnyAttribute, hasAttribute, hasEveryAttribute, hasNoneOrLooseComponentName, isAssignmentToThisState, isCaptureOwnerStack, isCaptureOwnerStackCall, isChildrenCount, isChildrenCountCall, isChildrenForEach, isChildrenForEachCall, isChildrenMap, isChildrenMapCall, isChildrenOfCreateElement, isChildrenOnly, isChildrenOnlyCall, isChildrenToArray, isChildrenToArrayCall, isClassComponent, isCloneElement, isCloneElementCall, isComponentDefinition, isComponentDidCatch, isComponentDidMount, isComponentDidUpdate, isComponentName, isComponentNameLoose, isComponentWillMount, isComponentWillReceiveProps, isComponentWillUnmount, isComponentWillUpdate, isComponentWrapperCall, isComponentWrapperCallLoose, isCreateContext, isCreateContextCall, isCreateElement, isCreateElementCall, isCreateRef, isCreateRefCall, isDeclaredInRenderPropLoose, isDirectValueOfRenderPropertyLoose, isForwardRef, isForwardRefCall, isFragmentElement, isFunctionOfRenderMethod, isGetChildContext, isGetDefaultProps, isGetDerivedStateFromError, isGetDerivedStateFromProps, isGetInitialState, isGetSnapshotBeforeUpdate, isHostElement, isInitializedFromReact, isInstanceIdEqual, isInversePhase, isJsxLike, isJsxText, isLazy, isLazyCall, isMemo, isMemoCall, isPureComponent, isReactAPI, isReactAPICall, isReactHook, isReactHookCall, isReactHookCallWithName, isReactHookCallWithNameAlias, isReactHookId, isReactHookName, isRender, isRenderFunctionLoose, isRenderMethodLike, isRenderPropLoose, isShouldComponentUpdate, isThisSetState, isUnsafeComponentWillMount, isUnsafeComponentWillReceiveProps, isUnsafeComponentWillUpdate, isUseActionStateCall, isUseCall, isUseCallbackCall, isUseContextCall, isUseDebugValueCall, isUseDeferredValueCall, isUseEffectCall, isUseEffectLikeCall, isUseFormStatusCall, isUseIdCall, isUseImperativeHandleCall, isUseInsertionEffectCall, isUseLayoutEffectCall, isUseMemoCall, isUseOptimisticCall, isUseReducerCall, isUseRefCall, isUseStateCall, isUseSyncExternalStoreCall, isUseTransitionCall, resolveAttributeValue, stringifyJsx, useComponentCollector, useComponentCollectorLegacy, useHookCollector };
|