@eslint-react/var 2.0.0-next.151 → 2.0.0-next.153
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 +31 -32
- package/dist/index.js +49 -49
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
|
+
import { TSESTree } from "@typescript-eslint/types";
|
|
1
2
|
import { unit } from "@eslint-react/eff";
|
|
2
3
|
import { Scope, Variable } from "@typescript-eslint/scope-manager";
|
|
3
|
-
import { TSESTree } from "@typescript-eslint/types";
|
|
4
|
-
|
|
5
|
-
//#region src/var-collect.d.ts
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @returns All variables from the given scope up to the global scope
|
|
11
|
-
*/
|
|
12
|
-
declare function getVariables(initialScope: Scope): Variable[];
|
|
13
|
-
declare const findVariable: {
|
|
14
|
-
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
|
|
15
|
-
(nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
|
|
16
|
-
};
|
|
17
|
-
declare function findPropertyInProperties(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
|
|
5
|
+
//#region src/misc.d.ts
|
|
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;
|
|
18
8
|
//#endregion
|
|
19
|
-
//#region src/
|
|
9
|
+
//#region src/value-construction.d.ts
|
|
20
10
|
declare const ConstructionDetectionHint: {
|
|
21
11
|
None: bigint;
|
|
22
12
|
StrictCallExpression: bigint;
|
|
@@ -58,16 +48,17 @@ type Construction = {
|
|
|
58
48
|
*/
|
|
59
49
|
declare function getConstruction(node: TSESTree.Node | unit, initialScope: Scope, hint?: bigint): Construction | unit;
|
|
60
50
|
//#endregion
|
|
61
|
-
//#region src/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
51
|
+
//#region src/value-equal.d.ts
|
|
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;
|
|
69
60
|
//#endregion
|
|
70
|
-
//#region src/
|
|
61
|
+
//#region src/value-helper.d.ts
|
|
71
62
|
type LazyValue = {
|
|
72
63
|
kind: "lazy";
|
|
73
64
|
node: TSESTree.Node;
|
|
@@ -94,14 +85,22 @@ declare function toStaticValue(lazyValue: LazyValue): {
|
|
|
94
85
|
readonly value: unknown;
|
|
95
86
|
};
|
|
96
87
|
//#endregion
|
|
97
|
-
//#region src/
|
|
88
|
+
//#region src/variable-assignment.d.ts
|
|
89
|
+
declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/variable-extractor.d.ts
|
|
98
92
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @param
|
|
101
|
-
* @
|
|
102
|
-
* @param initialScopes initial scopes of the two nodes
|
|
103
|
-
* @returns `true` if node value equal
|
|
93
|
+
* Get all variables from the given scope up to the global scope
|
|
94
|
+
* @param initialScope The scope to start from
|
|
95
|
+
* @returns All variables from the given scope up to the global scope
|
|
104
96
|
*/
|
|
105
|
-
declare function
|
|
97
|
+
declare function getVariables(initialScope: Scope): Variable[];
|
|
98
|
+
declare const findVariable: {
|
|
99
|
+
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
|
|
100
|
+
(nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/variable-resolver.d.ts
|
|
104
|
+
declare function getVariableDefinitionNode(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
106
105
|
//#endregion
|
|
107
|
-
export { Construction, ConstructionDetectionHint, LazyValue,
|
|
106
|
+
export { Construction, ConstructionDetectionHint, LazyValue, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
1
2
|
import { dual, unit } from "@eslint-react/eff";
|
|
2
3
|
import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
|
|
3
|
-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
4
4
|
import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
|
|
5
5
|
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
6
6
|
import * as AST from "@eslint-react/ast";
|
|
7
7
|
|
|
8
|
-
//#region src/
|
|
9
|
-
function getVariableInitNode(variable, at) {
|
|
10
|
-
if (variable == null) return unit;
|
|
11
|
-
const def = variable.defs.at(at);
|
|
12
|
-
if (def == null) return unit;
|
|
13
|
-
switch (true) {
|
|
14
|
-
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
|
|
15
|
-
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
|
|
16
|
-
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
|
|
17
|
-
default: return unit;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/var-collect.ts
|
|
8
|
+
//#region src/variable-extractor.ts
|
|
23
9
|
/**
|
|
24
10
|
* Get all variables from the given scope up to the global scope
|
|
25
11
|
* @param initialScope The scope to start from
|
|
@@ -38,21 +24,43 @@ const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
|
38
24
|
if (nameOrNode == null) return unit;
|
|
39
25
|
return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
40
26
|
});
|
|
41
|
-
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/variable-resolver.ts
|
|
30
|
+
function getVariableDefinitionNode(variable, at) {
|
|
31
|
+
if (variable == null) return unit;
|
|
32
|
+
const def = variable.defs.at(at);
|
|
33
|
+
if (def == null) return unit;
|
|
34
|
+
switch (true) {
|
|
35
|
+
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
|
|
36
|
+
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
|
|
37
|
+
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
|
|
38
|
+
default: return unit;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/misc.ts
|
|
44
|
+
function getChildScopes(scope) {
|
|
45
|
+
const scopes = [scope];
|
|
46
|
+
for (const childScope of scope.childScopes) scopes.push(...getChildScopes(childScope));
|
|
47
|
+
return scopes;
|
|
48
|
+
}
|
|
49
|
+
function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
42
50
|
return properties.findLast((prop) => {
|
|
43
51
|
if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
|
|
44
52
|
if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
|
|
45
53
|
case AST_NODE_TYPES.Identifier: {
|
|
46
54
|
if (seen.has(prop.argument.name)) return false;
|
|
47
55
|
const variable = findVariable(prop.argument.name, initialScope);
|
|
48
|
-
const variableNode =
|
|
56
|
+
const variableNode = getVariableDefinitionNode(variable, 0);
|
|
49
57
|
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
50
58
|
seen.add(prop.argument.name);
|
|
51
|
-
return
|
|
59
|
+
return findProperty(name, variableNode.properties, initialScope, seen) != null;
|
|
52
60
|
}
|
|
53
61
|
return false;
|
|
54
62
|
}
|
|
55
|
-
case AST_NODE_TYPES.ObjectExpression: return
|
|
63
|
+
case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
|
|
56
64
|
default: return false;
|
|
57
65
|
}
|
|
58
66
|
return false;
|
|
@@ -60,7 +68,7 @@ function findPropertyInProperties(name, properties, initialScope, seen = /* @__P
|
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
//#endregion
|
|
63
|
-
//#region src/
|
|
71
|
+
//#region src/value-construction.ts
|
|
64
72
|
const ConstructionDetectionHint = {
|
|
65
73
|
None: 0n,
|
|
66
74
|
StrictCallExpression: 1n << 0n
|
|
@@ -127,7 +135,7 @@ function getConstruction(node, initialScope, hint = ConstructionDetectionHint.No
|
|
|
127
135
|
case AST_NODE_TYPES.Identifier: {
|
|
128
136
|
if (!("name" in node) || typeof node.name !== "string") return unit;
|
|
129
137
|
const variable = initialScope.set.get(node.name);
|
|
130
|
-
const variableNode =
|
|
138
|
+
const variableNode = getVariableDefinitionNode(variable, -1);
|
|
131
139
|
return getConstruction(variableNode, initialScope, hint);
|
|
132
140
|
}
|
|
133
141
|
case AST_NODE_TYPES.Literal:
|
|
@@ -143,27 +151,7 @@ function getConstruction(node, initialScope, hint = ConstructionDetectionHint.No
|
|
|
143
151
|
}
|
|
144
152
|
|
|
145
153
|
//#endregion
|
|
146
|
-
//#region src/
|
|
147
|
-
function getVariableDeclaratorId(node, prev) {
|
|
148
|
-
if (node == null) return unit;
|
|
149
|
-
switch (true) {
|
|
150
|
-
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
151
|
-
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
152
|
-
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
153
|
-
default: return getVariableDeclaratorId(node.parent, node);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
//#endregion
|
|
158
|
-
//#region src/var-scope.ts
|
|
159
|
-
function getChidScopes(scope) {
|
|
160
|
-
const scopes = [scope];
|
|
161
|
-
for (const childScope of scope.childScopes) scopes.push(...getChidScopes(childScope));
|
|
162
|
-
return scopes;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
//#endregion
|
|
166
|
-
//#region src/var-value.ts
|
|
154
|
+
//#region src/value-helper.ts
|
|
167
155
|
function toStaticValue(lazyValue) {
|
|
168
156
|
const { kind, node, initialScope } = lazyValue;
|
|
169
157
|
if (kind !== "lazy") return lazyValue;
|
|
@@ -181,7 +169,7 @@ function toStaticValue(lazyValue) {
|
|
|
181
169
|
}
|
|
182
170
|
|
|
183
171
|
//#endregion
|
|
184
|
-
//#region src/
|
|
172
|
+
//#region src/value-equal.ts
|
|
185
173
|
const thisBlockTypes = [
|
|
186
174
|
AST_NODE_TYPES.FunctionDeclaration,
|
|
187
175
|
AST_NODE_TYPES.FunctionExpression,
|
|
@@ -204,8 +192,8 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
204
192
|
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
|
|
205
193
|
const aVar = findVariable(a, aScope);
|
|
206
194
|
const bVar = findVariable(b, bScope);
|
|
207
|
-
const aVarNode =
|
|
208
|
-
const bVarNode =
|
|
195
|
+
const aVarNode = getVariableDefinitionNodeLoose(aVar, 0);
|
|
196
|
+
const bVarNode = getVariableDefinitionNodeLoose(bVar, 0);
|
|
209
197
|
const aVarNodeParent = aVarNode?.parent;
|
|
210
198
|
const bVarNodeParent = bVarNode?.parent;
|
|
211
199
|
const aDef = aVar?.defs.at(0);
|
|
@@ -254,9 +242,9 @@ function isNodeValueEqual(a, b, initialScopes) {
|
|
|
254
242
|
}
|
|
255
243
|
}
|
|
256
244
|
}
|
|
257
|
-
function
|
|
245
|
+
function getVariableDefinitionNodeLoose(variable, at) {
|
|
258
246
|
if (variable == null) return unit;
|
|
259
|
-
const node =
|
|
247
|
+
const node = getVariableDefinitionNode(variable, at);
|
|
260
248
|
if (node != null) return node;
|
|
261
249
|
const def = variable.defs.at(at);
|
|
262
250
|
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node;
|
|
@@ -264,4 +252,16 @@ function getVariableInitNodeLoose(variable, at) {
|
|
|
264
252
|
}
|
|
265
253
|
|
|
266
254
|
//#endregion
|
|
267
|
-
|
|
255
|
+
//#region src/variable-assignment.ts
|
|
256
|
+
function findAssignmentTarget(node, prev) {
|
|
257
|
+
if (node == null) return unit;
|
|
258
|
+
switch (true) {
|
|
259
|
+
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
260
|
+
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
261
|
+
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
262
|
+
default: return findAssignmentTarget(node.parent, node);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
//#endregion
|
|
267
|
+
export { ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, 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.153",
|
|
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.41.0",
|
|
33
33
|
"string-ts": "^2.2.1",
|
|
34
34
|
"ts-pattern": "^5.8.0",
|
|
35
|
-
"@eslint-react/
|
|
36
|
-
"@eslint-react/
|
|
35
|
+
"@eslint-react/eff": "2.0.0-next.153",
|
|
36
|
+
"@eslint-react/ast": "2.0.0-next.153"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"tsdown": "^0.14.2",
|