@eslint-react/var 3.0.0-next.62 → 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 +34 -51
- 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:
|
|
@@ -87,20 +51,20 @@ function getObjectType(node, initialScope) {
|
|
|
87
51
|
case AST_NODE_TYPES.Identifier: {
|
|
88
52
|
const initNode = resolve(initialScope.set.get(node.name));
|
|
89
53
|
if (initNode == null) return unit;
|
|
90
|
-
return
|
|
54
|
+
return computeObjectType(initNode, initialScope);
|
|
91
55
|
}
|
|
92
56
|
case AST_NODE_TYPES.MemberExpression:
|
|
93
57
|
if (!("object" in node)) return unit;
|
|
94
|
-
return
|
|
58
|
+
return computeObjectType(node.object, initialScope);
|
|
95
59
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
96
60
|
case AST_NODE_TYPES.AssignmentPattern:
|
|
97
61
|
if (!("right" in node)) return unit;
|
|
98
|
-
return
|
|
99
|
-
case AST_NODE_TYPES.LogicalExpression: return
|
|
100
|
-
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);
|
|
101
65
|
case AST_NODE_TYPES.SequenceExpression:
|
|
102
66
|
if (node.expressions.length === 0) return unit;
|
|
103
|
-
return
|
|
67
|
+
return computeObjectType(node.expressions[node.expressions.length - 1], initialScope);
|
|
104
68
|
case AST_NODE_TYPES.CallExpression: return {
|
|
105
69
|
kind: "unknown",
|
|
106
70
|
node,
|
|
@@ -108,7 +72,7 @@ function getObjectType(node, initialScope) {
|
|
|
108
72
|
};
|
|
109
73
|
default:
|
|
110
74
|
if (!("expression" in node) || typeof node.expression !== "object") return unit;
|
|
111
|
-
return
|
|
75
|
+
return computeObjectType(node.expression, initialScope);
|
|
112
76
|
}
|
|
113
77
|
}
|
|
114
78
|
function resolve(v) {
|
|
@@ -121,6 +85,25 @@ function resolve(v) {
|
|
|
121
85
|
return def.node;
|
|
122
86
|
}
|
|
123
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);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
124
107
|
//#endregion
|
|
125
108
|
//#region src/is-value-equal.ts
|
|
126
109
|
const thisBlockTypes = [
|
|
@@ -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/ast": "3.0.0-next.
|
|
38
|
-
"@eslint-react/eff": "3.0.0-next.
|
|
39
|
-
"@eslint-react/shared": "3.0.0-next.
|
|
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",
|