@eslint-react/var 2.0.0-next.170 → 2.0.0-next.171
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 +24 -21
- package/dist/index.js +77 -74
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { TSESTree } from "@typescript-eslint/types";
|
|
2
1
|
import { unit } from "@eslint-react/eff";
|
|
2
|
+
import { TSESTree } from "@typescript-eslint/types";
|
|
3
3
|
import { Scope, Variable } from "@typescript-eslint/scope-manager";
|
|
4
4
|
|
|
5
|
-
//#region src/
|
|
6
|
-
declare function getChildScopes(scope: Scope): readonly Scope[];
|
|
7
|
-
declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
|
|
8
|
-
//#endregion
|
|
9
|
-
//#region src/value-construction.d.ts
|
|
5
|
+
//#region src/construction-detection.d.ts
|
|
10
6
|
declare const ConstructionDetectionHint: {
|
|
11
7
|
None: bigint;
|
|
12
8
|
StrictCallExpression: bigint;
|
|
@@ -48,20 +44,20 @@ type Construction = {
|
|
|
48
44
|
*/
|
|
49
45
|
declare function getConstruction(node: TSESTree.Node | unit, initialScope: Scope, hint?: bigint): Construction | unit;
|
|
50
46
|
//#endregion
|
|
51
|
-
//#region src/
|
|
52
|
-
/**
|
|
53
|
-
* Determines whether node value equals to another node value
|
|
54
|
-
* @param a node to compare
|
|
55
|
-
* @param b node to compare
|
|
56
|
-
* @param initialScopes initial scopes of the two nodes
|
|
57
|
-
* @returns `true` if node value equal
|
|
58
|
-
*/
|
|
59
|
-
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/variable-assignment.d.ts
|
|
47
|
+
//#region src/find-assignment-target.d.ts
|
|
62
48
|
declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
|
|
63
49
|
//#endregion
|
|
64
|
-
//#region src/
|
|
50
|
+
//#region src/find-property.d.ts
|
|
51
|
+
declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/get-child-scopes.d.ts
|
|
54
|
+
declare function getChildScopes(scope: Scope): readonly Scope[];
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/get-variable-definition-node.d.ts
|
|
57
|
+
declare function getVariableDefinitionNode(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
58
|
+
declare function getVariableDefinitionNodeLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/get-variables-from-scope.d.ts
|
|
65
61
|
/**
|
|
66
62
|
* Get all variables from the given scope up to the global scope
|
|
67
63
|
* @param initialScope The scope to start from
|
|
@@ -73,7 +69,14 @@ declare const findVariable: {
|
|
|
73
69
|
(nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
|
|
74
70
|
};
|
|
75
71
|
//#endregion
|
|
76
|
-
//#region src/
|
|
77
|
-
|
|
72
|
+
//#region src/is-node-value-equal.d.ts
|
|
73
|
+
/**
|
|
74
|
+
* Determines whether node value equals to another node value
|
|
75
|
+
* @param a node to compare
|
|
76
|
+
* @param b node to compare
|
|
77
|
+
* @param initialScopes initial scopes of the two nodes
|
|
78
|
+
* @returns `true` if node value equal
|
|
79
|
+
*/
|
|
80
|
+
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
|
|
78
81
|
//#endregion
|
|
79
|
-
export { Construction, ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariables, isNodeValueEqual };
|
|
82
|
+
export { Construction, ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,11 @@
|
|
|
1
|
-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
2
1
|
import { dual, unit } from "@eslint-react/eff";
|
|
2
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
3
|
+
import * as AST from "@eslint-react/ast";
|
|
3
4
|
import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
|
|
4
5
|
import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
|
|
5
6
|
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
6
|
-
import * as AST from "@eslint-react/ast";
|
|
7
|
-
|
|
8
|
-
//#region src/variable-extractor.ts
|
|
9
|
-
/**
|
|
10
|
-
* Get all variables from the given scope up to the global scope
|
|
11
|
-
* @param initialScope The scope to start from
|
|
12
|
-
* @returns All variables from the given scope up to the global scope
|
|
13
|
-
*/
|
|
14
|
-
function getVariables(initialScope) {
|
|
15
|
-
let scope = initialScope;
|
|
16
|
-
const variables = [...scope.variables];
|
|
17
|
-
while (scope.type !== ScopeType.global) {
|
|
18
|
-
scope = scope.upper;
|
|
19
|
-
variables.push(...scope.variables);
|
|
20
|
-
}
|
|
21
|
-
return variables.reverse();
|
|
22
|
-
}
|
|
23
|
-
const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
24
|
-
if (nameOrNode == null) return unit;
|
|
25
|
-
return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
26
|
-
});
|
|
27
7
|
|
|
28
|
-
//#
|
|
29
|
-
//#region src/variable-resolver.ts
|
|
8
|
+
//#region src/get-variable-definition-node.ts
|
|
30
9
|
function getVariableDefinitionNode(variable, at) {
|
|
31
10
|
if (variable == null) return unit;
|
|
32
11
|
const def = variable.defs.at(at);
|
|
@@ -38,37 +17,17 @@ function getVariableDefinitionNode(variable, at) {
|
|
|
38
17
|
default: return unit;
|
|
39
18
|
}
|
|
40
19
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
50
|
-
return properties.findLast((prop) => {
|
|
51
|
-
if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
|
|
52
|
-
if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
|
|
53
|
-
case AST_NODE_TYPES.Identifier: {
|
|
54
|
-
if (seen.has(prop.argument.name)) return false;
|
|
55
|
-
const variable = findVariable(prop.argument.name, initialScope);
|
|
56
|
-
const variableNode = getVariableDefinitionNode(variable, 0);
|
|
57
|
-
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
58
|
-
seen.add(prop.argument.name);
|
|
59
|
-
return findProperty(name, variableNode.properties, initialScope, seen) != null;
|
|
60
|
-
}
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
|
|
64
|
-
default: return false;
|
|
65
|
-
}
|
|
66
|
-
return false;
|
|
67
|
-
});
|
|
20
|
+
function getVariableDefinitionNodeLoose(variable, at) {
|
|
21
|
+
if (variable == null) return unit;
|
|
22
|
+
const node = getVariableDefinitionNode(variable, at);
|
|
23
|
+
if (node != null) return node;
|
|
24
|
+
const def = variable.defs.at(at);
|
|
25
|
+
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
|
|
26
|
+
return unit;
|
|
68
27
|
}
|
|
69
28
|
|
|
70
29
|
//#endregion
|
|
71
|
-
//#region src/
|
|
30
|
+
//#region src/construction-detection.ts
|
|
72
31
|
const ConstructionDetectionHint = {
|
|
73
32
|
None: 0n,
|
|
74
33
|
StrictCallExpression: 1n << 0n
|
|
@@ -151,7 +110,71 @@ function getConstruction(node, initialScope, hint = ConstructionDetectionHint.No
|
|
|
151
110
|
}
|
|
152
111
|
|
|
153
112
|
//#endregion
|
|
154
|
-
//#region src/
|
|
113
|
+
//#region src/find-assignment-target.ts
|
|
114
|
+
function findAssignmentTarget(node, prev) {
|
|
115
|
+
if (node == null) return unit;
|
|
116
|
+
switch (true) {
|
|
117
|
+
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
118
|
+
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
119
|
+
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
120
|
+
default: return findAssignmentTarget(node.parent, node);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/get-variables-from-scope.ts
|
|
126
|
+
/**
|
|
127
|
+
* Get all variables from the given scope up to the global scope
|
|
128
|
+
* @param initialScope The scope to start from
|
|
129
|
+
* @returns All variables from the given scope up to the global scope
|
|
130
|
+
*/
|
|
131
|
+
function getVariables(initialScope) {
|
|
132
|
+
let scope = initialScope;
|
|
133
|
+
const variables = [...scope.variables];
|
|
134
|
+
while (scope.type !== ScopeType.global) {
|
|
135
|
+
scope = scope.upper;
|
|
136
|
+
variables.push(...scope.variables);
|
|
137
|
+
}
|
|
138
|
+
return variables.reverse();
|
|
139
|
+
}
|
|
140
|
+
const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
141
|
+
if (nameOrNode == null) return unit;
|
|
142
|
+
return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/find-property.ts
|
|
147
|
+
function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
148
|
+
return properties.findLast((prop) => {
|
|
149
|
+
if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
|
|
150
|
+
if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
|
|
151
|
+
case AST_NODE_TYPES.Identifier: {
|
|
152
|
+
if (seen.has(prop.argument.name)) return false;
|
|
153
|
+
const variable = findVariable(prop.argument.name, initialScope);
|
|
154
|
+
const variableNode = getVariableDefinitionNode(variable, 0);
|
|
155
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
156
|
+
seen.add(prop.argument.name);
|
|
157
|
+
return findProperty(name, variableNode.properties, initialScope, seen) != null;
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
|
|
162
|
+
default: return false;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/get-child-scopes.ts
|
|
170
|
+
function getChildScopes(scope) {
|
|
171
|
+
const scopes = [scope];
|
|
172
|
+
for (const childScope of scope.childScopes) scopes.push(...getChildScopes(childScope));
|
|
173
|
+
return scopes;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/is-node-value-equal.ts
|
|
155
178
|
const thisBlockTypes = [
|
|
156
179
|
AST_NODE_TYPES.FunctionDeclaration,
|
|
157
180
|
AST_NODE_TYPES.FunctionExpression,
|
|
@@ -216,26 +239,6 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
216
239
|
}
|
|
217
240
|
}
|
|
218
241
|
}
|
|
219
|
-
function getVariableDefinitionNodeLoose(variable, at) {
|
|
220
|
-
if (variable == null) return unit;
|
|
221
|
-
const node = getVariableDefinitionNode(variable, at);
|
|
222
|
-
if (node != null) return node;
|
|
223
|
-
const def = variable.defs.at(at);
|
|
224
|
-
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
|
|
225
|
-
return unit;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
//#endregion
|
|
229
|
-
//#region src/variable-assignment.ts
|
|
230
|
-
function findAssignmentTarget(node, prev) {
|
|
231
|
-
if (node == null) return unit;
|
|
232
|
-
switch (true) {
|
|
233
|
-
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
234
|
-
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
235
|
-
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
236
|
-
default: return findAssignmentTarget(node.parent, node);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
242
|
|
|
240
243
|
//#endregion
|
|
241
|
-
export { ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariables, isNodeValueEqual };
|
|
244
|
+
export { ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
|
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.171",
|
|
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": {
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"@typescript-eslint/utils": "^8.42.0",
|
|
33
33
|
"string-ts": "^2.2.1",
|
|
34
34
|
"ts-pattern": "^5.8.0",
|
|
35
|
-
"@eslint-react/ast": "2.0.0-next.
|
|
36
|
-
"@eslint-react/eff": "2.0.0-next.
|
|
35
|
+
"@eslint-react/ast": "2.0.0-next.171",
|
|
36
|
+
"@eslint-react/eff": "2.0.0-next.171"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"tsdown": "^0.14.2",
|