@eslint-react/var 3.0.0-next.65 → 3.0.0-next.67
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 +9 -6
- package/dist/index.js +14 -9
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Scope } from "@typescript-eslint/scope-manager";
|
|
2
1
|
import { TSESTree } from "@typescript-eslint/types";
|
|
3
2
|
import { RuleContext } from "@eslint-react/shared";
|
|
4
3
|
|
|
@@ -69,12 +68,12 @@ declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node,
|
|
|
69
68
|
//#region src/is-value-equal.d.ts
|
|
70
69
|
/**
|
|
71
70
|
* Determine whether node value equals to another node value
|
|
71
|
+
* @param context rule context
|
|
72
72
|
* @param a node to compare
|
|
73
73
|
* @param b node to compare
|
|
74
|
-
* @param initialScopes initial scopes of the two nodes
|
|
75
74
|
* @returns `true` if node value equal
|
|
76
75
|
*/
|
|
77
|
-
declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node
|
|
76
|
+
declare function isValueEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
|
|
78
77
|
//#endregion
|
|
79
78
|
//#region src/resolve.d.ts
|
|
80
79
|
/**
|
|
@@ -99,13 +98,17 @@ declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes:
|
|
|
99
98
|
*
|
|
100
99
|
* @param context The ESLint rule context used for scope lookup.
|
|
101
100
|
* @param node The identifier to resolve.
|
|
102
|
-
* @param
|
|
103
|
-
*
|
|
101
|
+
* @param options Optional settings:
|
|
102
|
+
* - `at`: Index of the definition to resolve (default: `0` for the first definition).
|
|
103
|
+
* - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
|
|
104
104
|
* will miss variables declared in an outer scope). When `false` (default), traverse the scope
|
|
105
105
|
* chain upward via `findVariable` so that references to outer-scope bindings are resolved
|
|
106
106
|
* correctly.
|
|
107
107
|
* @returns The resolved node, or `null` if the identifier cannot be resolved to a value node.
|
|
108
108
|
*/
|
|
109
|
-
declare function resolve(context: RuleContext, node: TSESTree.Identifier,
|
|
109
|
+
declare function resolve(context: RuleContext, node: TSESTree.Identifier, options?: Partial<{
|
|
110
|
+
at: number;
|
|
111
|
+
localOnly: boolean;
|
|
112
|
+
}>): TSESTree.Node | null;
|
|
110
113
|
//#endregion
|
|
111
114
|
export { AssignmentTarget, ObjectType, computeObjectType, findEnclosingAssignmentTarget, isAssignmentTargetEqual, isValueEqual, resolve };
|
package/dist/index.js
CHANGED
|
@@ -26,14 +26,16 @@ import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils
|
|
|
26
26
|
*
|
|
27
27
|
* @param context The ESLint rule context used for scope lookup.
|
|
28
28
|
* @param node The identifier to resolve.
|
|
29
|
-
* @param
|
|
30
|
-
*
|
|
29
|
+
* @param options Optional settings:
|
|
30
|
+
* - `at`: Index of the definition to resolve (default: `0` for the first definition).
|
|
31
|
+
* - `localOnly`: If `true`, only consider variables declared in the same scope as the identifier
|
|
31
32
|
* will miss variables declared in an outer scope). When `false` (default), traverse the scope
|
|
32
33
|
* chain upward via `findVariable` so that references to outer-scope bindings are resolved
|
|
33
34
|
* correctly.
|
|
34
35
|
* @returns The resolved node, or `null` if the identifier cannot be resolved to a value node.
|
|
35
36
|
*/
|
|
36
|
-
function resolve(context, node,
|
|
37
|
+
function resolve(context, node, options) {
|
|
38
|
+
const { at = 0, localOnly = false } = options ?? {};
|
|
37
39
|
const scope = context.sourceCode.getScope(node);
|
|
38
40
|
const variable = localOnly ? scope.set.get(node.name) : findVariable(scope, node);
|
|
39
41
|
if (variable == null) return null;
|
|
@@ -107,7 +109,10 @@ function computeObjectType(context, node) {
|
|
|
107
109
|
return null;
|
|
108
110
|
case AST_NODE_TYPES.Identifier: {
|
|
109
111
|
if ((context.sourceCode.getScope(node).set.get(node.name)?.defs.at(-1))?.type === DefinitionType.Parameter) return null;
|
|
110
|
-
const initNode = resolve(context, node,
|
|
112
|
+
const initNode = resolve(context, node, {
|
|
113
|
+
at: -1,
|
|
114
|
+
localOnly: true
|
|
115
|
+
});
|
|
111
116
|
if (initNode == null) return null;
|
|
112
117
|
return computeObjectType(context, initNode);
|
|
113
118
|
}
|
|
@@ -163,15 +168,15 @@ const thisBlockTypes = [
|
|
|
163
168
|
];
|
|
164
169
|
/**
|
|
165
170
|
* Determine whether node value equals to another node value
|
|
171
|
+
* @param context rule context
|
|
166
172
|
* @param a node to compare
|
|
167
173
|
* @param b node to compare
|
|
168
|
-
* @param initialScopes initial scopes of the two nodes
|
|
169
174
|
* @returns `true` if node value equal
|
|
170
175
|
*/
|
|
171
|
-
function isValueEqual(a, b
|
|
176
|
+
function isValueEqual(context, a, b) {
|
|
172
177
|
a = ast.isTypeExpression(a) ? ast.getUnderlyingExpression(a) : a;
|
|
173
178
|
b = ast.isTypeExpression(b) ? ast.getUnderlyingExpression(b) : b;
|
|
174
|
-
const [aScope, bScope] =
|
|
179
|
+
const [aScope, bScope] = [context.sourceCode.getScope(a), context.sourceCode.getScope(b)];
|
|
175
180
|
switch (true) {
|
|
176
181
|
case a === b: return true;
|
|
177
182
|
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
|
|
@@ -218,7 +223,7 @@ function isValueEqual(a, b, initialScopes) {
|
|
|
218
223
|
default: return aVar != null && bVar != null && aVar === bVar;
|
|
219
224
|
}
|
|
220
225
|
}
|
|
221
|
-
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(a.object, b.object
|
|
226
|
+
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(context, a.object, b.object);
|
|
222
227
|
case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
|
|
223
228
|
if (aScope.block === bScope.block) return true;
|
|
224
229
|
return ast.findParentNode(a, ast.isOneOf(thisBlockTypes)) === ast.findParentNode(b, ast.isOneOf(thisBlockTypes));
|
|
@@ -242,7 +247,7 @@ function isValueEqual(a, b, initialScopes) {
|
|
|
242
247
|
* @internal
|
|
243
248
|
*/
|
|
244
249
|
function isAssignmentTargetEqual(context, a, b) {
|
|
245
|
-
return ast.isNodeEqual(a, b) || isValueEqual(
|
|
250
|
+
return ast.isNodeEqual(a, b) || isValueEqual(context, a, b);
|
|
246
251
|
}
|
|
247
252
|
|
|
248
253
|
//#endregion
|
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.67",
|
|
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.67",
|
|
38
|
+
"@eslint-react/eff": "3.0.0-next.67",
|
|
39
|
+
"@eslint-react/shared": "3.0.0-next.67"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@typescript-eslint/typescript-estree": "canary",
|