@eslint-react/var 5.17.1 → 5.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +40 -40
- package/dist/index.js +131 -131
- package/package.json +7 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,6 @@
|
|
|
1
|
-
import { Scope } from "@typescript-eslint/scope-manager";
|
|
2
1
|
import { TSESTree } from "@typescript-eslint/types";
|
|
2
|
+
import { Scope } from "@typescript-eslint/scope-manager";
|
|
3
3
|
import { RuleContext } from "@eslint-react/eslint";
|
|
4
|
-
//#region src/compute-object-type.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Represents the type classification of an object node.
|
|
7
|
-
*/
|
|
8
|
-
type ObjectType = {
|
|
9
|
-
kind: "jsx";
|
|
10
|
-
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
11
|
-
} | {
|
|
12
|
-
kind: "array";
|
|
13
|
-
node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
14
|
-
} | {
|
|
15
|
-
kind: "plain";
|
|
16
|
-
node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
17
|
-
} | {
|
|
18
|
-
kind: "class";
|
|
19
|
-
node: TSESTree.ClassExpression;
|
|
20
|
-
} | {
|
|
21
|
-
kind: "instance";
|
|
22
|
-
node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
23
|
-
} | {
|
|
24
|
-
kind: "function";
|
|
25
|
-
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
26
|
-
} | {
|
|
27
|
-
kind: "regexp";
|
|
28
|
-
node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
29
|
-
} | {
|
|
30
|
-
kind: "unknown";
|
|
31
|
-
node: TSESTree.Node;
|
|
32
|
-
reason?: string;
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Detect the ObjectType of a given node.
|
|
36
|
-
* @param context The context of the rule.
|
|
37
|
-
* @param node The node to check.
|
|
38
|
-
* @returns The ObjectType of the node, or undefined if not detected.
|
|
39
|
-
*/
|
|
40
|
-
declare function computeObjectType(context: RuleContext, node: TSESTree.Node | null): ObjectType | null;
|
|
41
|
-
//#endregion
|
|
42
4
|
//#region src/get-require-expression-arguments.d.ts
|
|
43
5
|
/**
|
|
44
6
|
* Get the arguments of a require expression.
|
|
@@ -144,4 +106,42 @@ type AssignmentTarget = ReturnType<typeof resolveEnclosingAssignmentTarget>;
|
|
|
144
106
|
//#region src/resolve-import-source.d.ts
|
|
145
107
|
declare function resolveImportSource(name: string, initialScope: Scope, seen?: Set<string>): string | null;
|
|
146
108
|
//#endregion
|
|
147
|
-
|
|
109
|
+
//#region src/resolve-object-type.d.ts
|
|
110
|
+
/**
|
|
111
|
+
* Represents the type classification of an object node.
|
|
112
|
+
*/
|
|
113
|
+
type ObjectType = {
|
|
114
|
+
kind: "jsx";
|
|
115
|
+
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
116
|
+
} | {
|
|
117
|
+
kind: "array";
|
|
118
|
+
node: TSESTree.ArrayExpression | TSESTree.CallExpression;
|
|
119
|
+
} | {
|
|
120
|
+
kind: "plain";
|
|
121
|
+
node: TSESTree.ObjectExpression | TSESTree.CallExpression;
|
|
122
|
+
} | {
|
|
123
|
+
kind: "class";
|
|
124
|
+
node: TSESTree.ClassExpression;
|
|
125
|
+
} | {
|
|
126
|
+
kind: "instance";
|
|
127
|
+
node: TSESTree.NewExpression | TSESTree.ThisExpression;
|
|
128
|
+
} | {
|
|
129
|
+
kind: "function";
|
|
130
|
+
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
131
|
+
} | {
|
|
132
|
+
kind: "regexp";
|
|
133
|
+
node: TSESTree.RegExpLiteral | TSESTree.CallExpression;
|
|
134
|
+
} | {
|
|
135
|
+
kind: "unknown";
|
|
136
|
+
node: TSESTree.Node;
|
|
137
|
+
reason?: string;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Detect the ObjectType of a given node.
|
|
141
|
+
* @param context The context of the rule.
|
|
142
|
+
* @param node The node to check.
|
|
143
|
+
* @returns The ObjectType of the node, or undefined if not detected.
|
|
144
|
+
*/
|
|
145
|
+
declare function resolveObjectType(context: RuleContext, node: TSESTree.Node | null): ObjectType | null;
|
|
146
|
+
//#endregion
|
|
147
|
+
export { AssignmentTarget, ObjectType, getRequireExpressionArguments, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveImportSource, resolveObjectType };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { Check, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
2
|
-
import { DefinitionType } from "@typescript-eslint/scope-manager";
|
|
3
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
4
3
|
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
4
|
+
import { DefinitionType } from "@typescript-eslint/scope-manager";
|
|
5
5
|
import { P, isMatching } from "ts-pattern";
|
|
6
6
|
|
|
7
|
+
//#region src/get-require-expression-arguments.ts
|
|
8
|
+
/**
|
|
9
|
+
* Get the arguments of a require expression.
|
|
10
|
+
* @param node The node to match.
|
|
11
|
+
* @returns The require expression arguments or null if the node is not a require expression.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
function getRequireExpressionArguments(node) {
|
|
15
|
+
const unwrapped = Extract.unwrap(node);
|
|
16
|
+
if (unwrapped.type === AST_NODE_TYPES.CallExpression) {
|
|
17
|
+
const callee = Extract.unwrap(unwrapped.callee);
|
|
18
|
+
if (callee.type === AST_NODE_TYPES.Identifier && callee.name === "require") return unwrapped.arguments;
|
|
19
|
+
}
|
|
20
|
+
if (unwrapped.type === AST_NODE_TYPES.MemberExpression) return getRequireExpressionArguments(unwrapped.object);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
7
25
|
//#region src/resolve.ts
|
|
8
26
|
/**
|
|
9
27
|
* Resolves an identifier to the AST node that represents its value,
|
|
@@ -63,135 +81,6 @@ function resolve(context, node, options) {
|
|
|
63
81
|
}
|
|
64
82
|
}
|
|
65
83
|
|
|
66
|
-
//#endregion
|
|
67
|
-
//#region src/compute-object-type.ts
|
|
68
|
-
/**
|
|
69
|
-
* Detect the ObjectType of a given node.
|
|
70
|
-
* @param context The context of the rule.
|
|
71
|
-
* @param node The node to check.
|
|
72
|
-
* @returns The ObjectType of the node, or undefined if not detected.
|
|
73
|
-
*/
|
|
74
|
-
function computeObjectType(context, node) {
|
|
75
|
-
if (node == null) return null;
|
|
76
|
-
switch (node.type) {
|
|
77
|
-
case AST_NODE_TYPES.JSXElement:
|
|
78
|
-
case AST_NODE_TYPES.JSXFragment: return {
|
|
79
|
-
kind: "jsx",
|
|
80
|
-
node
|
|
81
|
-
};
|
|
82
|
-
case AST_NODE_TYPES.ArrayExpression: return {
|
|
83
|
-
kind: "array",
|
|
84
|
-
node
|
|
85
|
-
};
|
|
86
|
-
case AST_NODE_TYPES.ObjectExpression: return {
|
|
87
|
-
kind: "plain",
|
|
88
|
-
node
|
|
89
|
-
};
|
|
90
|
-
case AST_NODE_TYPES.ClassExpression: return {
|
|
91
|
-
kind: "class",
|
|
92
|
-
node
|
|
93
|
-
};
|
|
94
|
-
case AST_NODE_TYPES.NewExpression:
|
|
95
|
-
case AST_NODE_TYPES.ThisExpression: return {
|
|
96
|
-
kind: "instance",
|
|
97
|
-
node
|
|
98
|
-
};
|
|
99
|
-
case AST_NODE_TYPES.FunctionDeclaration:
|
|
100
|
-
case AST_NODE_TYPES.FunctionExpression:
|
|
101
|
-
case AST_NODE_TYPES.ArrowFunctionExpression: return {
|
|
102
|
-
kind: "function",
|
|
103
|
-
node
|
|
104
|
-
};
|
|
105
|
-
case AST_NODE_TYPES.Literal:
|
|
106
|
-
if ("regex" in node) return {
|
|
107
|
-
kind: "regexp",
|
|
108
|
-
node
|
|
109
|
-
};
|
|
110
|
-
return null;
|
|
111
|
-
case AST_NODE_TYPES.Identifier: {
|
|
112
|
-
if ((context.sourceCode.getScope(node).set.get(node.name)?.defs.at(-1))?.type === DefinitionType.Parameter) return null;
|
|
113
|
-
const initNode = resolve(context, node, {
|
|
114
|
-
at: -1,
|
|
115
|
-
localOnly: true
|
|
116
|
-
});
|
|
117
|
-
if (initNode == null) return null;
|
|
118
|
-
return computeObjectType(context, initNode);
|
|
119
|
-
}
|
|
120
|
-
case AST_NODE_TYPES.MemberExpression: return computeObjectType(context, node.object);
|
|
121
|
-
case AST_NODE_TYPES.AssignmentExpression:
|
|
122
|
-
case AST_NODE_TYPES.AssignmentPattern: return computeObjectType(context, node.right);
|
|
123
|
-
case AST_NODE_TYPES.LogicalExpression: return computeObjectType(context, node.left) ?? computeObjectType(context, node.right);
|
|
124
|
-
case AST_NODE_TYPES.ConditionalExpression: return computeObjectType(context, node.consequent) ?? computeObjectType(context, node.alternate);
|
|
125
|
-
case AST_NODE_TYPES.SequenceExpression:
|
|
126
|
-
if (node.expressions.length === 0) return null;
|
|
127
|
-
return computeObjectType(context, node.expressions[node.expressions.length - 1] ?? null);
|
|
128
|
-
case AST_NODE_TYPES.CallExpression: {
|
|
129
|
-
const callee = Extract.unwrap(node.callee);
|
|
130
|
-
switch (true) {
|
|
131
|
-
case Check.isIdentifier(callee, "Boolean"): return null;
|
|
132
|
-
case Check.isIdentifier(callee, "String"): return null;
|
|
133
|
-
case Check.isIdentifier(callee, "Number"): return null;
|
|
134
|
-
case Check.isIdentifier(callee, "Object"): return {
|
|
135
|
-
kind: "plain",
|
|
136
|
-
node
|
|
137
|
-
};
|
|
138
|
-
case Check.isIdentifier(callee, "Array"): return {
|
|
139
|
-
kind: "array",
|
|
140
|
-
node
|
|
141
|
-
};
|
|
142
|
-
case Check.isIdentifier(callee, "RegExp"): return {
|
|
143
|
-
kind: "regexp",
|
|
144
|
-
node
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && callee.object.type === AST_NODE_TYPES.Identifier) {
|
|
148
|
-
const objName = callee.object.name;
|
|
149
|
-
const methodName = Extract.getCalleeName(node);
|
|
150
|
-
switch (objName) {
|
|
151
|
-
case "Array":
|
|
152
|
-
if (methodName === "from" || methodName === "of") return {
|
|
153
|
-
kind: "array",
|
|
154
|
-
node
|
|
155
|
-
};
|
|
156
|
-
break;
|
|
157
|
-
case "Object":
|
|
158
|
-
if (methodName === "create" || methodName === "assign" || methodName === "fromEntries") return {
|
|
159
|
-
kind: "plain",
|
|
160
|
-
node
|
|
161
|
-
};
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
kind: "unknown",
|
|
167
|
-
node,
|
|
168
|
-
reason: "call-expression"
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
default:
|
|
172
|
-
if (!("expression" in node) || typeof node.expression !== "object") return null;
|
|
173
|
-
return computeObjectType(context, node.expression);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
//#endregion
|
|
178
|
-
//#region src/get-require-expression-arguments.ts
|
|
179
|
-
/**
|
|
180
|
-
* Get the arguments of a require expression.
|
|
181
|
-
* @param node The node to match.
|
|
182
|
-
* @returns The require expression arguments or null if the node is not a require expression.
|
|
183
|
-
* @internal
|
|
184
|
-
*/
|
|
185
|
-
function getRequireExpressionArguments(node) {
|
|
186
|
-
const unwrapped = Extract.unwrap(node);
|
|
187
|
-
if (unwrapped.type === AST_NODE_TYPES.CallExpression) {
|
|
188
|
-
const callee = Extract.unwrap(unwrapped.callee);
|
|
189
|
-
if (callee.type === AST_NODE_TYPES.Identifier && callee.name === "require") return unwrapped.arguments;
|
|
190
|
-
}
|
|
191
|
-
if (unwrapped.type === AST_NODE_TYPES.MemberExpression) return getRequireExpressionArguments(unwrapped.object);
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
84
|
//#endregion
|
|
196
85
|
//#region src/is-value-equal.ts
|
|
197
86
|
const thisBlockTypes = [
|
|
@@ -350,4 +239,115 @@ function resolveEnclosingAssignmentTarget(node) {
|
|
|
350
239
|
}
|
|
351
240
|
|
|
352
241
|
//#endregion
|
|
353
|
-
|
|
242
|
+
//#region src/resolve-object-type.ts
|
|
243
|
+
/**
|
|
244
|
+
* Detect the ObjectType of a given node.
|
|
245
|
+
* @param context The context of the rule.
|
|
246
|
+
* @param node The node to check.
|
|
247
|
+
* @returns The ObjectType of the node, or undefined if not detected.
|
|
248
|
+
*/
|
|
249
|
+
function resolveObjectType(context, node) {
|
|
250
|
+
if (node == null) return null;
|
|
251
|
+
switch (node.type) {
|
|
252
|
+
case AST_NODE_TYPES.JSXElement:
|
|
253
|
+
case AST_NODE_TYPES.JSXFragment: return {
|
|
254
|
+
kind: "jsx",
|
|
255
|
+
node
|
|
256
|
+
};
|
|
257
|
+
case AST_NODE_TYPES.ArrayExpression: return {
|
|
258
|
+
kind: "array",
|
|
259
|
+
node
|
|
260
|
+
};
|
|
261
|
+
case AST_NODE_TYPES.ObjectExpression: return {
|
|
262
|
+
kind: "plain",
|
|
263
|
+
node
|
|
264
|
+
};
|
|
265
|
+
case AST_NODE_TYPES.ClassExpression: return {
|
|
266
|
+
kind: "class",
|
|
267
|
+
node
|
|
268
|
+
};
|
|
269
|
+
case AST_NODE_TYPES.NewExpression:
|
|
270
|
+
case AST_NODE_TYPES.ThisExpression: return {
|
|
271
|
+
kind: "instance",
|
|
272
|
+
node
|
|
273
|
+
};
|
|
274
|
+
case AST_NODE_TYPES.FunctionDeclaration:
|
|
275
|
+
case AST_NODE_TYPES.FunctionExpression:
|
|
276
|
+
case AST_NODE_TYPES.ArrowFunctionExpression: return {
|
|
277
|
+
kind: "function",
|
|
278
|
+
node
|
|
279
|
+
};
|
|
280
|
+
case AST_NODE_TYPES.Literal:
|
|
281
|
+
if ("regex" in node) return {
|
|
282
|
+
kind: "regexp",
|
|
283
|
+
node
|
|
284
|
+
};
|
|
285
|
+
return null;
|
|
286
|
+
case AST_NODE_TYPES.Identifier: {
|
|
287
|
+
if ((context.sourceCode.getScope(node).set.get(node.name)?.defs.at(-1))?.type === DefinitionType.Parameter) return null;
|
|
288
|
+
const initNode = resolve(context, node, {
|
|
289
|
+
at: -1,
|
|
290
|
+
localOnly: true
|
|
291
|
+
});
|
|
292
|
+
if (initNode == null) return null;
|
|
293
|
+
return resolveObjectType(context, initNode);
|
|
294
|
+
}
|
|
295
|
+
case AST_NODE_TYPES.MemberExpression: return resolveObjectType(context, node.object);
|
|
296
|
+
case AST_NODE_TYPES.AssignmentExpression:
|
|
297
|
+
case AST_NODE_TYPES.AssignmentPattern: return resolveObjectType(context, node.right);
|
|
298
|
+
case AST_NODE_TYPES.LogicalExpression: return resolveObjectType(context, node.left) ?? resolveObjectType(context, node.right);
|
|
299
|
+
case AST_NODE_TYPES.ConditionalExpression: return resolveObjectType(context, node.consequent) ?? resolveObjectType(context, node.alternate);
|
|
300
|
+
case AST_NODE_TYPES.SequenceExpression:
|
|
301
|
+
if (node.expressions.length === 0) return null;
|
|
302
|
+
return resolveObjectType(context, node.expressions[node.expressions.length - 1] ?? null);
|
|
303
|
+
case AST_NODE_TYPES.CallExpression: {
|
|
304
|
+
const callee = Extract.unwrap(node.callee);
|
|
305
|
+
switch (true) {
|
|
306
|
+
case Check.isIdentifier(callee, "Boolean"): return null;
|
|
307
|
+
case Check.isIdentifier(callee, "String"): return null;
|
|
308
|
+
case Check.isIdentifier(callee, "Number"): return null;
|
|
309
|
+
case Check.isIdentifier(callee, "Object"): return {
|
|
310
|
+
kind: "plain",
|
|
311
|
+
node
|
|
312
|
+
};
|
|
313
|
+
case Check.isIdentifier(callee, "Array"): return {
|
|
314
|
+
kind: "array",
|
|
315
|
+
node
|
|
316
|
+
};
|
|
317
|
+
case Check.isIdentifier(callee, "RegExp"): return {
|
|
318
|
+
kind: "regexp",
|
|
319
|
+
node
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
if (callee.type === AST_NODE_TYPES.MemberExpression && callee.object.type === AST_NODE_TYPES.Identifier) {
|
|
323
|
+
const objName = callee.object.name;
|
|
324
|
+
const methodName = Extract.getCalleeName(node);
|
|
325
|
+
switch (objName) {
|
|
326
|
+
case "Array":
|
|
327
|
+
if (methodName === "from" || methodName === "of") return {
|
|
328
|
+
kind: "array",
|
|
329
|
+
node
|
|
330
|
+
};
|
|
331
|
+
break;
|
|
332
|
+
case "Object":
|
|
333
|
+
if (methodName === "create" || methodName === "assign" || methodName === "fromEntries") return {
|
|
334
|
+
kind: "plain",
|
|
335
|
+
node
|
|
336
|
+
};
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
kind: "unknown",
|
|
342
|
+
node,
|
|
343
|
+
reason: "call-expression"
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
default:
|
|
347
|
+
if (!("expression" in node) || typeof node.expression !== "object") return null;
|
|
348
|
+
return resolveObjectType(context, node.expression);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
//#endregion
|
|
353
|
+
export { getRequireExpressionArguments, isAssignmentTargetEqual, isInitializedFromReact, isInitializedFromReactNative, isValueEqual, resolve, resolveEnclosingAssignmentTarget, resolveImportSource, resolveObjectType };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/var",
|
|
3
|
-
"version": "5.17.
|
|
3
|
+
"version": "5.17.2",
|
|
4
4
|
"description": "ESLint React's TSESTree AST utility module for static analysis of variables.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -33,18 +33,19 @@
|
|
|
33
33
|
"@typescript-eslint/types": "^8.64.0",
|
|
34
34
|
"@typescript-eslint/utils": "^8.64.0",
|
|
35
35
|
"ts-pattern": "^5.9.0",
|
|
36
|
-
"@eslint-react/ast": "5.17.
|
|
37
|
-
"@eslint-react/eslint": "5.17.
|
|
36
|
+
"@eslint-react/ast": "5.17.2",
|
|
37
|
+
"@eslint-react/eslint": "5.17.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@typescript-eslint/parser": "^8.64.0",
|
|
41
41
|
"@typescript-eslint/typescript-estree": "^8.64.0",
|
|
42
42
|
"eslint": "^10.7.0",
|
|
43
|
-
"tsdown": "^0.22.
|
|
43
|
+
"tsdown": "^0.22.9",
|
|
44
44
|
"typescript": "6.0.3",
|
|
45
45
|
"vitest": "^4.1.10",
|
|
46
|
-
"@local/
|
|
47
|
-
"@local/
|
|
46
|
+
"@local/eff": "0.0.0",
|
|
47
|
+
"@local/testkit": "0.0.0",
|
|
48
|
+
"@local/configs": "0.0.0"
|
|
48
49
|
},
|
|
49
50
|
"peerDependencies": {
|
|
50
51
|
"eslint": "*",
|