@eslint-react/var 2.0.0-next.0 → 2.0.0-next.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.js +73 -104
- package/package.json +6 -14
- package/dist/index.d.mts +0 -102
- package/dist/index.mjs +0 -256
package/dist/index.js
CHANGED
|
@@ -1,46 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var AST = require('@eslint-react/ast');
|
|
8
|
-
|
|
9
|
-
function _interopNamespace(e) {
|
|
10
|
-
if (e && e.__esModule) return e;
|
|
11
|
-
var n = Object.create(null);
|
|
12
|
-
if (e) {
|
|
13
|
-
Object.keys(e).forEach(function (k) {
|
|
14
|
-
if (k !== 'default') {
|
|
15
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return e[k]; }
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
n.default = e;
|
|
24
|
-
return Object.freeze(n);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var ASTUtils__namespace = /*#__PURE__*/_interopNamespace(ASTUtils);
|
|
28
|
-
var AST__namespace = /*#__PURE__*/_interopNamespace(AST);
|
|
1
|
+
import { dual, _ } from '@eslint-react/eff';
|
|
2
|
+
import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
|
|
3
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
4
|
+
import * as ASTUtils from '@typescript-eslint/utils/ast-utils';
|
|
5
|
+
import { getStaticValue } from '@typescript-eslint/utils/ast-utils';
|
|
6
|
+
import * as AST from '@eslint-react/ast';
|
|
29
7
|
|
|
30
8
|
// src/var-collect.ts
|
|
31
9
|
function getVariableInitNode(variable, at) {
|
|
32
|
-
if (variable == null) return
|
|
10
|
+
if (variable == null) return _;
|
|
33
11
|
const def = variable.defs.at(at);
|
|
34
|
-
if (def == null) return
|
|
12
|
+
if (def == null) return _;
|
|
35
13
|
switch (true) {
|
|
36
|
-
case (def.type ===
|
|
14
|
+
case (def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration):
|
|
37
15
|
return def.node;
|
|
38
|
-
case (def.type ===
|
|
16
|
+
case (def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration):
|
|
39
17
|
return def.node;
|
|
40
18
|
case ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)):
|
|
41
19
|
return def.node.init;
|
|
42
20
|
default:
|
|
43
|
-
return
|
|
21
|
+
return _;
|
|
44
22
|
}
|
|
45
23
|
}
|
|
46
24
|
|
|
@@ -48,28 +26,28 @@ function getVariableInitNode(variable, at) {
|
|
|
48
26
|
function getVariables(initialScope) {
|
|
49
27
|
let scope = initialScope;
|
|
50
28
|
const variables = [...scope.variables];
|
|
51
|
-
while (scope.type !==
|
|
29
|
+
while (scope.type !== ScopeType.global) {
|
|
52
30
|
scope = scope.upper;
|
|
53
31
|
variables.push(...scope.variables);
|
|
54
32
|
}
|
|
55
33
|
return variables.reverse();
|
|
56
34
|
}
|
|
57
|
-
var findVariable2 =
|
|
58
|
-
if (nameOrNode == null) return
|
|
59
|
-
return
|
|
35
|
+
var findVariable2 = dual(2, (nameOrNode, initialScope) => {
|
|
36
|
+
if (nameOrNode == null) return _;
|
|
37
|
+
return ASTUtils.findVariable(initialScope, nameOrNode) ?? _;
|
|
60
38
|
});
|
|
61
39
|
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
62
40
|
return properties.findLast((prop) => {
|
|
63
|
-
if (prop.type ===
|
|
41
|
+
if (prop.type === AST_NODE_TYPES.Property) {
|
|
64
42
|
return "name" in prop.key && prop.key.name === name;
|
|
65
43
|
}
|
|
66
|
-
if (prop.type ===
|
|
44
|
+
if (prop.type === AST_NODE_TYPES.SpreadElement) {
|
|
67
45
|
switch (prop.argument.type) {
|
|
68
|
-
case
|
|
46
|
+
case AST_NODE_TYPES.Identifier: {
|
|
69
47
|
if (seen.has(prop.argument.name)) return false;
|
|
70
48
|
const variable = findVariable2(prop.argument.name, initialScope);
|
|
71
49
|
const variableNode = getVariableInitNode(variable, 0);
|
|
72
|
-
if (variableNode?.type ===
|
|
50
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
73
51
|
seen.add(prop.argument.name);
|
|
74
52
|
return findPropertyInProperties(
|
|
75
53
|
name,
|
|
@@ -80,7 +58,7 @@ function findPropertyInProperties(name, properties, initialScope, seen = /* @__P
|
|
|
80
58
|
}
|
|
81
59
|
return false;
|
|
82
60
|
}
|
|
83
|
-
case
|
|
61
|
+
case AST_NODE_TYPES.ObjectExpression: {
|
|
84
62
|
return findPropertyInProperties(
|
|
85
63
|
name,
|
|
86
64
|
prop.argument.properties,
|
|
@@ -100,78 +78,78 @@ var ConstructionDetectionHint = {
|
|
|
100
78
|
StrictCallExpression: 1n << 0n
|
|
101
79
|
};
|
|
102
80
|
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
103
|
-
if (node == null) return
|
|
81
|
+
if (node == null) return _;
|
|
104
82
|
switch (node.type) {
|
|
105
|
-
case
|
|
106
|
-
case
|
|
83
|
+
case AST_NODE_TYPES.JSXElement:
|
|
84
|
+
case AST_NODE_TYPES.JSXFragment:
|
|
107
85
|
return { kind: "JSXElement", node };
|
|
108
|
-
case
|
|
86
|
+
case AST_NODE_TYPES.ArrayExpression:
|
|
109
87
|
return { kind: "ArrayExpression", node };
|
|
110
|
-
case
|
|
88
|
+
case AST_NODE_TYPES.ObjectExpression:
|
|
111
89
|
return { kind: "ObjectExpression", node };
|
|
112
|
-
case
|
|
90
|
+
case AST_NODE_TYPES.ClassExpression:
|
|
113
91
|
return { kind: "ClassExpression", node };
|
|
114
|
-
case
|
|
92
|
+
case AST_NODE_TYPES.NewExpression:
|
|
115
93
|
return { kind: "NewExpression", node };
|
|
116
|
-
case
|
|
117
|
-
case
|
|
94
|
+
case AST_NODE_TYPES.FunctionExpression:
|
|
95
|
+
case AST_NODE_TYPES.ArrowFunctionExpression:
|
|
118
96
|
return { kind: "FunctionExpression", node };
|
|
119
|
-
case
|
|
97
|
+
case AST_NODE_TYPES.CallExpression: {
|
|
120
98
|
if (hint & ConstructionDetectionHint.StrictCallExpression) {
|
|
121
99
|
return { kind: "CallExpression", node };
|
|
122
100
|
}
|
|
123
|
-
return
|
|
101
|
+
return _;
|
|
124
102
|
}
|
|
125
|
-
case
|
|
126
|
-
if (!("object" in node)) return
|
|
103
|
+
case AST_NODE_TYPES.MemberExpression: {
|
|
104
|
+
if (!("object" in node)) return _;
|
|
127
105
|
return getConstruction(node.object, initialScope, hint);
|
|
128
106
|
}
|
|
129
|
-
case
|
|
130
|
-
case
|
|
131
|
-
if (!("right" in node)) return
|
|
107
|
+
case AST_NODE_TYPES.AssignmentExpression:
|
|
108
|
+
case AST_NODE_TYPES.AssignmentPattern: {
|
|
109
|
+
if (!("right" in node)) return _;
|
|
132
110
|
return getConstruction(node.right, initialScope, hint);
|
|
133
111
|
}
|
|
134
|
-
case
|
|
112
|
+
case AST_NODE_TYPES.LogicalExpression: {
|
|
135
113
|
const lvc = getConstruction(node.left, initialScope, hint);
|
|
136
|
-
if (lvc == null) return
|
|
114
|
+
if (lvc == null) return _;
|
|
137
115
|
return getConstruction(node.right, initialScope, hint);
|
|
138
116
|
}
|
|
139
|
-
case
|
|
117
|
+
case AST_NODE_TYPES.ConditionalExpression: {
|
|
140
118
|
const cvc = getConstruction(node.consequent, initialScope, hint);
|
|
141
|
-
if (cvc == null) return
|
|
119
|
+
if (cvc == null) return _;
|
|
142
120
|
return getConstruction(node.alternate, initialScope, hint);
|
|
143
121
|
}
|
|
144
|
-
case
|
|
122
|
+
case AST_NODE_TYPES.Identifier: {
|
|
145
123
|
if (!("name" in node) || typeof node.name !== "string") {
|
|
146
|
-
return
|
|
124
|
+
return _;
|
|
147
125
|
}
|
|
148
126
|
const variable = initialScope.set.get(node.name);
|
|
149
127
|
const variableNode = getVariableInitNode(variable, -1);
|
|
150
128
|
return getConstruction(variableNode, initialScope, hint);
|
|
151
129
|
}
|
|
152
|
-
case
|
|
130
|
+
case AST_NODE_TYPES.Literal: {
|
|
153
131
|
if ("regex" in node) {
|
|
154
132
|
return { kind: "RegExpLiteral", node };
|
|
155
133
|
}
|
|
156
|
-
return
|
|
134
|
+
return _;
|
|
157
135
|
}
|
|
158
136
|
default: {
|
|
159
137
|
if (!("expression" in node) || typeof node.expression !== "object") {
|
|
160
|
-
return
|
|
138
|
+
return _;
|
|
161
139
|
}
|
|
162
140
|
return getConstruction(node.expression, initialScope, hint);
|
|
163
141
|
}
|
|
164
142
|
}
|
|
165
143
|
}
|
|
166
144
|
function getVariableDeclaratorId(node, prev) {
|
|
167
|
-
if (node == null) return
|
|
145
|
+
if (node == null) return _;
|
|
168
146
|
switch (true) {
|
|
169
|
-
case (node.type ===
|
|
147
|
+
case (node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev):
|
|
170
148
|
return node.id;
|
|
171
|
-
case (node.type ===
|
|
149
|
+
case (node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev):
|
|
172
150
|
return node.left;
|
|
173
|
-
case (node.type ===
|
|
174
|
-
return
|
|
151
|
+
case (node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node):
|
|
152
|
+
return _;
|
|
175
153
|
default:
|
|
176
154
|
return getVariableDeclaratorId(node.parent, node);
|
|
177
155
|
}
|
|
@@ -190,14 +168,14 @@ function toStaticValue(lazyValue) {
|
|
|
190
168
|
if (kind !== "lazy") {
|
|
191
169
|
return lazyValue;
|
|
192
170
|
}
|
|
193
|
-
const staticValue = initialScope == null ?
|
|
171
|
+
const staticValue = initialScope == null ? getStaticValue(node) : getStaticValue(node, initialScope);
|
|
194
172
|
return staticValue == null ? { kind: "none", node, initialScope } : { kind: "some", node, initialScope, value: staticValue.value };
|
|
195
173
|
}
|
|
196
174
|
var thisBlockTypes = [
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
175
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
176
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
177
|
+
AST_NODE_TYPES.ClassBody,
|
|
178
|
+
AST_NODE_TYPES.Program
|
|
201
179
|
];
|
|
202
180
|
function isNodeValueEqual(a, b, initialScopes) {
|
|
203
181
|
const [aScope, bScope] = initialScopes;
|
|
@@ -205,13 +183,13 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
205
183
|
case a === b: {
|
|
206
184
|
return true;
|
|
207
185
|
}
|
|
208
|
-
case (a.type ===
|
|
186
|
+
case (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal): {
|
|
209
187
|
return a.value === b.value;
|
|
210
188
|
}
|
|
211
|
-
case (a.type ===
|
|
189
|
+
case (a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement): {
|
|
212
190
|
return a.value.cooked === b.value.cooked;
|
|
213
191
|
}
|
|
214
|
-
case (a.type ===
|
|
192
|
+
case (a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier): {
|
|
215
193
|
const aVar = findVariable2(a, aScope);
|
|
216
194
|
const bVar = findVariable2(b, bScope);
|
|
217
195
|
const aVarNode = getVariableInitNodeLoose(aVar, 0);
|
|
@@ -223,17 +201,17 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
223
201
|
const aDefParentParent = aDef?.parent?.parent;
|
|
224
202
|
const bDefParentParent = bDef?.parent?.parent;
|
|
225
203
|
switch (true) {
|
|
226
|
-
case (aVarNodeParent?.type ===
|
|
227
|
-
if (!
|
|
204
|
+
case (aVarNodeParent?.type === AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === AST_NODE_TYPES.CallExpression && AST.isFunction(aVarNode) && AST.isFunction(bVarNode)): {
|
|
205
|
+
if (!AST.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) {
|
|
228
206
|
return false;
|
|
229
207
|
}
|
|
230
208
|
const aParams = aVarNode.params;
|
|
231
209
|
const bParams = bVarNode.params;
|
|
232
|
-
const aPos = aParams.findIndex((x) =>
|
|
233
|
-
const bPos = bParams.findIndex((x) =>
|
|
210
|
+
const aPos = aParams.findIndex((x) => AST.isNodeEqual(x, a));
|
|
211
|
+
const bPos = bParams.findIndex((x) => AST.isNodeEqual(x, b));
|
|
234
212
|
return aPos !== -1 && bPos !== -1 && aPos === bPos;
|
|
235
213
|
}
|
|
236
|
-
case (aDefParentParent?.type ===
|
|
214
|
+
case (aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement): {
|
|
237
215
|
const aLeft = aDefParentParent.left;
|
|
238
216
|
const bLeft = bDefParentParent.left;
|
|
239
217
|
if (aLeft.type !== bLeft.type) {
|
|
@@ -241,22 +219,22 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
241
219
|
}
|
|
242
220
|
const aRight = aDefParentParent.right;
|
|
243
221
|
const bRight = bDefParentParent.right;
|
|
244
|
-
return
|
|
222
|
+
return AST.isNodeEqual(aRight, bRight);
|
|
245
223
|
}
|
|
246
224
|
default: {
|
|
247
225
|
return aVar != null && bVar != null && aVar === bVar;
|
|
248
226
|
}
|
|
249
227
|
}
|
|
250
228
|
}
|
|
251
|
-
case (a.type ===
|
|
252
|
-
return
|
|
229
|
+
case (a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression): {
|
|
230
|
+
return AST.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes);
|
|
253
231
|
}
|
|
254
|
-
case (a.type ===
|
|
232
|
+
case (a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression): {
|
|
255
233
|
if (aScope.block === bScope.block) {
|
|
256
234
|
return true;
|
|
257
235
|
}
|
|
258
|
-
const aFunction =
|
|
259
|
-
const bFunction =
|
|
236
|
+
const aFunction = AST.findParentNode(a, AST.isOneOf(thisBlockTypes));
|
|
237
|
+
const bFunction = AST.findParentNode(b, AST.isOneOf(thisBlockTypes));
|
|
260
238
|
return aFunction === bFunction;
|
|
261
239
|
}
|
|
262
240
|
default: {
|
|
@@ -267,21 +245,12 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
267
245
|
}
|
|
268
246
|
}
|
|
269
247
|
function getVariableInitNodeLoose(variable, at) {
|
|
270
|
-
if (variable == null) return
|
|
248
|
+
if (variable == null) return _;
|
|
271
249
|
const node = getVariableInitNode(variable, at);
|
|
272
250
|
if (node != null) return node;
|
|
273
251
|
const def = variable.defs.at(at);
|
|
274
|
-
if (def?.type ===
|
|
275
|
-
return
|
|
252
|
+
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
|
|
253
|
+
return _;
|
|
276
254
|
}
|
|
277
255
|
|
|
278
|
-
|
|
279
|
-
exports.findPropertyInProperties = findPropertyInProperties;
|
|
280
|
-
exports.findVariable = findVariable2;
|
|
281
|
-
exports.getChidScopes = getChidScopes;
|
|
282
|
-
exports.getConstruction = getConstruction;
|
|
283
|
-
exports.getVariableDeclaratorId = getVariableDeclaratorId;
|
|
284
|
-
exports.getVariableInitNode = getVariableInitNode;
|
|
285
|
-
exports.getVariables = getVariables;
|
|
286
|
-
exports.isNodeValueEqual = isNodeValueEqual;
|
|
287
|
-
exports.toStaticValue = toStaticValue;
|
|
256
|
+
export { ConstructionDetectionHint, findPropertyInProperties, findVariable2 as findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/var",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.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": {
|
|
@@ -14,22 +14,14 @@
|
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"author": "Rel1cx<rel1cx@proton.me>",
|
|
16
16
|
"sideEffects": false,
|
|
17
|
+
"type": "module",
|
|
17
18
|
"exports": {
|
|
18
19
|
".": {
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"default": "./dist/index.mjs"
|
|
22
|
-
},
|
|
23
|
-
"require": {
|
|
24
|
-
"types": "./dist/index.d.ts",
|
|
25
|
-
"default": "./dist/index.js"
|
|
26
|
-
}
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
27
22
|
},
|
|
28
23
|
"./package.json": "./package.json"
|
|
29
24
|
},
|
|
30
|
-
"main": "dist/index.js",
|
|
31
|
-
"module": "dist/index.mjs",
|
|
32
|
-
"types": "dist/index.d.ts",
|
|
33
25
|
"files": [
|
|
34
26
|
"dist",
|
|
35
27
|
"./package.json"
|
|
@@ -40,8 +32,8 @@
|
|
|
40
32
|
"@typescript-eslint/utils": "^8.30.1",
|
|
41
33
|
"string-ts": "^2.2.1",
|
|
42
34
|
"ts-pattern": "^5.7.0",
|
|
43
|
-
"@eslint-react/
|
|
44
|
-
"@eslint-react/
|
|
35
|
+
"@eslint-react/ast": "2.0.0-next.2",
|
|
36
|
+
"@eslint-react/eff": "2.0.0-next.2"
|
|
45
37
|
},
|
|
46
38
|
"devDependencies": {
|
|
47
39
|
"tsup": "^8.4.0",
|
package/dist/index.d.mts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { Scope, Variable } from '@typescript-eslint/scope-manager';
|
|
2
|
-
import { TSESTree } from '@typescript-eslint/types';
|
|
3
|
-
import { _ } from '@eslint-react/eff';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Get all variables from the given scope up to the global scope
|
|
7
|
-
* @param initialScope The scope to start from
|
|
8
|
-
* @returns All variables from the given scope up to the global scope
|
|
9
|
-
*/
|
|
10
|
-
declare function getVariables(initialScope: Scope): Variable[];
|
|
11
|
-
declare const findVariable: {
|
|
12
|
-
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | _) => Variable | _;
|
|
13
|
-
(nameOrNode: string | TSESTree.Identifier | _, initialScope: Scope): Variable | _;
|
|
14
|
-
};
|
|
15
|
-
declare function findPropertyInProperties(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | _;
|
|
16
|
-
|
|
17
|
-
declare const ConstructionDetectionHint: {
|
|
18
|
-
None: bigint;
|
|
19
|
-
StrictCallExpression: bigint;
|
|
20
|
-
};
|
|
21
|
-
type Construction = {
|
|
22
|
-
kind: "ArrayExpression";
|
|
23
|
-
node: TSESTree.ArrayExpression;
|
|
24
|
-
} | {
|
|
25
|
-
kind: "CallExpression";
|
|
26
|
-
node: TSESTree.CallExpression;
|
|
27
|
-
} | {
|
|
28
|
-
kind: "ClassExpression";
|
|
29
|
-
node: TSESTree.ClassExpression;
|
|
30
|
-
} | {
|
|
31
|
-
kind: "FunctionDeclaration";
|
|
32
|
-
node: TSESTree.FunctionDeclaration;
|
|
33
|
-
} | {
|
|
34
|
-
kind: "FunctionExpression";
|
|
35
|
-
node: TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
36
|
-
} | {
|
|
37
|
-
kind: "JSXElement";
|
|
38
|
-
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
39
|
-
} | {
|
|
40
|
-
kind: "NewExpression";
|
|
41
|
-
node: TSESTree.NewExpression;
|
|
42
|
-
} | {
|
|
43
|
-
kind: "ObjectExpression";
|
|
44
|
-
node: TSESTree.ObjectExpression;
|
|
45
|
-
} | {
|
|
46
|
-
kind: "RegExpLiteral";
|
|
47
|
-
node: TSESTree.RegExpLiteral;
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Detects the construction type of a given node.
|
|
51
|
-
* @param node The node to check.
|
|
52
|
-
* @param initialScope The initial scope to check for variable declarations.
|
|
53
|
-
* @param hint Optional hint to control the detection behavior.
|
|
54
|
-
* @returns The construction type of the node, or `_` if not found.
|
|
55
|
-
*/
|
|
56
|
-
declare function getConstruction(node: TSESTree.Node | _, initialScope: Scope, hint?: bigint): Construction | _;
|
|
57
|
-
|
|
58
|
-
declare function getVariableDeclaratorId(node: TSESTree.Node | _, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | _;
|
|
59
|
-
|
|
60
|
-
declare function getVariableInitNode(variable: Variable | _, at: number): _ | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
61
|
-
|
|
62
|
-
declare function getChidScopes(scope: Scope): readonly Scope[];
|
|
63
|
-
|
|
64
|
-
type LazyValue = {
|
|
65
|
-
kind: "lazy";
|
|
66
|
-
node: TSESTree.Node;
|
|
67
|
-
initialScope: Scope | _;
|
|
68
|
-
} | {
|
|
69
|
-
kind: "none";
|
|
70
|
-
node: TSESTree.Node;
|
|
71
|
-
initialScope: Scope | _;
|
|
72
|
-
} | {
|
|
73
|
-
kind: "some";
|
|
74
|
-
node: TSESTree.Node;
|
|
75
|
-
value: unknown;
|
|
76
|
-
initialScope: Scope | _;
|
|
77
|
-
};
|
|
78
|
-
declare function toStaticValue(lazyValue: LazyValue): {
|
|
79
|
-
readonly kind: "none";
|
|
80
|
-
readonly node: TSESTree.Node;
|
|
81
|
-
readonly initialScope: Scope | undefined;
|
|
82
|
-
readonly value?: never;
|
|
83
|
-
} | {
|
|
84
|
-
readonly kind: "some";
|
|
85
|
-
readonly node: TSESTree.Node;
|
|
86
|
-
readonly initialScope: Scope | undefined;
|
|
87
|
-
readonly value: unknown;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Determines whether node value equals to another node value
|
|
92
|
-
* @param a node to compare
|
|
93
|
-
* @param b node to compare
|
|
94
|
-
* @param initialScopes initial scopes of the two nodes
|
|
95
|
-
* @returns `true` if node value equal
|
|
96
|
-
*/
|
|
97
|
-
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
98
|
-
aScope: Scope,
|
|
99
|
-
bScope: Scope
|
|
100
|
-
]): boolean;
|
|
101
|
-
|
|
102
|
-
export { type Construction, ConstructionDetectionHint, type LazyValue, findPropertyInProperties, findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/dist/index.mjs
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
import { dual, _ } from '@eslint-react/eff';
|
|
2
|
-
import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
|
|
3
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
4
|
-
import * as ASTUtils from '@typescript-eslint/utils/ast-utils';
|
|
5
|
-
import { getStaticValue } from '@typescript-eslint/utils/ast-utils';
|
|
6
|
-
import * as AST from '@eslint-react/ast';
|
|
7
|
-
|
|
8
|
-
// src/var-collect.ts
|
|
9
|
-
function getVariableInitNode(variable, at) {
|
|
10
|
-
if (variable == null) return _;
|
|
11
|
-
const def = variable.defs.at(at);
|
|
12
|
-
if (def == null) return _;
|
|
13
|
-
switch (true) {
|
|
14
|
-
case (def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration):
|
|
15
|
-
return def.node;
|
|
16
|
-
case (def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration):
|
|
17
|
-
return def.node;
|
|
18
|
-
case ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)):
|
|
19
|
-
return def.node.init;
|
|
20
|
-
default:
|
|
21
|
-
return _;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// src/var-collect.ts
|
|
26
|
-
function getVariables(initialScope) {
|
|
27
|
-
let scope = initialScope;
|
|
28
|
-
const variables = [...scope.variables];
|
|
29
|
-
while (scope.type !== ScopeType.global) {
|
|
30
|
-
scope = scope.upper;
|
|
31
|
-
variables.push(...scope.variables);
|
|
32
|
-
}
|
|
33
|
-
return variables.reverse();
|
|
34
|
-
}
|
|
35
|
-
var findVariable2 = dual(2, (nameOrNode, initialScope) => {
|
|
36
|
-
if (nameOrNode == null) return _;
|
|
37
|
-
return ASTUtils.findVariable(initialScope, nameOrNode) ?? _;
|
|
38
|
-
});
|
|
39
|
-
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
40
|
-
return properties.findLast((prop) => {
|
|
41
|
-
if (prop.type === AST_NODE_TYPES.Property) {
|
|
42
|
-
return "name" in prop.key && prop.key.name === name;
|
|
43
|
-
}
|
|
44
|
-
if (prop.type === AST_NODE_TYPES.SpreadElement) {
|
|
45
|
-
switch (prop.argument.type) {
|
|
46
|
-
case AST_NODE_TYPES.Identifier: {
|
|
47
|
-
if (seen.has(prop.argument.name)) return false;
|
|
48
|
-
const variable = findVariable2(prop.argument.name, initialScope);
|
|
49
|
-
const variableNode = getVariableInitNode(variable, 0);
|
|
50
|
-
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
51
|
-
seen.add(prop.argument.name);
|
|
52
|
-
return findPropertyInProperties(
|
|
53
|
-
name,
|
|
54
|
-
variableNode.properties,
|
|
55
|
-
initialScope,
|
|
56
|
-
seen
|
|
57
|
-
) != null;
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
case AST_NODE_TYPES.ObjectExpression: {
|
|
62
|
-
return findPropertyInProperties(
|
|
63
|
-
name,
|
|
64
|
-
prop.argument.properties,
|
|
65
|
-
initialScope,
|
|
66
|
-
seen
|
|
67
|
-
) != null;
|
|
68
|
-
}
|
|
69
|
-
default:
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return false;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
var ConstructionDetectionHint = {
|
|
77
|
-
None: 0n,
|
|
78
|
-
StrictCallExpression: 1n << 0n
|
|
79
|
-
};
|
|
80
|
-
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
81
|
-
if (node == null) return _;
|
|
82
|
-
switch (node.type) {
|
|
83
|
-
case AST_NODE_TYPES.JSXElement:
|
|
84
|
-
case AST_NODE_TYPES.JSXFragment:
|
|
85
|
-
return { kind: "JSXElement", node };
|
|
86
|
-
case AST_NODE_TYPES.ArrayExpression:
|
|
87
|
-
return { kind: "ArrayExpression", node };
|
|
88
|
-
case AST_NODE_TYPES.ObjectExpression:
|
|
89
|
-
return { kind: "ObjectExpression", node };
|
|
90
|
-
case AST_NODE_TYPES.ClassExpression:
|
|
91
|
-
return { kind: "ClassExpression", node };
|
|
92
|
-
case AST_NODE_TYPES.NewExpression:
|
|
93
|
-
return { kind: "NewExpression", node };
|
|
94
|
-
case AST_NODE_TYPES.FunctionExpression:
|
|
95
|
-
case AST_NODE_TYPES.ArrowFunctionExpression:
|
|
96
|
-
return { kind: "FunctionExpression", node };
|
|
97
|
-
case AST_NODE_TYPES.CallExpression: {
|
|
98
|
-
if (hint & ConstructionDetectionHint.StrictCallExpression) {
|
|
99
|
-
return { kind: "CallExpression", node };
|
|
100
|
-
}
|
|
101
|
-
return _;
|
|
102
|
-
}
|
|
103
|
-
case AST_NODE_TYPES.MemberExpression: {
|
|
104
|
-
if (!("object" in node)) return _;
|
|
105
|
-
return getConstruction(node.object, initialScope, hint);
|
|
106
|
-
}
|
|
107
|
-
case AST_NODE_TYPES.AssignmentExpression:
|
|
108
|
-
case AST_NODE_TYPES.AssignmentPattern: {
|
|
109
|
-
if (!("right" in node)) return _;
|
|
110
|
-
return getConstruction(node.right, initialScope, hint);
|
|
111
|
-
}
|
|
112
|
-
case AST_NODE_TYPES.LogicalExpression: {
|
|
113
|
-
const lvc = getConstruction(node.left, initialScope, hint);
|
|
114
|
-
if (lvc == null) return _;
|
|
115
|
-
return getConstruction(node.right, initialScope, hint);
|
|
116
|
-
}
|
|
117
|
-
case AST_NODE_TYPES.ConditionalExpression: {
|
|
118
|
-
const cvc = getConstruction(node.consequent, initialScope, hint);
|
|
119
|
-
if (cvc == null) return _;
|
|
120
|
-
return getConstruction(node.alternate, initialScope, hint);
|
|
121
|
-
}
|
|
122
|
-
case AST_NODE_TYPES.Identifier: {
|
|
123
|
-
if (!("name" in node) || typeof node.name !== "string") {
|
|
124
|
-
return _;
|
|
125
|
-
}
|
|
126
|
-
const variable = initialScope.set.get(node.name);
|
|
127
|
-
const variableNode = getVariableInitNode(variable, -1);
|
|
128
|
-
return getConstruction(variableNode, initialScope, hint);
|
|
129
|
-
}
|
|
130
|
-
case AST_NODE_TYPES.Literal: {
|
|
131
|
-
if ("regex" in node) {
|
|
132
|
-
return { kind: "RegExpLiteral", node };
|
|
133
|
-
}
|
|
134
|
-
return _;
|
|
135
|
-
}
|
|
136
|
-
default: {
|
|
137
|
-
if (!("expression" in node) || typeof node.expression !== "object") {
|
|
138
|
-
return _;
|
|
139
|
-
}
|
|
140
|
-
return getConstruction(node.expression, initialScope, hint);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
function getVariableDeclaratorId(node, prev) {
|
|
145
|
-
if (node == null) return _;
|
|
146
|
-
switch (true) {
|
|
147
|
-
case (node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev):
|
|
148
|
-
return node.id;
|
|
149
|
-
case (node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev):
|
|
150
|
-
return node.left;
|
|
151
|
-
case (node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node):
|
|
152
|
-
return _;
|
|
153
|
-
default:
|
|
154
|
-
return getVariableDeclaratorId(node.parent, node);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// src/var-scope.ts
|
|
159
|
-
function getChidScopes(scope) {
|
|
160
|
-
const scopes = [scope];
|
|
161
|
-
for (const childScope of scope.childScopes) {
|
|
162
|
-
scopes.push(...getChidScopes(childScope));
|
|
163
|
-
}
|
|
164
|
-
return scopes;
|
|
165
|
-
}
|
|
166
|
-
function toStaticValue(lazyValue) {
|
|
167
|
-
const { kind, node, initialScope } = lazyValue;
|
|
168
|
-
if (kind !== "lazy") {
|
|
169
|
-
return lazyValue;
|
|
170
|
-
}
|
|
171
|
-
const staticValue = initialScope == null ? getStaticValue(node) : getStaticValue(node, initialScope);
|
|
172
|
-
return staticValue == null ? { kind: "none", node, initialScope } : { kind: "some", node, initialScope, value: staticValue.value };
|
|
173
|
-
}
|
|
174
|
-
var thisBlockTypes = [
|
|
175
|
-
AST_NODE_TYPES.FunctionDeclaration,
|
|
176
|
-
AST_NODE_TYPES.FunctionExpression,
|
|
177
|
-
AST_NODE_TYPES.ClassBody,
|
|
178
|
-
AST_NODE_TYPES.Program
|
|
179
|
-
];
|
|
180
|
-
function isNodeValueEqual(a, b, initialScopes) {
|
|
181
|
-
const [aScope, bScope] = initialScopes;
|
|
182
|
-
switch (true) {
|
|
183
|
-
case a === b: {
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
case (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal): {
|
|
187
|
-
return a.value === b.value;
|
|
188
|
-
}
|
|
189
|
-
case (a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement): {
|
|
190
|
-
return a.value.cooked === b.value.cooked;
|
|
191
|
-
}
|
|
192
|
-
case (a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier): {
|
|
193
|
-
const aVar = findVariable2(a, aScope);
|
|
194
|
-
const bVar = findVariable2(b, bScope);
|
|
195
|
-
const aVarNode = getVariableInitNodeLoose(aVar, 0);
|
|
196
|
-
const bVarNode = getVariableInitNodeLoose(bVar, 0);
|
|
197
|
-
const aVarNodeParent = aVarNode?.parent;
|
|
198
|
-
const bVarNodeParent = bVarNode?.parent;
|
|
199
|
-
const aDef = aVar?.defs.at(0);
|
|
200
|
-
const bDef = bVar?.defs.at(0);
|
|
201
|
-
const aDefParentParent = aDef?.parent?.parent;
|
|
202
|
-
const bDefParentParent = bDef?.parent?.parent;
|
|
203
|
-
switch (true) {
|
|
204
|
-
case (aVarNodeParent?.type === AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === AST_NODE_TYPES.CallExpression && AST.isFunction(aVarNode) && AST.isFunction(bVarNode)): {
|
|
205
|
-
if (!AST.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) {
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
const aParams = aVarNode.params;
|
|
209
|
-
const bParams = bVarNode.params;
|
|
210
|
-
const aPos = aParams.findIndex((x) => AST.isNodeEqual(x, a));
|
|
211
|
-
const bPos = bParams.findIndex((x) => AST.isNodeEqual(x, b));
|
|
212
|
-
return aPos !== -1 && bPos !== -1 && aPos === bPos;
|
|
213
|
-
}
|
|
214
|
-
case (aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement): {
|
|
215
|
-
const aLeft = aDefParentParent.left;
|
|
216
|
-
const bLeft = bDefParentParent.left;
|
|
217
|
-
if (aLeft.type !== bLeft.type) {
|
|
218
|
-
return false;
|
|
219
|
-
}
|
|
220
|
-
const aRight = aDefParentParent.right;
|
|
221
|
-
const bRight = bDefParentParent.right;
|
|
222
|
-
return AST.isNodeEqual(aRight, bRight);
|
|
223
|
-
}
|
|
224
|
-
default: {
|
|
225
|
-
return aVar != null && bVar != null && aVar === bVar;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
case (a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression): {
|
|
230
|
-
return AST.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes);
|
|
231
|
-
}
|
|
232
|
-
case (a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression): {
|
|
233
|
-
if (aScope.block === bScope.block) {
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
const aFunction = AST.findParentNode(a, AST.isOneOf(thisBlockTypes));
|
|
237
|
-
const bFunction = AST.findParentNode(b, AST.isOneOf(thisBlockTypes));
|
|
238
|
-
return aFunction === bFunction;
|
|
239
|
-
}
|
|
240
|
-
default: {
|
|
241
|
-
const aStatic = toStaticValue({ kind: "lazy", node: a, initialScope: aScope });
|
|
242
|
-
const bStatic = toStaticValue({ kind: "lazy", node: b, initialScope: bScope });
|
|
243
|
-
return aStatic.kind !== "none" && bStatic.kind !== "none" && aStatic.value === bStatic.value;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function getVariableInitNodeLoose(variable, at) {
|
|
248
|
-
if (variable == null) return _;
|
|
249
|
-
const node = getVariableInitNode(variable, at);
|
|
250
|
-
if (node != null) return node;
|
|
251
|
-
const def = variable.defs.at(at);
|
|
252
|
-
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
|
|
253
|
-
return _;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export { ConstructionDetectionHint, findPropertyInProperties, findVariable2 as findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|