@eslint-react/core 1.23.3-next.8 → 1.24.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +48 -46
- package/dist/index.d.ts +48 -46
- package/dist/index.js +182 -205
- package/dist/index.mjs +136 -159
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -3,11 +3,9 @@
|
|
|
3
3
|
var AST8 = require('@eslint-react/ast');
|
|
4
4
|
var eff = require('@eslint-react/eff');
|
|
5
5
|
var JSX4 = require('@eslint-react/jsx');
|
|
6
|
-
var types
|
|
7
|
-
var tsPattern = require('ts-pattern');
|
|
8
|
-
var shared = require('@eslint-react/shared');
|
|
6
|
+
var types = require('@typescript-eslint/types');
|
|
9
7
|
var VAR = require('@eslint-react/var');
|
|
10
|
-
var
|
|
8
|
+
var shared = require('@eslint-react/shared');
|
|
11
9
|
|
|
12
10
|
function _interopNamespace(e) {
|
|
13
11
|
if (e && e.__esModule) return e;
|
|
@@ -32,23 +30,24 @@ var JSX4__namespace = /*#__PURE__*/_interopNamespace(JSX4);
|
|
|
32
30
|
var VAR__namespace = /*#__PURE__*/_interopNamespace(VAR);
|
|
33
31
|
|
|
34
32
|
// src/component/component-collector.ts
|
|
35
|
-
function
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
eff.O.fromNullable(polymorphicPropName),
|
|
47
|
-
eff.O.flatMap(JSX4__namespace.findPropInAttributes(node.attributes, context.sourceCode.getScope(node))),
|
|
48
|
-
eff.O.flatMap((attr) => JSX4__namespace.getPropValue(attr, context.sourceCode.getScope(attr))),
|
|
49
|
-
eff.O.filter(eff.isString),
|
|
50
|
-
eff.O.getOrElse(() => rawElementName)
|
|
33
|
+
function getElementNameAndRepresentName(node, context, polymorphicPropName, additionalComponents = []) {
|
|
34
|
+
const name = JSX4__namespace.getElementName(node);
|
|
35
|
+
if (name === name.toLowerCase()) return [name, name];
|
|
36
|
+
const component = additionalComponents.findLast((c) => c.name === name || c.re.test(name));
|
|
37
|
+
if (component != null) return [name, component.as];
|
|
38
|
+
if (polymorphicPropName == null) return [name, name];
|
|
39
|
+
const initialScope = context.sourceCode.getScope(node);
|
|
40
|
+
const polymorphicProp = JSX4__namespace.findPropInAttributes(
|
|
41
|
+
polymorphicPropName,
|
|
42
|
+
initialScope,
|
|
43
|
+
node.attributes
|
|
51
44
|
);
|
|
45
|
+
if (polymorphicProp == null) return [name, name];
|
|
46
|
+
const polymorphicName = VAR__namespace.toResolved(JSX4__namespace.getPropValue(polymorphicProp, initialScope)).value;
|
|
47
|
+
if (typeof polymorphicName === "string") {
|
|
48
|
+
return [name, polymorphicName];
|
|
49
|
+
}
|
|
50
|
+
return [name, name];
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
// src/utils/get-id.ts
|
|
@@ -67,14 +66,14 @@ function isFromReact(name) {
|
|
|
67
66
|
return (node, context) => {
|
|
68
67
|
const settings = shared.unsafeDecodeSettings(context.settings);
|
|
69
68
|
if (!settings.strictImportCheck) {
|
|
70
|
-
if (node.type === types
|
|
71
|
-
return node.object.type === types
|
|
69
|
+
if (node.type === types.AST_NODE_TYPES.MemberExpression) {
|
|
70
|
+
return node.object.type === types.AST_NODE_TYPES.Identifier && node.property.type === types.AST_NODE_TYPES.Identifier && node.property.name === name;
|
|
72
71
|
}
|
|
73
72
|
return node.name === name;
|
|
74
73
|
}
|
|
75
74
|
const initialScope = context.sourceCode.getScope(node);
|
|
76
|
-
if (node.type === types
|
|
77
|
-
return node.object.type === types
|
|
75
|
+
if (node.type === types.AST_NODE_TYPES.MemberExpression) {
|
|
76
|
+
return node.object.type === types.AST_NODE_TYPES.Identifier && node.property.type === types.AST_NODE_TYPES.Identifier && node.property.name === name && isInitializedFromReact(node.object.name, initialScope, settings.importSource);
|
|
78
77
|
}
|
|
79
78
|
if (node.name === name) {
|
|
80
79
|
return isInitializedFromReact(name, initialScope, settings.importSource);
|
|
@@ -86,47 +85,47 @@ function isFromReactMember(memberName, name) {
|
|
|
86
85
|
return (node, context) => {
|
|
87
86
|
const settings = shared.unsafeDecodeSettings(context.settings);
|
|
88
87
|
if (!settings.strictImportCheck) {
|
|
89
|
-
if (node.property.type !== types
|
|
88
|
+
if (node.property.type !== types.AST_NODE_TYPES.Identifier || node.property.name !== name) {
|
|
90
89
|
return false;
|
|
91
90
|
}
|
|
92
|
-
if (node.object.type === types
|
|
91
|
+
if (node.object.type === types.AST_NODE_TYPES.Identifier && node.object.name === memberName) {
|
|
93
92
|
return true;
|
|
94
93
|
}
|
|
95
|
-
if (node.object.type === types
|
|
94
|
+
if (node.object.type === types.AST_NODE_TYPES.MemberExpression && node.object.object.type === types.AST_NODE_TYPES.Identifier && node.object.property.type === types.AST_NODE_TYPES.Identifier) {
|
|
96
95
|
return node.object.property.name === memberName;
|
|
97
96
|
}
|
|
98
97
|
return false;
|
|
99
98
|
}
|
|
100
99
|
const initialScope = context.sourceCode.getScope(node);
|
|
101
|
-
if (node.property.type !== types
|
|
100
|
+
if (node.property.type !== types.AST_NODE_TYPES.Identifier || node.property.name !== name) {
|
|
102
101
|
return false;
|
|
103
102
|
}
|
|
104
|
-
if (node.object.type === types
|
|
103
|
+
if (node.object.type === types.AST_NODE_TYPES.Identifier && node.object.name === memberName) {
|
|
105
104
|
return isInitializedFromReact(node.object.name, initialScope, settings.importSource);
|
|
106
105
|
}
|
|
107
|
-
if (node.object.type === types
|
|
106
|
+
if (node.object.type === types.AST_NODE_TYPES.MemberExpression && node.object.object.type === types.AST_NODE_TYPES.Identifier && isInitializedFromReact(node.object.object.name, initialScope, settings.importSource) && node.object.property.type === types.AST_NODE_TYPES.Identifier) {
|
|
108
107
|
return node.object.property.name === memberName;
|
|
109
108
|
}
|
|
110
109
|
return false;
|
|
111
110
|
};
|
|
112
111
|
}
|
|
113
112
|
function isCallFromReact(name) {
|
|
114
|
-
return eff.
|
|
115
|
-
if (node.type !== types
|
|
113
|
+
return eff.dual(2, (node, context) => {
|
|
114
|
+
if (node.type !== types.AST_NODE_TYPES.CallExpression) {
|
|
116
115
|
return false;
|
|
117
116
|
}
|
|
118
|
-
if (!AST8__namespace.isOneOf([types
|
|
117
|
+
if (!AST8__namespace.isOneOf([types.AST_NODE_TYPES.Identifier, types.AST_NODE_TYPES.MemberExpression])(node.callee)) {
|
|
119
118
|
return false;
|
|
120
119
|
}
|
|
121
120
|
return isFromReact(name)(node.callee, context);
|
|
122
121
|
});
|
|
123
122
|
}
|
|
124
123
|
function isCallFromReactMember(pragmaMemberName, name) {
|
|
125
|
-
return eff.
|
|
126
|
-
if (node.type !== types
|
|
124
|
+
return eff.dual(2, (node, context) => {
|
|
125
|
+
if (node.type !== types.AST_NODE_TYPES.CallExpression) {
|
|
127
126
|
return false;
|
|
128
127
|
}
|
|
129
|
-
if (!AST8__namespace.is(types
|
|
128
|
+
if (!AST8__namespace.is(types.AST_NODE_TYPES.MemberExpression)(node.callee)) {
|
|
130
129
|
return false;
|
|
131
130
|
}
|
|
132
131
|
return isFromReactMember(pragmaMemberName, name)(node.callee, context);
|
|
@@ -135,10 +134,10 @@ function isCallFromReactMember(pragmaMemberName, name) {
|
|
|
135
134
|
|
|
136
135
|
// src/utils/is-react-api.ts
|
|
137
136
|
function isReactAPIWithName(name, member) {
|
|
138
|
-
return member
|
|
137
|
+
return member != null ? isFromReactMember(name, member) : isFromReact(name);
|
|
139
138
|
}
|
|
140
139
|
function isReactAPICallWithName(name, member) {
|
|
141
|
-
return member
|
|
140
|
+
return member != null ? isCallFromReactMember(name, member) : isCallFromReact(name);
|
|
142
141
|
}
|
|
143
142
|
var isChildrenCount = isReactAPIWithName("Children", "count");
|
|
144
143
|
var isChildrenForEach = isReactAPIWithName("Children", "forEach");
|
|
@@ -165,23 +164,17 @@ var isMemoCall = isReactAPICallWithName("memo");
|
|
|
165
164
|
|
|
166
165
|
// src/element/hierarchy.ts
|
|
167
166
|
function isInsideCreateElementProps(node, context) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
eff.O.exists(({ arg1, prop }) => prop === arg1)
|
|
174
|
-
);
|
|
167
|
+
const call = AST8__namespace.findParentNodeGuard(node, isCreateElementCall(context));
|
|
168
|
+
if (call == null) return false;
|
|
169
|
+
const prop = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.ObjectExpression));
|
|
170
|
+
if (prop == null) return false;
|
|
171
|
+
return prop === call.arguments[1];
|
|
175
172
|
}
|
|
176
173
|
function isChildrenOfCreateElement(node, context) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
eff.O.exists(
|
|
182
|
-
(n) => n.arguments.slice(2).some((arg) => arg === node)
|
|
183
|
-
)
|
|
184
|
-
);
|
|
174
|
+
const parent = node.parent;
|
|
175
|
+
if (parent == null || parent.type !== types.AST_NODE_TYPES.CallExpression) return false;
|
|
176
|
+
if (!isCreateElementCall(parent, context)) return false;
|
|
177
|
+
return parent.arguments.slice(2).some((arg) => arg === node);
|
|
185
178
|
}
|
|
186
179
|
|
|
187
180
|
// src/hook/hook-name.ts
|
|
@@ -192,32 +185,32 @@ function isReactHookName(name) {
|
|
|
192
185
|
|
|
193
186
|
// src/hook/is.ts
|
|
194
187
|
function isReactHook(node) {
|
|
195
|
-
return eff.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
eff.O.exists(isReactHookName)
|
|
199
|
-
);
|
|
188
|
+
if (node == null) return eff._;
|
|
189
|
+
const id2 = AST8__namespace.getFunctionIdentifier(node);
|
|
190
|
+
return id2?.name != null && isReactHookName(id2.name);
|
|
200
191
|
}
|
|
201
192
|
function isReactHookCall(node) {
|
|
202
|
-
if (node
|
|
193
|
+
if (node == null) return false;
|
|
194
|
+
if (node.type !== types.AST_NODE_TYPES.CallExpression) {
|
|
203
195
|
return false;
|
|
204
196
|
}
|
|
205
|
-
if (node.callee.type === types
|
|
197
|
+
if (node.callee.type === types.AST_NODE_TYPES.Identifier) {
|
|
206
198
|
return isReactHookName(node.callee.name);
|
|
207
199
|
}
|
|
208
|
-
if (node.callee.type === types
|
|
209
|
-
return node.callee.property.type === types
|
|
200
|
+
if (node.callee.type === types.AST_NODE_TYPES.MemberExpression) {
|
|
201
|
+
return node.callee.property.type === types.AST_NODE_TYPES.Identifier && isReactHookName(node.callee.property.name);
|
|
210
202
|
}
|
|
211
203
|
return false;
|
|
212
204
|
}
|
|
213
205
|
function isReactHookCallWithName(node, context) {
|
|
206
|
+
if (node == null) return eff.returnFalse;
|
|
214
207
|
const settings = shared.unsafeDecodeSettings(context.settings);
|
|
215
208
|
return (name) => {
|
|
216
209
|
const initialScope = context.sourceCode.getScope(node);
|
|
217
210
|
switch (true) {
|
|
218
|
-
case (node.callee.type === types
|
|
211
|
+
case (node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
219
212
|
return !settings.strictImportCheck || isInitializedFromReact(name, initialScope, settings.importSource);
|
|
220
|
-
case (node.callee.type === types
|
|
213
|
+
case (node.callee.type === types.AST_NODE_TYPES.MemberExpression && node.callee.property.type === types.AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
221
214
|
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name, initialScope, settings.importSource);
|
|
222
215
|
default:
|
|
223
216
|
return false;
|
|
@@ -225,12 +218,13 @@ function isReactHookCallWithName(node, context) {
|
|
|
225
218
|
};
|
|
226
219
|
}
|
|
227
220
|
function isReactHookCallWithNameLoose(node) {
|
|
221
|
+
if (node == null) return eff.returnFalse;
|
|
228
222
|
return (name) => {
|
|
229
223
|
switch (node.callee.type) {
|
|
230
|
-
case types
|
|
224
|
+
case types.AST_NODE_TYPES.Identifier:
|
|
231
225
|
return node.callee.name === name;
|
|
232
|
-
case types
|
|
233
|
-
return node.callee.property.type === types
|
|
226
|
+
case types.AST_NODE_TYPES.MemberExpression:
|
|
227
|
+
return node.callee.property.type === types.AST_NODE_TYPES.Identifier && node.callee.property.name === name;
|
|
234
228
|
default:
|
|
235
229
|
return false;
|
|
236
230
|
}
|
|
@@ -241,9 +235,9 @@ function isReactHookCallWithNameAlias(name, context, alias) {
|
|
|
241
235
|
return (node) => {
|
|
242
236
|
const initialScope = context.sourceCode.getScope(node);
|
|
243
237
|
switch (true) {
|
|
244
|
-
case (node.callee.type === types
|
|
238
|
+
case (node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
245
239
|
return !settings.strictImportCheck || isInitializedFromReact(name, initialScope, settings.importSource);
|
|
246
|
-
case (node.callee.type === types
|
|
240
|
+
case (node.callee.type === types.AST_NODE_TYPES.MemberExpression && node.callee.property.type === types.AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
247
241
|
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name, initialScope, settings.importSource);
|
|
248
242
|
default:
|
|
249
243
|
return alias.some(isReactHookCallWithNameLoose(node));
|
|
@@ -251,47 +245,48 @@ function isReactHookCallWithNameAlias(name, context, alias) {
|
|
|
251
245
|
};
|
|
252
246
|
}
|
|
253
247
|
function isUseEffectCallLoose(node) {
|
|
254
|
-
if (node
|
|
248
|
+
if (node == null) return false;
|
|
249
|
+
if (node.type !== types.AST_NODE_TYPES.CallExpression) {
|
|
255
250
|
return false;
|
|
256
251
|
}
|
|
257
252
|
switch (node.callee.type) {
|
|
258
|
-
case types
|
|
253
|
+
case types.AST_NODE_TYPES.Identifier:
|
|
259
254
|
return /^use\w*Effect$/u.test(node.callee.name);
|
|
260
|
-
case types
|
|
261
|
-
return node.callee.property.type === types
|
|
255
|
+
case types.AST_NODE_TYPES.MemberExpression:
|
|
256
|
+
return node.callee.property.type === types.AST_NODE_TYPES.Identifier && /^use\w*Effect$/u.test(node.callee.property.name);
|
|
262
257
|
default:
|
|
263
258
|
return false;
|
|
264
259
|
}
|
|
265
260
|
}
|
|
266
|
-
var isUseCallbackCall = eff.
|
|
267
|
-
var isUseContextCall = eff.
|
|
268
|
-
var isUseDebugValueCall = eff.
|
|
269
|
-
var isUseDeferredValueCall = eff.
|
|
270
|
-
var isUseEffectCall = eff.
|
|
271
|
-
var isUseIdCall = eff.
|
|
272
|
-
var isUseImperativeHandleCall = eff.
|
|
273
|
-
var isUseInsertionEffectCall = eff.
|
|
274
|
-
var isUseLayoutEffectCall = eff.
|
|
275
|
-
var isUseMemoCall = eff.
|
|
276
|
-
var isUseReducerCall = eff.
|
|
277
|
-
var isUseRefCall = eff.
|
|
278
|
-
var isUseStateCall = eff.
|
|
279
|
-
var isUseSyncExternalStoreCall = eff.
|
|
280
|
-
var isUseTransitionCall = eff.
|
|
261
|
+
var isUseCallbackCall = eff.flip(isReactHookCallWithName)("useCallback");
|
|
262
|
+
var isUseContextCall = eff.flip(isReactHookCallWithName)("useContext");
|
|
263
|
+
var isUseDebugValueCall = eff.flip(isReactHookCallWithName)("useDebugValue");
|
|
264
|
+
var isUseDeferredValueCall = eff.flip(isReactHookCallWithName)("useDeferredValue");
|
|
265
|
+
var isUseEffectCall = eff.flip(isReactHookCallWithName)("useEffect");
|
|
266
|
+
var isUseIdCall = eff.flip(isReactHookCallWithName)("useId");
|
|
267
|
+
var isUseImperativeHandleCall = eff.flip(isReactHookCallWithName)("useImperativeHandle");
|
|
268
|
+
var isUseInsertionEffectCall = eff.flip(isReactHookCallWithName)("useInsertionEffect");
|
|
269
|
+
var isUseLayoutEffectCall = eff.flip(isReactHookCallWithName)("useLayoutEffect");
|
|
270
|
+
var isUseMemoCall = eff.flip(isReactHookCallWithName)("useMemo");
|
|
271
|
+
var isUseReducerCall = eff.flip(isReactHookCallWithName)("useReducer");
|
|
272
|
+
var isUseRefCall = eff.flip(isReactHookCallWithName)("useRef");
|
|
273
|
+
var isUseStateCall = eff.flip(isReactHookCallWithName)("useState");
|
|
274
|
+
var isUseSyncExternalStoreCall = eff.flip(isReactHookCallWithName)("useSyncExternalStore");
|
|
275
|
+
var isUseTransitionCall = eff.flip(isReactHookCallWithName)("useTransition");
|
|
281
276
|
|
|
282
277
|
// src/hook/hierarchy.ts
|
|
283
278
|
function isInsideReactHook(node) {
|
|
284
|
-
return
|
|
279
|
+
return isReactHook(AST8__namespace.findParentNodeGuard(node, AST8__namespace.isFunction));
|
|
285
280
|
}
|
|
286
281
|
function useHookCollector() {
|
|
287
282
|
const hooks = /* @__PURE__ */ new Map();
|
|
288
|
-
const
|
|
283
|
+
const fEntries = [];
|
|
289
284
|
const onFunctionEnter = (node) => {
|
|
290
285
|
const id2 = AST8__namespace.getFunctionIdentifier(node);
|
|
291
|
-
const name =
|
|
292
|
-
if (
|
|
286
|
+
const name = id2?.name;
|
|
287
|
+
if (name != null && isReactHookName(name)) {
|
|
293
288
|
const key = getId();
|
|
294
|
-
|
|
289
|
+
fEntries.push({ key, node });
|
|
295
290
|
hooks.set(key, {
|
|
296
291
|
_: key,
|
|
297
292
|
id: id2,
|
|
@@ -304,13 +299,14 @@ function useHookCollector() {
|
|
|
304
299
|
});
|
|
305
300
|
return;
|
|
306
301
|
}
|
|
307
|
-
|
|
302
|
+
fEntries.push({ key: eff._, node });
|
|
308
303
|
};
|
|
309
304
|
const onFunctionExit = () => {
|
|
310
|
-
|
|
305
|
+
fEntries.pop();
|
|
311
306
|
};
|
|
312
307
|
const ctx = {
|
|
313
|
-
|
|
308
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
309
|
+
getAllHooks(node) {
|
|
314
310
|
return hooks;
|
|
315
311
|
},
|
|
316
312
|
getCurrentHooks() {
|
|
@@ -324,24 +320,21 @@ function useHookCollector() {
|
|
|
324
320
|
if (!isReactHookCall(node)) {
|
|
325
321
|
return;
|
|
326
322
|
}
|
|
327
|
-
const
|
|
328
|
-
if (
|
|
323
|
+
const fEntry = fEntries.at(-1);
|
|
324
|
+
if (fEntry?.key == null) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const hook = hooks.get(fEntry.key);
|
|
328
|
+
if (hook == null) {
|
|
329
329
|
return;
|
|
330
330
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
hookCalls: [
|
|
339
|
-
...hook.hookCalls,
|
|
340
|
-
node
|
|
341
|
-
]
|
|
342
|
-
});
|
|
343
|
-
})
|
|
344
|
-
);
|
|
331
|
+
hooks.set(hook._, {
|
|
332
|
+
...hook,
|
|
333
|
+
hookCalls: [
|
|
334
|
+
...hook.hookCalls,
|
|
335
|
+
node
|
|
336
|
+
]
|
|
337
|
+
});
|
|
345
338
|
}
|
|
346
339
|
};
|
|
347
340
|
return { ctx, listeners };
|
|
@@ -383,29 +376,28 @@ var ERFunctionComponentFlag = {
|
|
|
383
376
|
// Async: 1n << 4n,
|
|
384
377
|
};
|
|
385
378
|
function isComponentWrapperCall(node, context) {
|
|
386
|
-
if (node.type !== types
|
|
379
|
+
if (node.type !== types.AST_NODE_TYPES.CallExpression) {
|
|
387
380
|
return false;
|
|
388
381
|
}
|
|
389
382
|
return isMemoCall(node, context) || isForwardRefCall(node, context) || isReactHookCallWithNameLoose(node)("useCallback");
|
|
390
383
|
}
|
|
391
384
|
function getFunctionComponentIdentifier(node, context) {
|
|
392
385
|
const functionId = AST8__namespace.getFunctionIdentifier(node);
|
|
393
|
-
if (
|
|
386
|
+
if (functionId != null) {
|
|
394
387
|
return functionId;
|
|
395
388
|
}
|
|
396
389
|
const { parent } = node;
|
|
397
|
-
if (parent.type === types
|
|
398
|
-
return
|
|
390
|
+
if (parent.type === types.AST_NODE_TYPES.CallExpression && isComponentWrapperCall(parent, context) && parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator && parent.parent.id.type === types.AST_NODE_TYPES.Identifier) {
|
|
391
|
+
return parent.parent.id;
|
|
399
392
|
}
|
|
400
|
-
if (parent.type === types
|
|
401
|
-
return
|
|
393
|
+
if (parent.type === types.AST_NODE_TYPES.CallExpression && isComponentWrapperCall(parent, context) && parent.parent.type === types.AST_NODE_TYPES.CallExpression && isComponentWrapperCall(parent.parent, context) && parent.parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator && parent.parent.parent.id.type === types.AST_NODE_TYPES.Identifier) {
|
|
394
|
+
return parent.parent.parent.id;
|
|
402
395
|
}
|
|
403
|
-
return eff.
|
|
396
|
+
return eff._;
|
|
404
397
|
}
|
|
405
|
-
|
|
406
|
-
// src/component/component-name.ts
|
|
407
398
|
var RE_COMPONENT_NAME = /^_?[A-Z]/u;
|
|
408
399
|
function getComponentNameFromIdentifier(node) {
|
|
400
|
+
if (node == null) return eff._;
|
|
409
401
|
return Array.isArray(node) ? node.map((n) => n.name).join(".") : node.name;
|
|
410
402
|
}
|
|
411
403
|
function isComponentName(name) {
|
|
@@ -415,9 +407,9 @@ function isClassComponent(node) {
|
|
|
415
407
|
if ("superClass" in node && node.superClass != null) {
|
|
416
408
|
const re = /^(?:Pure)?Component$/u;
|
|
417
409
|
switch (true) {
|
|
418
|
-
case node.superClass.type === types
|
|
410
|
+
case node.superClass.type === types.AST_NODE_TYPES.Identifier:
|
|
419
411
|
return re.test(node.superClass.name);
|
|
420
|
-
case (node.superClass.type === types
|
|
412
|
+
case (node.superClass.type === types.AST_NODE_TYPES.MemberExpression && node.superClass.property.type === types.AST_NODE_TYPES.Identifier):
|
|
421
413
|
return re.test(node.superClass.property.name);
|
|
422
414
|
}
|
|
423
415
|
}
|
|
@@ -427,9 +419,9 @@ function isPureComponent(node) {
|
|
|
427
419
|
if ("superClass" in node && node.superClass != null) {
|
|
428
420
|
const re = /^PureComponent$/u;
|
|
429
421
|
switch (true) {
|
|
430
|
-
case node.superClass.type === types
|
|
422
|
+
case node.superClass.type === types.AST_NODE_TYPES.Identifier:
|
|
431
423
|
return re.test(node.superClass.name);
|
|
432
|
-
case (node.superClass.type === types
|
|
424
|
+
case (node.superClass.type === types.AST_NODE_TYPES.MemberExpression && node.superClass.property.type === types.AST_NODE_TYPES.Identifier):
|
|
433
425
|
return re.test(node.superClass.property.name);
|
|
434
426
|
}
|
|
435
427
|
}
|
|
@@ -438,7 +430,7 @@ function isPureComponent(node) {
|
|
|
438
430
|
|
|
439
431
|
// src/component/component-render-method.ts
|
|
440
432
|
function isRenderMethodLike(node) {
|
|
441
|
-
return (node.type === types
|
|
433
|
+
return (node.type === types.AST_NODE_TYPES.MethodDefinition || node.type === types.AST_NODE_TYPES.PropertyDefinition) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "render" && node.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration;
|
|
442
434
|
}
|
|
443
435
|
function isFunctionOfRenderMethod(node) {
|
|
444
436
|
if (!isRenderMethodLike(node.parent)) {
|
|
@@ -447,14 +439,13 @@ function isFunctionOfRenderMethod(node) {
|
|
|
447
439
|
return isClassComponent(node.parent.parent.parent);
|
|
448
440
|
}
|
|
449
441
|
function isInsideRenderMethod(node) {
|
|
450
|
-
return
|
|
442
|
+
return AST8__namespace.findParentNode(node, (node2) => isRenderMethodLike(node2) && isClassComponent(node2.parent.parent)) != null;
|
|
451
443
|
}
|
|
444
|
+
|
|
445
|
+
// src/component/misc.ts
|
|
452
446
|
function hasNoneOrValidComponentName(node, context) {
|
|
453
|
-
const
|
|
454
|
-
if (
|
|
455
|
-
return true;
|
|
456
|
-
}
|
|
457
|
-
const id2 = mbIdentifier.value;
|
|
447
|
+
const id2 = getFunctionComponentIdentifier(node, context);
|
|
448
|
+
if (id2 == null) return true;
|
|
458
449
|
const name = Array.isArray(id2) ? id2.at(-1)?.name : id2.name;
|
|
459
450
|
return name != null && isComponentName(name);
|
|
460
451
|
}
|
|
@@ -476,26 +467,24 @@ function hasValidHierarchy(node, context, hint) {
|
|
|
476
467
|
if (hint & ERComponentHint.SkipClassProperty && AST8__namespace.isFunctionOfClassProperty(node.parent)) {
|
|
477
468
|
return false;
|
|
478
469
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
])
|
|
489
|
-
),
|
|
490
|
-
AST8__namespace.is(types$1.AST_NODE_TYPES.JSXExpressionContainer)
|
|
470
|
+
const boundaryNode = AST8__namespace.findParentNode(
|
|
471
|
+
node,
|
|
472
|
+
AST8__namespace.isOneOf([
|
|
473
|
+
types.AST_NODE_TYPES.JSXExpressionContainer,
|
|
474
|
+
types.AST_NODE_TYPES.ArrowFunctionExpression,
|
|
475
|
+
types.AST_NODE_TYPES.FunctionExpression,
|
|
476
|
+
types.AST_NODE_TYPES.Property,
|
|
477
|
+
types.AST_NODE_TYPES.ClassBody
|
|
478
|
+
])
|
|
491
479
|
);
|
|
480
|
+
return boundaryNode == null || boundaryNode.type !== types.AST_NODE_TYPES.JSXExpressionContainer;
|
|
492
481
|
}
|
|
493
482
|
function getComponentFlag(initPath) {
|
|
494
483
|
let flag = ERFunctionComponentFlag.None;
|
|
495
|
-
if (AST8__namespace.hasCallInFunctionInitPath("memo")(initPath)) {
|
|
484
|
+
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("memo")(initPath)) {
|
|
496
485
|
flag |= ERFunctionComponentFlag.Memo;
|
|
497
486
|
}
|
|
498
|
-
if (AST8__namespace.hasCallInFunctionInitPath("forwardRef")(initPath)) {
|
|
487
|
+
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("forwardRef")(initPath)) {
|
|
499
488
|
flag |= ERFunctionComponentFlag.ForwardRef;
|
|
500
489
|
}
|
|
501
490
|
return flag;
|
|
@@ -504,14 +493,15 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
504
493
|
const jsxCtx = { getScope: (node) => context.sourceCode.getScope(node) };
|
|
505
494
|
const components = /* @__PURE__ */ new Map();
|
|
506
495
|
const functionEntries = [];
|
|
507
|
-
const
|
|
496
|
+
const getCurrentEntry = () => functionEntries.at(-1);
|
|
508
497
|
const onFunctionEnter = (node) => {
|
|
509
498
|
const key = getId();
|
|
510
499
|
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
|
|
511
500
|
};
|
|
512
501
|
const onFunctionExit = () => {
|
|
513
502
|
const entry = functionEntries.at(-1);
|
|
514
|
-
if (
|
|
503
|
+
if (entry == null) return;
|
|
504
|
+
if (!entry.isComponent) {
|
|
515
505
|
return functionEntries.pop();
|
|
516
506
|
}
|
|
517
507
|
const shouldDrop = AST8__namespace.getNestedReturnStatements(entry.node.body).slice().reverse().some((r) => {
|
|
@@ -523,23 +513,23 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
523
513
|
return functionEntries.pop();
|
|
524
514
|
};
|
|
525
515
|
const ctx = {
|
|
526
|
-
|
|
516
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
517
|
+
getAllComponents(node) {
|
|
527
518
|
return components;
|
|
528
519
|
},
|
|
529
|
-
|
|
530
|
-
getCurrentFunctionStack() {
|
|
520
|
+
getCurrentEntries() {
|
|
531
521
|
return [...functionEntries];
|
|
532
|
-
}
|
|
522
|
+
},
|
|
523
|
+
getCurrentEntry
|
|
533
524
|
};
|
|
534
525
|
const listeners = {
|
|
535
526
|
":function[type]": onFunctionEnter,
|
|
536
527
|
":function[type]:exit": onFunctionExit,
|
|
537
528
|
"ArrowFunctionExpression[type][body.type!='BlockStatement']"() {
|
|
538
|
-
const
|
|
539
|
-
if (
|
|
529
|
+
const entry = getCurrentEntry();
|
|
530
|
+
if (entry == null) {
|
|
540
531
|
return;
|
|
541
532
|
}
|
|
542
|
-
const entry = mbEntry.value;
|
|
543
533
|
const { body } = entry.node;
|
|
544
534
|
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX4__namespace.isJSXValue(body, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
545
535
|
if (!isComponent) {
|
|
@@ -547,7 +537,7 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
547
537
|
}
|
|
548
538
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
549
539
|
const id2 = getFunctionComponentIdentifier(entry.node, context);
|
|
550
|
-
const name =
|
|
540
|
+
const name = getComponentNameFromIdentifier(id2);
|
|
551
541
|
const key = getId();
|
|
552
542
|
components.set(key, {
|
|
553
543
|
_: key,
|
|
@@ -555,7 +545,7 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
555
545
|
kind: "function",
|
|
556
546
|
name,
|
|
557
547
|
node: entry.node,
|
|
558
|
-
displayName: eff.
|
|
548
|
+
displayName: eff._,
|
|
559
549
|
flag: getComponentFlag(initPath),
|
|
560
550
|
hint,
|
|
561
551
|
hookCalls: entry.hookCalls,
|
|
@@ -564,38 +554,32 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
564
554
|
},
|
|
565
555
|
"AssignmentExpression[type][operator='='][left.type='MemberExpression'][left.property.name='displayName']"(node) {
|
|
566
556
|
const { left, right } = node;
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
return;
|
|
570
|
-
}
|
|
571
|
-
const componentName = mbComponentName.value;
|
|
572
|
-
const component = Array.from(components.values()).findLast(({ name }) => eff.O.exists(name, (n) => n === componentName));
|
|
557
|
+
const componentName = left.object.type === types.AST_NODE_TYPES.Identifier ? left.object.name : eff._;
|
|
558
|
+
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);
|
|
573
559
|
if (component == null) {
|
|
574
560
|
return;
|
|
575
561
|
}
|
|
576
562
|
components.set(component._, {
|
|
577
563
|
...component,
|
|
578
|
-
displayName:
|
|
564
|
+
displayName: right
|
|
579
565
|
});
|
|
580
566
|
},
|
|
581
567
|
"CallExpression[type]:exit"(node) {
|
|
582
568
|
if (!isReactHookCall(node)) {
|
|
583
569
|
return;
|
|
584
570
|
}
|
|
585
|
-
const
|
|
586
|
-
if (
|
|
571
|
+
const entry = getCurrentEntry();
|
|
572
|
+
if (entry == null) {
|
|
587
573
|
return;
|
|
588
574
|
}
|
|
589
|
-
const entry = mbEntry.value;
|
|
590
575
|
functionEntries.pop();
|
|
591
576
|
functionEntries.push({ ...entry, hookCalls: [...entry.hookCalls, node] });
|
|
592
577
|
},
|
|
593
578
|
"ReturnStatement[type]"(node) {
|
|
594
|
-
const
|
|
595
|
-
if (
|
|
579
|
+
const entry = getCurrentEntry();
|
|
580
|
+
if (entry == null) {
|
|
596
581
|
return;
|
|
597
582
|
}
|
|
598
|
-
const entry = mbEntry.value;
|
|
599
583
|
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX4__namespace.isJSXValue(node.argument, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
600
584
|
if (!isComponent) {
|
|
601
585
|
return;
|
|
@@ -604,14 +588,14 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
604
588
|
functionEntries.push({ ...entry, isComponent });
|
|
605
589
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
606
590
|
const id2 = getFunctionComponentIdentifier(entry.node, context);
|
|
607
|
-
const name =
|
|
591
|
+
const name = getComponentNameFromIdentifier(id2);
|
|
608
592
|
components.set(entry.key, {
|
|
609
593
|
_: entry.key,
|
|
610
594
|
id: id2,
|
|
611
595
|
kind: "function",
|
|
612
596
|
name,
|
|
613
597
|
node: entry.node,
|
|
614
|
-
displayName: eff.
|
|
598
|
+
displayName: eff._,
|
|
615
599
|
flag: getComponentFlag(initPath),
|
|
616
600
|
hint,
|
|
617
601
|
hookCalls: entry.hookCalls,
|
|
@@ -624,7 +608,8 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
624
608
|
function useComponentCollectorLegacy() {
|
|
625
609
|
const components = /* @__PURE__ */ new Map();
|
|
626
610
|
const ctx = {
|
|
627
|
-
|
|
611
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
612
|
+
getAllComponents(node) {
|
|
628
613
|
return components;
|
|
629
614
|
}
|
|
630
615
|
};
|
|
@@ -641,10 +626,10 @@ function useComponentCollectorLegacy() {
|
|
|
641
626
|
_: key,
|
|
642
627
|
id: id2,
|
|
643
628
|
kind: "class",
|
|
644
|
-
name:
|
|
629
|
+
name: id2?.name,
|
|
645
630
|
node,
|
|
646
631
|
// TODO: Get displayName of class component
|
|
647
|
-
displayName: eff.
|
|
632
|
+
displayName: eff._,
|
|
648
633
|
flag,
|
|
649
634
|
hint: 0n,
|
|
650
635
|
// TODO: Get methods of class component
|
|
@@ -659,24 +644,20 @@ function useComponentCollectorLegacy() {
|
|
|
659
644
|
return { ctx, listeners };
|
|
660
645
|
}
|
|
661
646
|
function isSetupFunction(node) {
|
|
662
|
-
|
|
647
|
+
if (node == null) return eff._;
|
|
648
|
+
return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && node.parent.callee.type === types.AST_NODE_TYPES.Identifier && node.parent.arguments.at(0) === node && isUseEffectCallLoose(node.parent);
|
|
663
649
|
}
|
|
664
650
|
function isCleanupFunction(node) {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
eff.O.bind("nearFunctionOfReturn", ({ nearReturn }) => AST8__namespace.findParentNodeGuard(nearReturn, AST8__namespace.isFunction)),
|
|
670
|
-
eff.O.exists(
|
|
671
|
-
({ nearFunction, nearFunctionOfReturn }) => nearFunction === nearFunctionOfReturn && isSetupFunction(nearFunction)
|
|
672
|
-
)
|
|
673
|
-
);
|
|
651
|
+
const nearReturn = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.ReturnStatement));
|
|
652
|
+
const nearFunction = AST8__namespace.findParentNodeGuard(node, AST8__namespace.isFunction);
|
|
653
|
+
const nearFunctionOfReturn = AST8__namespace.findParentNodeGuard(nearReturn, AST8__namespace.isFunction);
|
|
654
|
+
return nearFunction === nearFunctionOfReturn && isSetupFunction(nearFunction);
|
|
674
655
|
}
|
|
675
656
|
function isComponentDidMount(node) {
|
|
676
|
-
return AST8__namespace.isOneOf([types
|
|
657
|
+
return AST8__namespace.isOneOf([types.AST_NODE_TYPES.MethodDefinition, types.AST_NODE_TYPES.PropertyDefinition])(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentDidMount";
|
|
677
658
|
}
|
|
678
659
|
function isComponentWillUnmount(node) {
|
|
679
|
-
return AST8__namespace.isOneOf([types
|
|
660
|
+
return AST8__namespace.isOneOf([types.AST_NODE_TYPES.MethodDefinition, types.AST_NODE_TYPES.PropertyDefinition])(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentWillUnmount";
|
|
680
661
|
}
|
|
681
662
|
function isComponentDidMountFunction(node) {
|
|
682
663
|
return AST8__namespace.isFunction(node) && isComponentDidMount(node.parent) && node.parent.value === node;
|
|
@@ -684,14 +665,14 @@ function isComponentDidMountFunction(node) {
|
|
|
684
665
|
function isComponentWillUnmountFunction(node) {
|
|
685
666
|
return AST8__namespace.isFunction(node) && isComponentWillUnmount(node.parent) && node.parent.value === node;
|
|
686
667
|
}
|
|
687
|
-
var ERPhaseRelevance =
|
|
668
|
+
var ERPhaseRelevance = eff.birecord({
|
|
688
669
|
mount: "unmount",
|
|
689
670
|
setup: "cleanup"
|
|
690
671
|
});
|
|
691
|
-
var isInversePhase = eff.
|
|
672
|
+
var isInversePhase = eff.dual(2, (a, b) => ERPhaseRelevance.get(a) === b);
|
|
692
673
|
function isDirectValueOfRenderPropertyLoose(node) {
|
|
693
674
|
const matching = (node2) => {
|
|
694
|
-
return node2.type === types
|
|
675
|
+
return node2.type === types.AST_NODE_TYPES.Property && node2.key.type === types.AST_NODE_TYPES.Identifier && node2.key.name.startsWith("render");
|
|
695
676
|
};
|
|
696
677
|
return matching(node) || node.parent != null && matching(node.parent);
|
|
697
678
|
}
|
|
@@ -699,20 +680,16 @@ function isDeclaredInRenderPropLoose(node) {
|
|
|
699
680
|
if (isDirectValueOfRenderPropertyLoose(node)) {
|
|
700
681
|
return true;
|
|
701
682
|
}
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
eff.O.exists(
|
|
708
|
-
(n) => n.type === types$1.AST_NODE_TYPES.JSXIdentifier && n.name.startsWith("render")
|
|
709
|
-
)
|
|
710
|
-
);
|
|
683
|
+
const parent = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
684
|
+
if (parent?.type !== types.AST_NODE_TYPES.JSXAttribute) {
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
return parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
711
688
|
}
|
|
712
689
|
function isRenderFunctionLoose(node, context) {
|
|
713
690
|
const { body, parent } = node;
|
|
714
|
-
if (
|
|
715
|
-
return parent.type === types
|
|
691
|
+
if (AST8__namespace.getFunctionIdentifier(node)?.name.startsWith("render")) {
|
|
692
|
+
return parent.type === types.AST_NODE_TYPES.JSXExpressionContainer && parent.parent.type === types.AST_NODE_TYPES.JSXAttribute && parent.parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.parent.name.name.startsWith("render");
|
|
716
693
|
}
|
|
717
694
|
return JSX4__namespace.isJSXValue(
|
|
718
695
|
body,
|
|
@@ -723,14 +700,14 @@ function isRenderFunctionLoose(node, context) {
|
|
|
723
700
|
);
|
|
724
701
|
}
|
|
725
702
|
function isRenderPropLoose(node, context) {
|
|
726
|
-
if (node.name.type !== types
|
|
703
|
+
if (node.name.type !== types.AST_NODE_TYPES.JSXIdentifier) {
|
|
727
704
|
return false;
|
|
728
705
|
}
|
|
729
|
-
return node.name.name.startsWith("render") && node.value?.type === types
|
|
706
|
+
return node.name.name.startsWith("render") && node.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer && AST8__namespace.isFunction(node.value.expression) && isRenderFunctionLoose(node.value.expression, context);
|
|
730
707
|
}
|
|
731
708
|
function isThisSetState(node) {
|
|
732
709
|
const { callee } = node;
|
|
733
|
-
return callee.type === types
|
|
710
|
+
return callee.type === types.AST_NODE_TYPES.MemberExpression && AST8__namespace.isThisExpression(callee.object) && callee.property.type === types.AST_NODE_TYPES.Identifier && callee.property.name === "setState";
|
|
734
711
|
}
|
|
735
712
|
|
|
736
713
|
exports.DEFAULT_COMPONENT_HINT = DEFAULT_COMPONENT_HINT;
|
|
@@ -741,7 +718,7 @@ exports.ERPhaseRelevance = ERPhaseRelevance;
|
|
|
741
718
|
exports.RE_COMPONENT_NAME = RE_COMPONENT_NAME;
|
|
742
719
|
exports.RE_HOOK_NAME = RE_HOOK_NAME;
|
|
743
720
|
exports.getComponentNameFromIdentifier = getComponentNameFromIdentifier;
|
|
744
|
-
exports.
|
|
721
|
+
exports.getElementNameAndRepresentName = getElementNameAndRepresentName;
|
|
745
722
|
exports.getFunctionComponentIdentifier = getFunctionComponentIdentifier;
|
|
746
723
|
exports.getId = getId;
|
|
747
724
|
exports.hasNoneOrValidComponentName = hasNoneOrValidComponentName;
|