@eslint-react/core 1.23.3-next.8 → 1.24.0-beta.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.
- package/dist/index.d.mts +47 -45
- package/dist/index.d.ts +47 -45
- package/dist/index.js +186 -208
- package/dist/index.mjs +140 -162
- 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 !== eff._) 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 === eff._) 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) return false;
|
|
169
|
+
const prop = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.ObjectExpression));
|
|
170
|
+
if (!prop) 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 || 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 === eff._) return eff._;
|
|
189
|
+
const id2 = AST8__namespace.getFunctionIdentifier(node);
|
|
190
|
+
return id2?.name !== eff._ && isReactHookName(id2.name);
|
|
200
191
|
}
|
|
201
192
|
function isReactHookCall(node) {
|
|
202
|
-
if (node
|
|
193
|
+
if (node === eff._) 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 === eff._) 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 === eff._) 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 === eff._) 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 !== eff._ && 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 === eff._) {
|
|
329
325
|
return;
|
|
330
326
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
});
|
|
343
|
-
})
|
|
344
|
-
);
|
|
327
|
+
const hook = hooks.get(fEntry.key);
|
|
328
|
+
if (hook === eff._) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
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,24 +376,24 @@ 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) {
|
|
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
398
|
|
|
406
399
|
// src/component/component-name.ts
|
|
@@ -412,24 +405,24 @@ function isComponentName(name) {
|
|
|
412
405
|
return RE_COMPONENT_NAME.test(name);
|
|
413
406
|
}
|
|
414
407
|
function isClassComponent(node) {
|
|
415
|
-
if ("superClass" in node && node.superClass
|
|
408
|
+
if ("superClass" in node && node.superClass) {
|
|
416
409
|
const re = /^(?:Pure)?Component$/u;
|
|
417
410
|
switch (true) {
|
|
418
|
-
case node.superClass.type === types
|
|
411
|
+
case node.superClass.type === types.AST_NODE_TYPES.Identifier:
|
|
419
412
|
return re.test(node.superClass.name);
|
|
420
|
-
case (node.superClass.type === types
|
|
413
|
+
case (node.superClass.type === types.AST_NODE_TYPES.MemberExpression && node.superClass.property.type === types.AST_NODE_TYPES.Identifier):
|
|
421
414
|
return re.test(node.superClass.property.name);
|
|
422
415
|
}
|
|
423
416
|
}
|
|
424
417
|
return false;
|
|
425
418
|
}
|
|
426
419
|
function isPureComponent(node) {
|
|
427
|
-
if ("superClass" in node && node.superClass
|
|
420
|
+
if ("superClass" in node && node.superClass) {
|
|
428
421
|
const re = /^PureComponent$/u;
|
|
429
422
|
switch (true) {
|
|
430
|
-
case node.superClass.type === types
|
|
423
|
+
case node.superClass.type === types.AST_NODE_TYPES.Identifier:
|
|
431
424
|
return re.test(node.superClass.name);
|
|
432
|
-
case (node.superClass.type === types
|
|
425
|
+
case (node.superClass.type === types.AST_NODE_TYPES.MemberExpression && node.superClass.property.type === types.AST_NODE_TYPES.Identifier):
|
|
433
426
|
return re.test(node.superClass.property.name);
|
|
434
427
|
}
|
|
435
428
|
}
|
|
@@ -438,7 +431,7 @@ function isPureComponent(node) {
|
|
|
438
431
|
|
|
439
432
|
// src/component/component-render-method.ts
|
|
440
433
|
function isRenderMethodLike(node) {
|
|
441
|
-
return (node.type === types
|
|
434
|
+
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
435
|
}
|
|
443
436
|
function isFunctionOfRenderMethod(node) {
|
|
444
437
|
if (!isRenderMethodLike(node.parent)) {
|
|
@@ -447,14 +440,13 @@ function isFunctionOfRenderMethod(node) {
|
|
|
447
440
|
return isClassComponent(node.parent.parent.parent);
|
|
448
441
|
}
|
|
449
442
|
function isInsideRenderMethod(node) {
|
|
450
|
-
return
|
|
443
|
+
return !!AST8__namespace.findParentNode(node, (node2) => isRenderMethodLike(node2) && isClassComponent(node2.parent.parent));
|
|
451
444
|
}
|
|
445
|
+
|
|
446
|
+
// src/component/misc.ts
|
|
452
447
|
function hasNoneOrValidComponentName(node, context) {
|
|
453
|
-
const
|
|
454
|
-
if (
|
|
455
|
-
return true;
|
|
456
|
-
}
|
|
457
|
-
const id2 = mbIdentifier.value;
|
|
448
|
+
const id2 = getFunctionComponentIdentifier(node, context);
|
|
449
|
+
if (!id2) return true;
|
|
458
450
|
const name = Array.isArray(id2) ? id2.at(-1)?.name : id2.name;
|
|
459
451
|
return name != null && isComponentName(name);
|
|
460
452
|
}
|
|
@@ -476,26 +468,24 @@ function hasValidHierarchy(node, context, hint) {
|
|
|
476
468
|
if (hint & ERComponentHint.SkipClassProperty && AST8__namespace.isFunctionOfClassProperty(node.parent)) {
|
|
477
469
|
return false;
|
|
478
470
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
])
|
|
489
|
-
),
|
|
490
|
-
AST8__namespace.is(types$1.AST_NODE_TYPES.JSXExpressionContainer)
|
|
471
|
+
const boundaryNode = AST8__namespace.findParentNode(
|
|
472
|
+
node,
|
|
473
|
+
AST8__namespace.isOneOf([
|
|
474
|
+
types.AST_NODE_TYPES.JSXExpressionContainer,
|
|
475
|
+
types.AST_NODE_TYPES.ArrowFunctionExpression,
|
|
476
|
+
types.AST_NODE_TYPES.FunctionExpression,
|
|
477
|
+
types.AST_NODE_TYPES.Property,
|
|
478
|
+
types.AST_NODE_TYPES.ClassBody
|
|
479
|
+
])
|
|
491
480
|
);
|
|
481
|
+
return !boundaryNode || boundaryNode.type !== types.AST_NODE_TYPES.JSXExpressionContainer;
|
|
492
482
|
}
|
|
493
483
|
function getComponentFlag(initPath) {
|
|
494
484
|
let flag = ERFunctionComponentFlag.None;
|
|
495
|
-
if (AST8__namespace.hasCallInFunctionInitPath("memo")(initPath)) {
|
|
485
|
+
if (initPath && AST8__namespace.hasCallInFunctionInitPath("memo")(initPath)) {
|
|
496
486
|
flag |= ERFunctionComponentFlag.Memo;
|
|
497
487
|
}
|
|
498
|
-
if (AST8__namespace.hasCallInFunctionInitPath("forwardRef")(initPath)) {
|
|
488
|
+
if (initPath && AST8__namespace.hasCallInFunctionInitPath("forwardRef")(initPath)) {
|
|
499
489
|
flag |= ERFunctionComponentFlag.ForwardRef;
|
|
500
490
|
}
|
|
501
491
|
return flag;
|
|
@@ -504,18 +494,19 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
504
494
|
const jsxCtx = { getScope: (node) => context.sourceCode.getScope(node) };
|
|
505
495
|
const components = /* @__PURE__ */ new Map();
|
|
506
496
|
const functionEntries = [];
|
|
507
|
-
const
|
|
497
|
+
const getCurrentEntry = () => functionEntries.at(-1);
|
|
508
498
|
const onFunctionEnter = (node) => {
|
|
509
499
|
const key = getId();
|
|
510
500
|
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
|
|
511
501
|
};
|
|
512
502
|
const onFunctionExit = () => {
|
|
513
503
|
const entry = functionEntries.at(-1);
|
|
514
|
-
if (!entry
|
|
504
|
+
if (!entry) return;
|
|
505
|
+
if (!entry.isComponent) {
|
|
515
506
|
return functionEntries.pop();
|
|
516
507
|
}
|
|
517
508
|
const shouldDrop = AST8__namespace.getNestedReturnStatements(entry.node.body).slice().reverse().some((r) => {
|
|
518
|
-
return context.sourceCode.getScope(r).block === entry.node && r.argument
|
|
509
|
+
return context.sourceCode.getScope(r).block === entry.node && !!r.argument && !JSX4__namespace.isJSXValue(r.argument, jsxCtx, hint);
|
|
519
510
|
});
|
|
520
511
|
if (shouldDrop) {
|
|
521
512
|
components.delete(entry.key);
|
|
@@ -523,23 +514,23 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
523
514
|
return functionEntries.pop();
|
|
524
515
|
};
|
|
525
516
|
const ctx = {
|
|
526
|
-
|
|
517
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
518
|
+
getAllComponents(node) {
|
|
527
519
|
return components;
|
|
528
520
|
},
|
|
529
|
-
|
|
530
|
-
getCurrentFunctionStack() {
|
|
521
|
+
getCurrentEntries() {
|
|
531
522
|
return [...functionEntries];
|
|
532
|
-
}
|
|
523
|
+
},
|
|
524
|
+
getCurrentEntry
|
|
533
525
|
};
|
|
534
526
|
const listeners = {
|
|
535
527
|
":function[type]": onFunctionEnter,
|
|
536
528
|
":function[type]:exit": onFunctionExit,
|
|
537
529
|
"ArrowFunctionExpression[type][body.type!='BlockStatement']"() {
|
|
538
|
-
const
|
|
539
|
-
if (
|
|
530
|
+
const entry = getCurrentEntry();
|
|
531
|
+
if (!entry) {
|
|
540
532
|
return;
|
|
541
533
|
}
|
|
542
|
-
const entry = mbEntry.value;
|
|
543
534
|
const { body } = entry.node;
|
|
544
535
|
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX4__namespace.isJSXValue(body, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
545
536
|
if (!isComponent) {
|
|
@@ -547,7 +538,7 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
547
538
|
}
|
|
548
539
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
549
540
|
const id2 = getFunctionComponentIdentifier(entry.node, context);
|
|
550
|
-
const name =
|
|
541
|
+
const name = id2 && getComponentNameFromIdentifier(id2);
|
|
551
542
|
const key = getId();
|
|
552
543
|
components.set(key, {
|
|
553
544
|
_: key,
|
|
@@ -555,7 +546,7 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
555
546
|
kind: "function",
|
|
556
547
|
name,
|
|
557
548
|
node: entry.node,
|
|
558
|
-
displayName: eff.
|
|
549
|
+
displayName: eff._,
|
|
559
550
|
flag: getComponentFlag(initPath),
|
|
560
551
|
hint,
|
|
561
552
|
hookCalls: entry.hookCalls,
|
|
@@ -564,38 +555,32 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
564
555
|
},
|
|
565
556
|
"AssignmentExpression[type][operator='='][left.type='MemberExpression'][left.property.name='displayName']"(node) {
|
|
566
557
|
const { left, right } = node;
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
571
|
-
const componentName = mbComponentName.value;
|
|
572
|
-
const component = Array.from(components.values()).findLast(({ name }) => eff.O.exists(name, (n) => n === componentName));
|
|
573
|
-
if (component == null) {
|
|
558
|
+
const componentName = left.object.type === types.AST_NODE_TYPES.Identifier ? left.object.name : eff._;
|
|
559
|
+
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);
|
|
560
|
+
if (!component) {
|
|
574
561
|
return;
|
|
575
562
|
}
|
|
576
563
|
components.set(component._, {
|
|
577
564
|
...component,
|
|
578
|
-
displayName:
|
|
565
|
+
displayName: right
|
|
579
566
|
});
|
|
580
567
|
},
|
|
581
568
|
"CallExpression[type]:exit"(node) {
|
|
582
569
|
if (!isReactHookCall(node)) {
|
|
583
570
|
return;
|
|
584
571
|
}
|
|
585
|
-
const
|
|
586
|
-
if (
|
|
572
|
+
const entry = getCurrentEntry();
|
|
573
|
+
if (!entry) {
|
|
587
574
|
return;
|
|
588
575
|
}
|
|
589
|
-
const entry = mbEntry.value;
|
|
590
576
|
functionEntries.pop();
|
|
591
577
|
functionEntries.push({ ...entry, hookCalls: [...entry.hookCalls, node] });
|
|
592
578
|
},
|
|
593
579
|
"ReturnStatement[type]"(node) {
|
|
594
|
-
const
|
|
595
|
-
if (
|
|
580
|
+
const entry = getCurrentEntry();
|
|
581
|
+
if (!entry) {
|
|
596
582
|
return;
|
|
597
583
|
}
|
|
598
|
-
const entry = mbEntry.value;
|
|
599
584
|
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX4__namespace.isJSXValue(node.argument, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
600
585
|
if (!isComponent) {
|
|
601
586
|
return;
|
|
@@ -604,14 +589,14 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
604
589
|
functionEntries.push({ ...entry, isComponent });
|
|
605
590
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
606
591
|
const id2 = getFunctionComponentIdentifier(entry.node, context);
|
|
607
|
-
const name =
|
|
592
|
+
const name = id2 && getComponentNameFromIdentifier(id2);
|
|
608
593
|
components.set(entry.key, {
|
|
609
594
|
_: entry.key,
|
|
610
595
|
id: id2,
|
|
611
596
|
kind: "function",
|
|
612
597
|
name,
|
|
613
598
|
node: entry.node,
|
|
614
|
-
displayName: eff.
|
|
599
|
+
displayName: eff._,
|
|
615
600
|
flag: getComponentFlag(initPath),
|
|
616
601
|
hint,
|
|
617
602
|
hookCalls: entry.hookCalls,
|
|
@@ -624,7 +609,8 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
624
609
|
function useComponentCollectorLegacy() {
|
|
625
610
|
const components = /* @__PURE__ */ new Map();
|
|
626
611
|
const ctx = {
|
|
627
|
-
|
|
612
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
613
|
+
getAllComponents(node) {
|
|
628
614
|
return components;
|
|
629
615
|
}
|
|
630
616
|
};
|
|
@@ -641,10 +627,10 @@ function useComponentCollectorLegacy() {
|
|
|
641
627
|
_: key,
|
|
642
628
|
id: id2,
|
|
643
629
|
kind: "class",
|
|
644
|
-
name:
|
|
630
|
+
name: id2?.name,
|
|
645
631
|
node,
|
|
646
632
|
// TODO: Get displayName of class component
|
|
647
|
-
displayName: eff.
|
|
633
|
+
displayName: eff._,
|
|
648
634
|
flag,
|
|
649
635
|
hint: 0n,
|
|
650
636
|
// TODO: Get methods of class component
|
|
@@ -659,24 +645,20 @@ function useComponentCollectorLegacy() {
|
|
|
659
645
|
return { ctx, listeners };
|
|
660
646
|
}
|
|
661
647
|
function isSetupFunction(node) {
|
|
662
|
-
|
|
648
|
+
if (node === eff._) return eff._;
|
|
649
|
+
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
650
|
}
|
|
664
651
|
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
|
-
);
|
|
652
|
+
const nearReturn = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.ReturnStatement));
|
|
653
|
+
const nearFunction = AST8__namespace.findParentNodeGuard(node, AST8__namespace.isFunction);
|
|
654
|
+
const nearFunctionOfReturn = AST8__namespace.findParentNodeGuard(nearReturn, AST8__namespace.isFunction);
|
|
655
|
+
return nearFunction === nearFunctionOfReturn && isSetupFunction(nearFunction);
|
|
674
656
|
}
|
|
675
657
|
function isComponentDidMount(node) {
|
|
676
|
-
return AST8__namespace.isOneOf([types
|
|
658
|
+
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
659
|
}
|
|
678
660
|
function isComponentWillUnmount(node) {
|
|
679
|
-
return AST8__namespace.isOneOf([types
|
|
661
|
+
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
662
|
}
|
|
681
663
|
function isComponentDidMountFunction(node) {
|
|
682
664
|
return AST8__namespace.isFunction(node) && isComponentDidMount(node.parent) && node.parent.value === node;
|
|
@@ -684,35 +666,31 @@ function isComponentDidMountFunction(node) {
|
|
|
684
666
|
function isComponentWillUnmountFunction(node) {
|
|
685
667
|
return AST8__namespace.isFunction(node) && isComponentWillUnmount(node.parent) && node.parent.value === node;
|
|
686
668
|
}
|
|
687
|
-
var ERPhaseRelevance =
|
|
669
|
+
var ERPhaseRelevance = eff.birecord({
|
|
688
670
|
mount: "unmount",
|
|
689
671
|
setup: "cleanup"
|
|
690
672
|
});
|
|
691
|
-
var isInversePhase = eff.
|
|
673
|
+
var isInversePhase = eff.dual(2, (a, b) => ERPhaseRelevance.get(a) === b);
|
|
692
674
|
function isDirectValueOfRenderPropertyLoose(node) {
|
|
693
675
|
const matching = (node2) => {
|
|
694
|
-
return node2.type === types
|
|
676
|
+
return node2.type === types.AST_NODE_TYPES.Property && node2.key.type === types.AST_NODE_TYPES.Identifier && node2.key.name.startsWith("render");
|
|
695
677
|
};
|
|
696
|
-
return matching(node) || node.parent
|
|
678
|
+
return matching(node) || !!node.parent && matching(node.parent);
|
|
697
679
|
}
|
|
698
680
|
function isDeclaredInRenderPropLoose(node) {
|
|
699
681
|
if (isDirectValueOfRenderPropertyLoose(node)) {
|
|
700
682
|
return true;
|
|
701
683
|
}
|
|
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
|
-
);
|
|
684
|
+
const parent = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
685
|
+
if (parent?.type !== types.AST_NODE_TYPES.JSXAttribute) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
return parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
711
689
|
}
|
|
712
690
|
function isRenderFunctionLoose(node, context) {
|
|
713
691
|
const { body, parent } = node;
|
|
714
|
-
if (
|
|
715
|
-
return parent.type === types
|
|
692
|
+
if (AST8__namespace.getFunctionIdentifier(node)?.name.startsWith("render")) {
|
|
693
|
+
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
694
|
}
|
|
717
695
|
return JSX4__namespace.isJSXValue(
|
|
718
696
|
body,
|
|
@@ -723,14 +701,14 @@ function isRenderFunctionLoose(node, context) {
|
|
|
723
701
|
);
|
|
724
702
|
}
|
|
725
703
|
function isRenderPropLoose(node, context) {
|
|
726
|
-
if (node.name.type !== types
|
|
704
|
+
if (node.name.type !== types.AST_NODE_TYPES.JSXIdentifier) {
|
|
727
705
|
return false;
|
|
728
706
|
}
|
|
729
|
-
return node.name.name.startsWith("render") && node.value?.type === types
|
|
707
|
+
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
708
|
}
|
|
731
709
|
function isThisSetState(node) {
|
|
732
710
|
const { callee } = node;
|
|
733
|
-
return callee.type === types
|
|
711
|
+
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
712
|
}
|
|
735
713
|
|
|
736
714
|
exports.DEFAULT_COMPONENT_HINT = DEFAULT_COMPONENT_HINT;
|
|
@@ -741,7 +719,7 @@ exports.ERPhaseRelevance = ERPhaseRelevance;
|
|
|
741
719
|
exports.RE_COMPONENT_NAME = RE_COMPONENT_NAME;
|
|
742
720
|
exports.RE_HOOK_NAME = RE_HOOK_NAME;
|
|
743
721
|
exports.getComponentNameFromIdentifier = getComponentNameFromIdentifier;
|
|
744
|
-
exports.
|
|
722
|
+
exports.getElementNameAndRepresentName = getElementNameAndRepresentName;
|
|
745
723
|
exports.getFunctionComponentIdentifier = getFunctionComponentIdentifier;
|
|
746
724
|
exports.getId = getId;
|
|
747
725
|
exports.hasNoneOrValidComponentName = hasNoneOrValidComponentName;
|