@eslint-react/var 2.0.0-next.9 → 2.0.1-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.ts +54 -74
- package/dist/index.js +226 -242
- package/package.json +9 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,50 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { TSESTree } from
|
|
3
|
-
import {
|
|
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] | _;
|
|
1
|
+
import { unit } from "@eslint-react/eff";
|
|
2
|
+
import { TSESTree } from "@typescript-eslint/types";
|
|
3
|
+
import { Scope, Variable } from "@typescript-eslint/scope-manager";
|
|
16
4
|
|
|
5
|
+
//#region src/construction-detection.d.ts
|
|
17
6
|
declare const ConstructionDetectionHint: {
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
None: bigint;
|
|
8
|
+
StrictCallExpression: bigint;
|
|
20
9
|
};
|
|
21
10
|
type Construction = {
|
|
22
|
-
|
|
23
|
-
|
|
11
|
+
kind: "ArrayExpression";
|
|
12
|
+
node: TSESTree.ArrayExpression;
|
|
24
13
|
} | {
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
kind: "CallExpression";
|
|
15
|
+
node: TSESTree.CallExpression;
|
|
27
16
|
} | {
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
kind: "ClassExpression";
|
|
18
|
+
node: TSESTree.ClassExpression;
|
|
30
19
|
} | {
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
kind: "FunctionDeclaration";
|
|
21
|
+
node: TSESTree.FunctionDeclaration;
|
|
33
22
|
} | {
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
kind: "FunctionExpression";
|
|
24
|
+
node: TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
|
|
36
25
|
} | {
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
kind: "JSXElement";
|
|
27
|
+
node: TSESTree.JSXElement | TSESTree.JSXFragment;
|
|
39
28
|
} | {
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
kind: "NewExpression";
|
|
30
|
+
node: TSESTree.NewExpression;
|
|
42
31
|
} | {
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
kind: "ObjectExpression";
|
|
33
|
+
node: TSESTree.ObjectExpression;
|
|
45
34
|
} | {
|
|
46
|
-
|
|
47
|
-
|
|
35
|
+
kind: "RegExpLiteral";
|
|
36
|
+
node: TSESTree.RegExpLiteral;
|
|
48
37
|
};
|
|
49
38
|
/**
|
|
50
39
|
* Detects the construction type of a given node.
|
|
@@ -53,40 +42,34 @@ type Construction = {
|
|
|
53
42
|
* @param hint Optional hint to control the detection behavior.
|
|
54
43
|
* @returns The construction type of the node, or `_` if not found.
|
|
55
44
|
*/
|
|
56
|
-
declare function getConstruction(node: TSESTree.Node |
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
declare function
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
declare
|
|
79
|
-
|
|
80
|
-
|
|
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;
|
|
45
|
+
declare function getConstruction(node: TSESTree.Node | unit, initialScope: Scope, hint?: bigint): Construction | unit;
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/find-assignment-target.d.ts
|
|
48
|
+
declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
|
|
49
|
+
//#endregion
|
|
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
|
|
61
|
+
/**
|
|
62
|
+
* Get all variables from the given scope up to the global scope
|
|
63
|
+
* @param initialScope The scope to start from
|
|
64
|
+
* @returns All variables from the given scope up to the global scope
|
|
65
|
+
*/
|
|
66
|
+
declare function getVariables(initialScope: Scope): Variable[];
|
|
67
|
+
declare const findVariable: {
|
|
68
|
+
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
|
|
69
|
+
(nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
|
|
88
70
|
};
|
|
89
|
-
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/is-node-value-equal.d.ts
|
|
90
73
|
/**
|
|
91
74
|
* Determines whether node value equals to another node value
|
|
92
75
|
* @param a node to compare
|
|
@@ -94,9 +77,6 @@ declare function toStaticValue(lazyValue: LazyValue): {
|
|
|
94
77
|
* @param initialScopes initial scopes of the two nodes
|
|
95
78
|
* @returns `true` if node value equal
|
|
96
79
|
*/
|
|
97
|
-
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
]): boolean;
|
|
101
|
-
|
|
102
|
-
export { type Construction, ConstructionDetectionHint, type LazyValue, findPropertyInProperties, findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
|
80
|
+
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
|
|
81
|
+
//#endregion
|
|
82
|
+
export { Construction, ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
|
package/dist/index.js
CHANGED
|
@@ -1,256 +1,240 @@
|
|
|
1
|
-
import { dual,
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
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";
|
|
4
|
+
import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
|
|
5
|
+
import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
|
|
6
|
+
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return def.node.init;
|
|
20
|
-
default:
|
|
21
|
-
return _;
|
|
22
|
-
}
|
|
8
|
+
//#region src/get-variable-definition-node.ts
|
|
9
|
+
function getVariableDefinitionNode(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
|
+
}
|
|
23
19
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
});
|
|
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;
|
|
75
27
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/construction-detection.ts
|
|
31
|
+
const ConstructionDetectionHint = {
|
|
32
|
+
None: 0n,
|
|
33
|
+
StrictCallExpression: 1n << 0n
|
|
79
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Detects the construction type of a given node.
|
|
37
|
+
* @param node The node to check.
|
|
38
|
+
* @param initialScope The initial scope to check for variable declarations.
|
|
39
|
+
* @param hint Optional hint to control the detection behavior.
|
|
40
|
+
* @returns The construction type of the node, or `_` if not found.
|
|
41
|
+
*/
|
|
80
42
|
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
43
|
+
if (node == null) return unit;
|
|
44
|
+
switch (node.type) {
|
|
45
|
+
case AST_NODE_TYPES.JSXElement:
|
|
46
|
+
case AST_NODE_TYPES.JSXFragment: return {
|
|
47
|
+
kind: "JSXElement",
|
|
48
|
+
node
|
|
49
|
+
};
|
|
50
|
+
case AST_NODE_TYPES.ArrayExpression: return {
|
|
51
|
+
kind: "ArrayExpression",
|
|
52
|
+
node
|
|
53
|
+
};
|
|
54
|
+
case AST_NODE_TYPES.ObjectExpression: return {
|
|
55
|
+
kind: "ObjectExpression",
|
|
56
|
+
node
|
|
57
|
+
};
|
|
58
|
+
case AST_NODE_TYPES.ClassExpression: return {
|
|
59
|
+
kind: "ClassExpression",
|
|
60
|
+
node
|
|
61
|
+
};
|
|
62
|
+
case AST_NODE_TYPES.NewExpression: return {
|
|
63
|
+
kind: "NewExpression",
|
|
64
|
+
node
|
|
65
|
+
};
|
|
66
|
+
case AST_NODE_TYPES.FunctionExpression:
|
|
67
|
+
case AST_NODE_TYPES.ArrowFunctionExpression: return {
|
|
68
|
+
kind: "FunctionExpression",
|
|
69
|
+
node
|
|
70
|
+
};
|
|
71
|
+
case AST_NODE_TYPES.CallExpression:
|
|
72
|
+
if (hint & ConstructionDetectionHint.StrictCallExpression) return {
|
|
73
|
+
kind: "CallExpression",
|
|
74
|
+
node
|
|
75
|
+
};
|
|
76
|
+
return unit;
|
|
77
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
78
|
+
if (!("object" in node)) return unit;
|
|
79
|
+
return getConstruction(node.object, initialScope, hint);
|
|
80
|
+
case AST_NODE_TYPES.AssignmentExpression:
|
|
81
|
+
case AST_NODE_TYPES.AssignmentPattern:
|
|
82
|
+
if (!("right" in node)) return unit;
|
|
83
|
+
return getConstruction(node.right, initialScope, hint);
|
|
84
|
+
case AST_NODE_TYPES.LogicalExpression:
|
|
85
|
+
if (getConstruction(node.left, initialScope, hint) == null) return unit;
|
|
86
|
+
return getConstruction(node.right, initialScope, hint);
|
|
87
|
+
case AST_NODE_TYPES.ConditionalExpression:
|
|
88
|
+
if (getConstruction(node.consequent, initialScope, hint) == null) return unit;
|
|
89
|
+
return getConstruction(node.alternate, initialScope, hint);
|
|
90
|
+
case AST_NODE_TYPES.Identifier: {
|
|
91
|
+
if (!("name" in node) || typeof node.name !== "string") return unit;
|
|
92
|
+
const variable = initialScope.set.get(node.name);
|
|
93
|
+
const variableNode = getVariableDefinitionNode(variable, -1);
|
|
94
|
+
return getConstruction(variableNode, initialScope, hint);
|
|
95
|
+
}
|
|
96
|
+
case AST_NODE_TYPES.Literal:
|
|
97
|
+
if ("regex" in node) return {
|
|
98
|
+
kind: "RegExpLiteral",
|
|
99
|
+
node
|
|
100
|
+
};
|
|
101
|
+
return unit;
|
|
102
|
+
default:
|
|
103
|
+
if (!("expression" in node) || typeof node.expression !== "object") return unit;
|
|
104
|
+
return getConstruction(node.expression, initialScope, hint);
|
|
105
|
+
}
|
|
143
106
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/find-assignment-target.ts
|
|
110
|
+
function findAssignmentTarget(node, prev) {
|
|
111
|
+
if (node == null) return unit;
|
|
112
|
+
switch (true) {
|
|
113
|
+
case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
|
|
114
|
+
case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
|
|
115
|
+
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
116
|
+
default: return findAssignmentTarget(node.parent, node);
|
|
117
|
+
}
|
|
156
118
|
}
|
|
157
119
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/get-variables-from-scope.ts
|
|
122
|
+
/**
|
|
123
|
+
* Get all variables from the given scope up to the global scope
|
|
124
|
+
* @param initialScope The scope to start from
|
|
125
|
+
* @returns All variables from the given scope up to the global scope
|
|
126
|
+
*/
|
|
127
|
+
function getVariables(initialScope) {
|
|
128
|
+
let scope = initialScope;
|
|
129
|
+
const variables = [...scope.variables];
|
|
130
|
+
while (scope.type !== ScopeType.global) {
|
|
131
|
+
scope = scope.upper;
|
|
132
|
+
variables.push(...scope.variables);
|
|
133
|
+
}
|
|
134
|
+
return variables.reverse();
|
|
165
135
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
136
|
+
const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
137
|
+
if (nameOrNode == null) return unit;
|
|
138
|
+
return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/find-property.ts
|
|
143
|
+
function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
144
|
+
return properties.findLast((prop) => {
|
|
145
|
+
if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
|
|
146
|
+
if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
|
|
147
|
+
case AST_NODE_TYPES.Identifier: {
|
|
148
|
+
if (seen.has(prop.argument.name)) return false;
|
|
149
|
+
const variable = findVariable(prop.argument.name, initialScope);
|
|
150
|
+
const variableNode = getVariableDefinitionNode(variable, 0);
|
|
151
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
152
|
+
seen.add(prop.argument.name);
|
|
153
|
+
return findProperty(name, variableNode.properties, initialScope, seen) != null;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
|
|
158
|
+
default: return false;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/get-child-scopes.ts
|
|
166
|
+
function getChildScopes(scope) {
|
|
167
|
+
const scopes = [scope];
|
|
168
|
+
for (const childScope of scope.childScopes) scopes.push(...getChildScopes(childScope));
|
|
169
|
+
return scopes;
|
|
173
170
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/is-node-value-equal.ts
|
|
174
|
+
const thisBlockTypes = [
|
|
175
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
176
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
177
|
+
AST_NODE_TYPES.ClassBody,
|
|
178
|
+
AST_NODE_TYPES.Program
|
|
179
179
|
];
|
|
180
|
+
/**
|
|
181
|
+
* Determines whether node value equals to another node value
|
|
182
|
+
* @param a node to compare
|
|
183
|
+
* @param b node to compare
|
|
184
|
+
* @param initialScopes initial scopes of the two nodes
|
|
185
|
+
* @returns `true` if node value equal
|
|
186
|
+
*/
|
|
180
187
|
function isNodeValueEqual(a, b, initialScopes) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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 _;
|
|
188
|
+
const [aScope, bScope] = initialScopes;
|
|
189
|
+
switch (true) {
|
|
190
|
+
case a === b: return true;
|
|
191
|
+
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
|
|
192
|
+
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
|
|
193
|
+
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
|
|
194
|
+
const aVar = findVariable(a, aScope);
|
|
195
|
+
const bVar = findVariable(b, bScope);
|
|
196
|
+
const aVarNode = getVariableDefinitionNodeLoose(aVar, 0);
|
|
197
|
+
const bVarNode = getVariableDefinitionNodeLoose(bVar, 0);
|
|
198
|
+
const aVarNodeParent = aVarNode?.parent;
|
|
199
|
+
const bVarNodeParent = bVarNode?.parent;
|
|
200
|
+
const aDef = aVar?.defs.at(0);
|
|
201
|
+
const bDef = bVar?.defs.at(0);
|
|
202
|
+
const aDefParentParent = aDef?.parent?.parent;
|
|
203
|
+
const bDefParentParent = bDef?.parent?.parent;
|
|
204
|
+
switch (true) {
|
|
205
|
+
case aVarNodeParent?.type === AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === AST_NODE_TYPES.CallExpression && AST.isFunction(aVarNode) && AST.isFunction(bVarNode): {
|
|
206
|
+
if (!AST.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) return false;
|
|
207
|
+
const aParams = aVarNode.params;
|
|
208
|
+
const bParams = bVarNode.params;
|
|
209
|
+
const aPos = aParams.findIndex((x) => AST.isNodeEqual(x, a));
|
|
210
|
+
const bPos = bParams.findIndex((x) => AST.isNodeEqual(x, b));
|
|
211
|
+
return aPos !== -1 && bPos !== -1 && aPos === bPos;
|
|
212
|
+
}
|
|
213
|
+
case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
|
|
214
|
+
const aLeft = aDefParentParent.left;
|
|
215
|
+
const bLeft = bDefParentParent.left;
|
|
216
|
+
if (aLeft.type !== bLeft.type) return false;
|
|
217
|
+
const aRight = aDefParentParent.right;
|
|
218
|
+
const bRight = bDefParentParent.right;
|
|
219
|
+
return AST.isNodeEqual(aRight, bRight);
|
|
220
|
+
}
|
|
221
|
+
default: return aVar != null && bVar != null && aVar === bVar;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return AST.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes);
|
|
225
|
+
case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression: {
|
|
226
|
+
if (aScope.block === bScope.block) return true;
|
|
227
|
+
const aFunction = AST.findParentNode(a, AST.isOneOf(thisBlockTypes));
|
|
228
|
+
const bFunction = AST.findParentNode(b, AST.isOneOf(thisBlockTypes));
|
|
229
|
+
return aFunction === bFunction;
|
|
230
|
+
}
|
|
231
|
+
default: {
|
|
232
|
+
const aStatic = getStaticValue(a, aScope);
|
|
233
|
+
const bStatic = getStaticValue(b, bScope);
|
|
234
|
+
return aStatic != null && bStatic != null && aStatic.value === bStatic.value;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
254
237
|
}
|
|
255
238
|
|
|
256
|
-
|
|
239
|
+
//#endregion
|
|
240
|
+
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.
|
|
3
|
+
"version": "2.0.1-beta.0",
|
|
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": {
|
|
@@ -27,24 +27,23 @@
|
|
|
27
27
|
"./package.json"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@typescript-eslint/scope-manager": "^8.
|
|
31
|
-
"@typescript-eslint/types": "^8.
|
|
32
|
-
"@typescript-eslint/utils": "^8.
|
|
30
|
+
"@typescript-eslint/scope-manager": "^8.44.1",
|
|
31
|
+
"@typescript-eslint/types": "^8.44.1",
|
|
32
|
+
"@typescript-eslint/utils": "^8.44.1",
|
|
33
33
|
"string-ts": "^2.2.1",
|
|
34
|
-
"ts-pattern": "^5.
|
|
35
|
-
"@eslint-react/ast": "2.0.
|
|
36
|
-
"@eslint-react/eff": "2.0.
|
|
34
|
+
"ts-pattern": "^5.8.0",
|
|
35
|
+
"@eslint-react/ast": "2.0.1-beta.0",
|
|
36
|
+
"@eslint-react/eff": "2.0.1-beta.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"
|
|
39
|
+
"tsdown": "^0.15.4",
|
|
40
40
|
"@local/configs": "0.0.0"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
|
-
"bun": ">=1.0.15",
|
|
44
43
|
"node": ">=20.19.0"
|
|
45
44
|
},
|
|
46
45
|
"scripts": {
|
|
47
|
-
"build": "
|
|
46
|
+
"build": "tsdown --dts-resolve",
|
|
48
47
|
"lint:publish": "publint",
|
|
49
48
|
"lint:ts": "tsc --noEmit"
|
|
50
49
|
}
|