@eslint-react/var 3.0.0-next.60 → 3.0.0-next.62
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 +1 -17
- package/dist/index.js +29 -41
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -70,22 +70,6 @@ type ObjectType = {
|
|
|
70
70
|
*/
|
|
71
71
|
declare function getObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit;
|
|
72
72
|
//#endregion
|
|
73
|
-
//#region src/get-variable-initializer.d.ts
|
|
74
|
-
/**
|
|
75
|
-
* Get the initializer expression or statement of a variable definition at a specified index
|
|
76
|
-
* @param variable The variable to get the initializer from
|
|
77
|
-
* @param at The index of the variable definition to get the initializer from
|
|
78
|
-
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
|
|
79
|
-
*/
|
|
80
|
-
declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
|
|
81
|
-
/**
|
|
82
|
-
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
|
|
83
|
-
* @param variable The variable to get the initializer from
|
|
84
|
-
* @param at The index of the variable definition to get the initializer from
|
|
85
|
-
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
|
|
86
|
-
*/
|
|
87
|
-
declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
|
|
88
|
-
//#endregion
|
|
89
73
|
//#region src/is-assignment-target-equal.d.ts
|
|
90
74
|
/**
|
|
91
75
|
* Check if two assignment targets are equal
|
|
@@ -108,4 +92,4 @@ declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node,
|
|
|
108
92
|
*/
|
|
109
93
|
declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
|
|
110
94
|
//#endregion
|
|
111
|
-
export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType,
|
|
95
|
+
export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual };
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,8 @@ import { dual, unit } from "@eslint-react/eff";
|
|
|
2
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
3
3
|
import * as astUtils from "@typescript-eslint/utils/ast-utils";
|
|
4
4
|
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
5
|
-
import * as ast from "@eslint-react/ast";
|
|
6
5
|
import { DefinitionType } from "@typescript-eslint/scope-manager";
|
|
6
|
+
import * as ast from "@eslint-react/ast";
|
|
7
7
|
|
|
8
8
|
//#region src/find-enclosing-assignment-target.ts
|
|
9
9
|
/**
|
|
@@ -39,40 +39,6 @@ const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
|
39
39
|
return astUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/get-variable-initializer.ts
|
|
44
|
-
/**
|
|
45
|
-
* Get the initializer expression or statement of a variable definition at a specified index
|
|
46
|
-
* @param variable The variable to get the initializer from
|
|
47
|
-
* @param at The index of the variable definition to get the initializer from
|
|
48
|
-
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
|
|
49
|
-
*/
|
|
50
|
-
function getVariableInitializer(variable, at) {
|
|
51
|
-
if (variable == null) return unit;
|
|
52
|
-
const def = variable.defs.at(at);
|
|
53
|
-
if (def == null) return unit;
|
|
54
|
-
switch (true) {
|
|
55
|
-
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
|
|
56
|
-
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
|
|
57
|
-
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
|
|
58
|
-
default: return unit;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
|
|
63
|
-
* @param variable The variable to get the initializer from
|
|
64
|
-
* @param at The index of the variable definition to get the initializer from
|
|
65
|
-
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
|
|
66
|
-
*/
|
|
67
|
-
function getVariableInitializerLoose(variable, at) {
|
|
68
|
-
if (variable == null) return unit;
|
|
69
|
-
const node = getVariableInitializer(variable, at);
|
|
70
|
-
if (node != null) return node;
|
|
71
|
-
const def = variable.defs.at(at);
|
|
72
|
-
if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
|
|
73
|
-
return unit;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
42
|
//#endregion
|
|
77
43
|
//#region src/get-object-type.ts
|
|
78
44
|
/**
|
|
@@ -118,9 +84,11 @@ function getObjectType(node, initialScope) {
|
|
|
118
84
|
node
|
|
119
85
|
};
|
|
120
86
|
return unit;
|
|
121
|
-
case AST_NODE_TYPES.Identifier:
|
|
122
|
-
|
|
123
|
-
|
|
87
|
+
case AST_NODE_TYPES.Identifier: {
|
|
88
|
+
const initNode = resolve(initialScope.set.get(node.name));
|
|
89
|
+
if (initNode == null) return unit;
|
|
90
|
+
return getObjectType(initNode, initialScope);
|
|
91
|
+
}
|
|
124
92
|
case AST_NODE_TYPES.MemberExpression:
|
|
125
93
|
if (!("object" in node)) return unit;
|
|
126
94
|
return getObjectType(node.object, initialScope);
|
|
@@ -143,6 +111,15 @@ function getObjectType(node, initialScope) {
|
|
|
143
111
|
return getObjectType(node.expression, initialScope);
|
|
144
112
|
}
|
|
145
113
|
}
|
|
114
|
+
function resolve(v) {
|
|
115
|
+
if (v == null) return unit;
|
|
116
|
+
const def = v.defs.at(-1);
|
|
117
|
+
if (def == null) return unit;
|
|
118
|
+
if (def.type === DefinitionType.Variable) return def.node.init;
|
|
119
|
+
if (def.type === DefinitionType.Parameter) return unit;
|
|
120
|
+
if (def.type === DefinitionType.ImportBinding) return unit;
|
|
121
|
+
return def.node;
|
|
122
|
+
}
|
|
146
123
|
|
|
147
124
|
//#endregion
|
|
148
125
|
//#region src/is-value-equal.ts
|
|
@@ -170,8 +147,19 @@ function isValueEqual(a, b, initialScopes) {
|
|
|
170
147
|
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
|
|
171
148
|
const aVar = findVariable(a, aScope);
|
|
172
149
|
const bVar = findVariable(b, bScope);
|
|
173
|
-
const
|
|
174
|
-
|
|
150
|
+
const resolve = (variable) => {
|
|
151
|
+
if (variable == null) return unit;
|
|
152
|
+
const def = variable.defs.at(0);
|
|
153
|
+
if (def != null) switch (true) {
|
|
154
|
+
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
|
|
155
|
+
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
|
|
156
|
+
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
|
|
157
|
+
}
|
|
158
|
+
if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
|
|
159
|
+
return unit;
|
|
160
|
+
};
|
|
161
|
+
const aVarInit = resolve(aVar);
|
|
162
|
+
const bVarInit = resolve(bVar);
|
|
175
163
|
const aVarInitParent = aVarInit?.parent;
|
|
176
164
|
const bVarInitParent = bVarInit?.parent;
|
|
177
165
|
const aDef = aVar?.defs.at(0);
|
|
@@ -226,4 +214,4 @@ function isAssignmentTargetEqual(context, a, b) {
|
|
|
226
214
|
}
|
|
227
215
|
|
|
228
216
|
//#endregion
|
|
229
|
-
export { findEnclosingAssignmentTarget, findVariable, getObjectType,
|
|
217
|
+
export { findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual, isValueEqual };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/var",
|
|
3
|
-
"version": "3.0.0-next.
|
|
3
|
+
"version": "3.0.0-next.62",
|
|
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": {
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"@typescript-eslint/types": "canary",
|
|
35
35
|
"@typescript-eslint/utils": "canary",
|
|
36
36
|
"ts-pattern": "^5.9.0",
|
|
37
|
-
"@eslint-react/
|
|
38
|
-
"@eslint-react/
|
|
39
|
-
"@eslint-react/shared": "3.0.0-next.
|
|
37
|
+
"@eslint-react/ast": "3.0.0-next.62",
|
|
38
|
+
"@eslint-react/eff": "3.0.0-next.62",
|
|
39
|
+
"@eslint-react/shared": "3.0.0-next.62"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"tsdown": "^0.21.0-beta.2",
|