@eslint-react/var 3.0.0-next.61 → 3.0.0-next.63
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 +18 -33
- package/dist/index.js +43 -60
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,9 @@
|
|
|
1
1
|
import { unit } from "@eslint-react/eff";
|
|
2
|
+
import { Scope } from "@typescript-eslint/scope-manager";
|
|
2
3
|
import { TSESTree } from "@typescript-eslint/types";
|
|
3
|
-
import { Scope, Variable } from "@typescript-eslint/scope-manager";
|
|
4
4
|
import { RuleContext } from "@eslint-react/shared";
|
|
5
5
|
|
|
6
|
-
//#region src/
|
|
7
|
-
/**
|
|
8
|
-
* Finds the enclosing assignment target (variable, property, etc.) for a given node
|
|
9
|
-
*
|
|
10
|
-
* @todo Verify correctness and completeness of this function
|
|
11
|
-
* @param node The starting node
|
|
12
|
-
* @returns The enclosing assignment target node, or undefined if not found
|
|
13
|
-
*/
|
|
14
|
-
declare function findEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression | undefined;
|
|
15
|
-
/**
|
|
16
|
-
* Type representing the possible assignment targets returned by `findEnclosingAssignmentTarget`
|
|
17
|
-
*/
|
|
18
|
-
type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>;
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/find-variable.d.ts
|
|
21
|
-
/**
|
|
22
|
-
* Find a variable by name or identifier node in the scope chain
|
|
23
|
-
* @param initialScope The scope to start searching from
|
|
24
|
-
* @returns The found variable or unit if not found
|
|
25
|
-
* @overload
|
|
26
|
-
* @param nameOrNode The variable name or identifier node to find
|
|
27
|
-
* @param initialScope The scope to start searching from
|
|
28
|
-
* @returns The found variable or unit if not found
|
|
29
|
-
*/
|
|
30
|
-
declare const findVariable: {
|
|
31
|
-
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | unit) => Variable | unit;
|
|
32
|
-
(nameOrNode: string | TSESTree.Identifier | unit, initialScope: Scope): Variable | unit;
|
|
33
|
-
};
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region src/get-object-type.d.ts
|
|
6
|
+
//#region src/compute-object-type.d.ts
|
|
36
7
|
/**
|
|
37
8
|
* Represents the type classification of an object node
|
|
38
9
|
*/
|
|
@@ -68,7 +39,21 @@ type ObjectType = {
|
|
|
68
39
|
* @param initialScope The initial scope to check for variable declarations
|
|
69
40
|
* @returns The ObjectType of the node, or undefined if not detected
|
|
70
41
|
*/
|
|
71
|
-
declare function
|
|
42
|
+
declare function computeObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit;
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/find-enclosing-assignment-target.d.ts
|
|
45
|
+
/**
|
|
46
|
+
* Finds the enclosing assignment target (variable, property, etc.) for a given node
|
|
47
|
+
*
|
|
48
|
+
* @todo Verify correctness and completeness of this function
|
|
49
|
+
* @param node The starting node
|
|
50
|
+
* @returns The enclosing assignment target node, or undefined if not found
|
|
51
|
+
*/
|
|
52
|
+
declare function findEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpression | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.PrivateIdentifier | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldExpression | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Type representing the possible assignment targets returned by `findEnclosingAssignmentTarget`
|
|
55
|
+
*/
|
|
56
|
+
type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>;
|
|
72
57
|
//#endregion
|
|
73
58
|
//#region src/is-assignment-target-equal.d.ts
|
|
74
59
|
/**
|
|
@@ -92,4 +77,4 @@ declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node,
|
|
|
92
77
|
*/
|
|
93
78
|
declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
|
|
94
79
|
//#endregion
|
|
95
|
-
export { AssignmentTarget, ObjectType,
|
|
80
|
+
export { AssignmentTarget, ObjectType, computeObjectType, findEnclosingAssignmentTarget, isAssignmentTargetEqual, isValueEqual };
|
package/dist/index.js
CHANGED
|
@@ -1,53 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
3
|
-
import * as astUtils from "@typescript-eslint/utils/ast-utils";
|
|
4
|
-
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
1
|
+
import { unit } from "@eslint-react/eff";
|
|
5
2
|
import { DefinitionType } from "@typescript-eslint/scope-manager";
|
|
3
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
6
4
|
import * as ast from "@eslint-react/ast";
|
|
5
|
+
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
7
6
|
|
|
8
|
-
//#region src/
|
|
9
|
-
/**
|
|
10
|
-
* Finds the enclosing assignment target (variable, property, etc.) for a given node
|
|
11
|
-
*
|
|
12
|
-
* @todo Verify correctness and completeness of this function
|
|
13
|
-
* @param node The starting node
|
|
14
|
-
* @returns The enclosing assignment target node, or undefined if not found
|
|
15
|
-
*/
|
|
16
|
-
function findEnclosingAssignmentTarget(node) {
|
|
17
|
-
switch (true) {
|
|
18
|
-
case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id;
|
|
19
|
-
case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left;
|
|
20
|
-
case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key;
|
|
21
|
-
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
22
|
-
default: return findEnclosingAssignmentTarget(node.parent);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/find-variable.ts
|
|
28
|
-
/**
|
|
29
|
-
* Find a variable by name or identifier node in the scope chain
|
|
30
|
-
* @param initialScope The scope to start searching from
|
|
31
|
-
* @returns The found variable or unit if not found
|
|
32
|
-
* @overload
|
|
33
|
-
* @param nameOrNode The variable name or identifier node to find
|
|
34
|
-
* @param initialScope The scope to start searching from
|
|
35
|
-
* @returns The found variable or unit if not found
|
|
36
|
-
*/
|
|
37
|
-
const findVariable = dual(2, (nameOrNode, initialScope) => {
|
|
38
|
-
if (nameOrNode == null) return unit;
|
|
39
|
-
return astUtils.findVariable(initialScope, nameOrNode) ?? unit;
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/get-object-type.ts
|
|
7
|
+
//#region src/compute-object-type.ts
|
|
44
8
|
/**
|
|
45
9
|
* Detect the ObjectType of a given node
|
|
46
10
|
* @param node The node to check
|
|
47
11
|
* @param initialScope The initial scope to check for variable declarations
|
|
48
12
|
* @returns The ObjectType of the node, or undefined if not detected
|
|
49
13
|
*/
|
|
50
|
-
function
|
|
14
|
+
function computeObjectType(node, initialScope) {
|
|
51
15
|
if (node == null) return unit;
|
|
52
16
|
switch (node.type) {
|
|
53
17
|
case AST_NODE_TYPES.JSXElement:
|
|
@@ -85,31 +49,22 @@ function getObjectType(node, initialScope) {
|
|
|
85
49
|
};
|
|
86
50
|
return unit;
|
|
87
51
|
case AST_NODE_TYPES.Identifier: {
|
|
88
|
-
function resolve(v) {
|
|
89
|
-
if (v == null) return unit;
|
|
90
|
-
const def = v.defs.at(-1);
|
|
91
|
-
if (def == null) return unit;
|
|
92
|
-
if (def.type === DefinitionType.Variable) return def.node.init;
|
|
93
|
-
if (def.type === DefinitionType.Parameter) return unit;
|
|
94
|
-
if (def.type === DefinitionType.ImportBinding) return unit;
|
|
95
|
-
return def.node;
|
|
96
|
-
}
|
|
97
52
|
const initNode = resolve(initialScope.set.get(node.name));
|
|
98
53
|
if (initNode == null) return unit;
|
|
99
|
-
return
|
|
54
|
+
return computeObjectType(initNode, initialScope);
|
|
100
55
|
}
|
|
101
56
|
case AST_NODE_TYPES.MemberExpression:
|
|
102
57
|
if (!("object" in node)) return unit;
|
|
103
|
-
return
|
|
58
|
+
return computeObjectType(node.object, initialScope);
|
|
104
59
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
105
60
|
case AST_NODE_TYPES.AssignmentPattern:
|
|
106
61
|
if (!("right" in node)) return unit;
|
|
107
|
-
return
|
|
108
|
-
case AST_NODE_TYPES.LogicalExpression: return
|
|
109
|
-
case AST_NODE_TYPES.ConditionalExpression: return
|
|
62
|
+
return computeObjectType(node.right, initialScope);
|
|
63
|
+
case AST_NODE_TYPES.LogicalExpression: return computeObjectType(node.right, initialScope);
|
|
64
|
+
case AST_NODE_TYPES.ConditionalExpression: return computeObjectType(node.consequent, initialScope) ?? computeObjectType(node.alternate, initialScope);
|
|
110
65
|
case AST_NODE_TYPES.SequenceExpression:
|
|
111
66
|
if (node.expressions.length === 0) return unit;
|
|
112
|
-
return
|
|
67
|
+
return computeObjectType(node.expressions[node.expressions.length - 1], initialScope);
|
|
113
68
|
case AST_NODE_TYPES.CallExpression: return {
|
|
114
69
|
kind: "unknown",
|
|
115
70
|
node,
|
|
@@ -117,7 +72,35 @@ function getObjectType(node, initialScope) {
|
|
|
117
72
|
};
|
|
118
73
|
default:
|
|
119
74
|
if (!("expression" in node) || typeof node.expression !== "object") return unit;
|
|
120
|
-
return
|
|
75
|
+
return computeObjectType(node.expression, initialScope);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function resolve(v) {
|
|
79
|
+
if (v == null) return unit;
|
|
80
|
+
const def = v.defs.at(-1);
|
|
81
|
+
if (def == null) return unit;
|
|
82
|
+
if (def.type === DefinitionType.Variable) return def.node.init;
|
|
83
|
+
if (def.type === DefinitionType.Parameter) return unit;
|
|
84
|
+
if (def.type === DefinitionType.ImportBinding) return unit;
|
|
85
|
+
return def.node;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/find-enclosing-assignment-target.ts
|
|
90
|
+
/**
|
|
91
|
+
* Finds the enclosing assignment target (variable, property, etc.) for a given node
|
|
92
|
+
*
|
|
93
|
+
* @todo Verify correctness and completeness of this function
|
|
94
|
+
* @param node The starting node
|
|
95
|
+
* @returns The enclosing assignment target node, or undefined if not found
|
|
96
|
+
*/
|
|
97
|
+
function findEnclosingAssignmentTarget(node) {
|
|
98
|
+
switch (true) {
|
|
99
|
+
case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id;
|
|
100
|
+
case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left;
|
|
101
|
+
case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key;
|
|
102
|
+
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
|
|
103
|
+
default: return findEnclosingAssignmentTarget(node.parent);
|
|
121
104
|
}
|
|
122
105
|
}
|
|
123
106
|
|
|
@@ -145,8 +128,8 @@ function isValueEqual(a, b, initialScopes) {
|
|
|
145
128
|
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
|
|
146
129
|
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
|
|
147
130
|
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
|
|
148
|
-
const aVar = findVariable(
|
|
149
|
-
const bVar = findVariable(
|
|
131
|
+
const aVar = findVariable(aScope, a);
|
|
132
|
+
const bVar = findVariable(bScope, b);
|
|
150
133
|
const resolve = (variable) => {
|
|
151
134
|
if (variable == null) return unit;
|
|
152
135
|
const def = variable.defs.at(0);
|
|
@@ -214,4 +197,4 @@ function isAssignmentTargetEqual(context, a, b) {
|
|
|
214
197
|
}
|
|
215
198
|
|
|
216
199
|
//#endregion
|
|
217
|
-
export {
|
|
200
|
+
export { computeObjectType, findEnclosingAssignmentTarget, 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.63",
|
|
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/eff": "3.0.0-next.
|
|
39
|
-
"@eslint-react/
|
|
37
|
+
"@eslint-react/ast": "3.0.0-next.63",
|
|
38
|
+
"@eslint-react/eff": "3.0.0-next.63",
|
|
39
|
+
"@eslint-react/shared": "3.0.0-next.63"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"tsdown": "^0.21.0-beta.2",
|