@eslint-react/core 2.0.0-next.9 → 2.0.0

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