@eslint-react/var 1.48.2-beta.0 → 1.48.2-next.1
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.mts +30 -38
- package/dist/index.d.ts +30 -38
- package/dist/index.js +70 -76
- package/dist/index.mjs +70 -76
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import { TSESTree } from '@typescript-eslint/types';
|
|
2
1
|
import { Scope, Variable } from '@typescript-eslint/scope-manager';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/types';
|
|
3
3
|
import { _ } from '@eslint-react/eff';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
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
|
|
7
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] | _;
|
|
16
|
+
|
|
17
|
+
declare const ConstructionDetectionHint: {
|
|
18
|
+
None: bigint;
|
|
19
|
+
StrictCallExpression: bigint;
|
|
20
|
+
};
|
|
8
21
|
type Construction = {
|
|
9
22
|
kind: "ArrayExpression";
|
|
10
23
|
node: TSESTree.ArrayExpression;
|
|
@@ -33,7 +46,6 @@ type Construction = {
|
|
|
33
46
|
kind: "RegExpLiteral";
|
|
34
47
|
node: TSESTree.RegExpLiteral;
|
|
35
48
|
};
|
|
36
|
-
|
|
37
49
|
/**
|
|
38
50
|
* Detects the construction type of a given node.
|
|
39
51
|
* @param node The node to check.
|
|
@@ -41,45 +53,13 @@ type Construction = {
|
|
|
41
53
|
* @param hint Optional hint to control the detection behavior.
|
|
42
54
|
* @returns The construction type of the node, or `_` if not found.
|
|
43
55
|
*/
|
|
44
|
-
declare function
|
|
45
|
-
|
|
46
|
-
type ConstructionDetectionHint = bigint;
|
|
47
|
-
declare const ConstructionDetectionHint: {
|
|
48
|
-
None: bigint;
|
|
49
|
-
StrictCallExpression: bigint;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
declare function findPropertyInProperties(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | _;
|
|
53
|
-
|
|
54
|
-
declare const findVariable: {
|
|
55
|
-
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | _) => Variable | _;
|
|
56
|
-
(nameOrNode: string | TSESTree.Identifier | _, initialScope: Scope): Variable | _;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
declare function getChidScopes(scope: Scope): readonly Scope[];
|
|
56
|
+
declare function getConstruction(node: TSESTree.Node | _, initialScope: Scope, hint?: bigint): Construction | _;
|
|
60
57
|
|
|
61
58
|
declare function getVariableDeclaratorId(node: TSESTree.Node | _, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | _;
|
|
62
59
|
|
|
63
60
|
declare function getVariableInitNode(variable: Variable | _, at: number): _ | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
* Get all variables from the given scope up to the global scope
|
|
67
|
-
* @param initialScope The scope to start from
|
|
68
|
-
* @returns All variables from the given scope up to the global scope
|
|
69
|
-
*/
|
|
70
|
-
declare function getVariables(initialScope: Scope): Variable[];
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Determines whether node value equals to another node value
|
|
74
|
-
* @param a node to compare
|
|
75
|
-
* @param b node to compare
|
|
76
|
-
* @param initialScopes initial scopes of the two nodes
|
|
77
|
-
* @returns `true` if node value equal
|
|
78
|
-
*/
|
|
79
|
-
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
80
|
-
aScope: Scope,
|
|
81
|
-
bScope: Scope
|
|
82
|
-
]): boolean;
|
|
62
|
+
declare function getChidScopes(scope: Scope): readonly Scope[];
|
|
83
63
|
|
|
84
64
|
type LazyValue = {
|
|
85
65
|
kind: "lazy";
|
|
@@ -107,4 +87,16 @@ declare function toStaticValue(lazyValue: LazyValue): {
|
|
|
107
87
|
readonly value: unknown;
|
|
108
88
|
};
|
|
109
89
|
|
|
110
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Determines whether node value equals to another node value
|
|
92
|
+
* @param a node to compare
|
|
93
|
+
* @param b node to compare
|
|
94
|
+
* @param initialScopes initial scopes of the two nodes
|
|
95
|
+
* @returns `true` if node value equal
|
|
96
|
+
*/
|
|
97
|
+
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
98
|
+
aScope: Scope,
|
|
99
|
+
bScope: Scope
|
|
100
|
+
]): boolean;
|
|
101
|
+
|
|
102
|
+
export { type Construction, ConstructionDetectionHint, type LazyValue, findPropertyInProperties, findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import { TSESTree } from '@typescript-eslint/types';
|
|
2
1
|
import { Scope, Variable } from '@typescript-eslint/scope-manager';
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/types';
|
|
3
3
|
import { _ } from '@eslint-react/eff';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
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
|
|
7
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] | _;
|
|
16
|
+
|
|
17
|
+
declare const ConstructionDetectionHint: {
|
|
18
|
+
None: bigint;
|
|
19
|
+
StrictCallExpression: bigint;
|
|
20
|
+
};
|
|
8
21
|
type Construction = {
|
|
9
22
|
kind: "ArrayExpression";
|
|
10
23
|
node: TSESTree.ArrayExpression;
|
|
@@ -33,7 +46,6 @@ type Construction = {
|
|
|
33
46
|
kind: "RegExpLiteral";
|
|
34
47
|
node: TSESTree.RegExpLiteral;
|
|
35
48
|
};
|
|
36
|
-
|
|
37
49
|
/**
|
|
38
50
|
* Detects the construction type of a given node.
|
|
39
51
|
* @param node The node to check.
|
|
@@ -41,45 +53,13 @@ type Construction = {
|
|
|
41
53
|
* @param hint Optional hint to control the detection behavior.
|
|
42
54
|
* @returns The construction type of the node, or `_` if not found.
|
|
43
55
|
*/
|
|
44
|
-
declare function
|
|
45
|
-
|
|
46
|
-
type ConstructionDetectionHint = bigint;
|
|
47
|
-
declare const ConstructionDetectionHint: {
|
|
48
|
-
None: bigint;
|
|
49
|
-
StrictCallExpression: bigint;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
declare function findPropertyInProperties(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | _;
|
|
53
|
-
|
|
54
|
-
declare const findVariable: {
|
|
55
|
-
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | _) => Variable | _;
|
|
56
|
-
(nameOrNode: string | TSESTree.Identifier | _, initialScope: Scope): Variable | _;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
declare function getChidScopes(scope: Scope): readonly Scope[];
|
|
56
|
+
declare function getConstruction(node: TSESTree.Node | _, initialScope: Scope, hint?: bigint): Construction | _;
|
|
60
57
|
|
|
61
58
|
declare function getVariableDeclaratorId(node: TSESTree.Node | _, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | _;
|
|
62
59
|
|
|
63
60
|
declare function getVariableInitNode(variable: Variable | _, at: number): _ | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
* Get all variables from the given scope up to the global scope
|
|
67
|
-
* @param initialScope The scope to start from
|
|
68
|
-
* @returns All variables from the given scope up to the global scope
|
|
69
|
-
*/
|
|
70
|
-
declare function getVariables(initialScope: Scope): Variable[];
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Determines whether node value equals to another node value
|
|
74
|
-
* @param a node to compare
|
|
75
|
-
* @param b node to compare
|
|
76
|
-
* @param initialScopes initial scopes of the two nodes
|
|
77
|
-
* @returns `true` if node value equal
|
|
78
|
-
*/
|
|
79
|
-
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
80
|
-
aScope: Scope,
|
|
81
|
-
bScope: Scope
|
|
82
|
-
]): boolean;
|
|
62
|
+
declare function getChidScopes(scope: Scope): readonly Scope[];
|
|
83
63
|
|
|
84
64
|
type LazyValue = {
|
|
85
65
|
kind: "lazy";
|
|
@@ -107,4 +87,16 @@ declare function toStaticValue(lazyValue: LazyValue): {
|
|
|
107
87
|
readonly value: unknown;
|
|
108
88
|
};
|
|
109
89
|
|
|
110
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Determines whether node value equals to another node value
|
|
92
|
+
* @param a node to compare
|
|
93
|
+
* @param b node to compare
|
|
94
|
+
* @param initialScopes initial scopes of the two nodes
|
|
95
|
+
* @returns `true` if node value equal
|
|
96
|
+
*/
|
|
97
|
+
declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [
|
|
98
|
+
aScope: Scope,
|
|
99
|
+
bScope: Scope
|
|
100
|
+
]): boolean;
|
|
101
|
+
|
|
102
|
+
export { type Construction, ConstructionDetectionHint, type LazyValue, findPropertyInProperties, findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var eff = require('@eslint-react/eff');
|
|
4
|
-
var types = require('@typescript-eslint/types');
|
|
5
4
|
var scopeManager = require('@typescript-eslint/scope-manager');
|
|
5
|
+
var types = require('@typescript-eslint/types');
|
|
6
6
|
var ASTUtils = require('@typescript-eslint/utils/ast-utils');
|
|
7
7
|
var AST = require('@eslint-react/ast');
|
|
8
8
|
|
|
@@ -27,7 +27,7 @@ function _interopNamespace(e) {
|
|
|
27
27
|
var ASTUtils__namespace = /*#__PURE__*/_interopNamespace(ASTUtils);
|
|
28
28
|
var AST__namespace = /*#__PURE__*/_interopNamespace(AST);
|
|
29
29
|
|
|
30
|
-
// src/
|
|
30
|
+
// src/var-collect.ts
|
|
31
31
|
function getVariableInitNode(variable, at) {
|
|
32
32
|
if (variable == null) return eff._;
|
|
33
33
|
const def = variable.defs.at(at);
|
|
@@ -44,14 +44,62 @@ function getVariableInitNode(variable, at) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
// src/
|
|
47
|
+
// src/var-collect.ts
|
|
48
|
+
function getVariables(initialScope) {
|
|
49
|
+
let scope = initialScope;
|
|
50
|
+
const variables = [...scope.variables];
|
|
51
|
+
while (scope.type !== scopeManager.ScopeType.global) {
|
|
52
|
+
scope = scope.upper;
|
|
53
|
+
variables.push(...scope.variables);
|
|
54
|
+
}
|
|
55
|
+
return variables.reverse();
|
|
56
|
+
}
|
|
57
|
+
var findVariable2 = eff.dual(2, (nameOrNode, initialScope) => {
|
|
58
|
+
if (nameOrNode == null) return eff._;
|
|
59
|
+
return ASTUtils__namespace.findVariable(initialScope, nameOrNode) ?? eff._;
|
|
60
|
+
});
|
|
61
|
+
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
62
|
+
return properties.findLast((prop) => {
|
|
63
|
+
if (prop.type === types.AST_NODE_TYPES.Property) {
|
|
64
|
+
return "name" in prop.key && prop.key.name === name;
|
|
65
|
+
}
|
|
66
|
+
if (prop.type === types.AST_NODE_TYPES.SpreadElement) {
|
|
67
|
+
switch (prop.argument.type) {
|
|
68
|
+
case types.AST_NODE_TYPES.Identifier: {
|
|
69
|
+
if (seen.has(prop.argument.name)) return false;
|
|
70
|
+
const variable = findVariable2(prop.argument.name, initialScope);
|
|
71
|
+
const variableNode = getVariableInitNode(variable, 0);
|
|
72
|
+
if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
|
|
73
|
+
seen.add(prop.argument.name);
|
|
74
|
+
return findPropertyInProperties(
|
|
75
|
+
name,
|
|
76
|
+
variableNode.properties,
|
|
77
|
+
initialScope,
|
|
78
|
+
seen
|
|
79
|
+
) != null;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
case types.AST_NODE_TYPES.ObjectExpression: {
|
|
84
|
+
return findPropertyInProperties(
|
|
85
|
+
name,
|
|
86
|
+
prop.argument.properties,
|
|
87
|
+
initialScope,
|
|
88
|
+
seen
|
|
89
|
+
) != null;
|
|
90
|
+
}
|
|
91
|
+
default:
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
48
98
|
var ConstructionDetectionHint = {
|
|
49
99
|
None: 0n,
|
|
50
100
|
StrictCallExpression: 1n << 0n
|
|
51
101
|
};
|
|
52
|
-
|
|
53
|
-
// src/construction/construction-detection.ts
|
|
54
|
-
function getConstructionDetectionResult(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
102
|
+
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
55
103
|
if (node == null) return eff._;
|
|
56
104
|
switch (node.type) {
|
|
57
105
|
case types.AST_NODE_TYPES.JSXElement:
|
|
@@ -76,22 +124,22 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
76
124
|
}
|
|
77
125
|
case types.AST_NODE_TYPES.MemberExpression: {
|
|
78
126
|
if (!("object" in node)) return eff._;
|
|
79
|
-
return
|
|
127
|
+
return getConstruction(node.object, initialScope, hint);
|
|
80
128
|
}
|
|
81
129
|
case types.AST_NODE_TYPES.AssignmentExpression:
|
|
82
130
|
case types.AST_NODE_TYPES.AssignmentPattern: {
|
|
83
131
|
if (!("right" in node)) return eff._;
|
|
84
|
-
return
|
|
132
|
+
return getConstruction(node.right, initialScope, hint);
|
|
85
133
|
}
|
|
86
134
|
case types.AST_NODE_TYPES.LogicalExpression: {
|
|
87
|
-
const lvc =
|
|
135
|
+
const lvc = getConstruction(node.left, initialScope, hint);
|
|
88
136
|
if (lvc == null) return eff._;
|
|
89
|
-
return
|
|
137
|
+
return getConstruction(node.right, initialScope, hint);
|
|
90
138
|
}
|
|
91
139
|
case types.AST_NODE_TYPES.ConditionalExpression: {
|
|
92
|
-
const cvc =
|
|
140
|
+
const cvc = getConstruction(node.consequent, initialScope, hint);
|
|
93
141
|
if (cvc == null) return eff._;
|
|
94
|
-
return
|
|
142
|
+
return getConstruction(node.alternate, initialScope, hint);
|
|
95
143
|
}
|
|
96
144
|
case types.AST_NODE_TYPES.Identifier: {
|
|
97
145
|
if (!("name" in node) || typeof node.name !== "string") {
|
|
@@ -99,7 +147,7 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
99
147
|
}
|
|
100
148
|
const variable = initialScope.set.get(node.name);
|
|
101
149
|
const variableNode = getVariableInitNode(variable, -1);
|
|
102
|
-
return
|
|
150
|
+
return getConstruction(variableNode, initialScope, hint);
|
|
103
151
|
}
|
|
104
152
|
case types.AST_NODE_TYPES.Literal: {
|
|
105
153
|
if ("regex" in node) {
|
|
@@ -111,62 +159,10 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
111
159
|
if (!("expression" in node) || typeof node.expression !== "object") {
|
|
112
160
|
return eff._;
|
|
113
161
|
}
|
|
114
|
-
return
|
|
162
|
+
return getConstruction(node.expression, initialScope, hint);
|
|
115
163
|
}
|
|
116
164
|
}
|
|
117
165
|
}
|
|
118
|
-
var findVariable2 = eff.dual(2, (nameOrNode, initialScope) => {
|
|
119
|
-
if (nameOrNode == null) return eff._;
|
|
120
|
-
return ASTUtils__namespace.findVariable(initialScope, nameOrNode) ?? eff._;
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// src/find-property-in-properties.ts
|
|
124
|
-
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
125
|
-
return properties.findLast((prop) => {
|
|
126
|
-
if (prop.type === types.AST_NODE_TYPES.Property) {
|
|
127
|
-
return "name" in prop.key && prop.key.name === name;
|
|
128
|
-
}
|
|
129
|
-
if (prop.type === types.AST_NODE_TYPES.SpreadElement) {
|
|
130
|
-
switch (prop.argument.type) {
|
|
131
|
-
case types.AST_NODE_TYPES.Identifier: {
|
|
132
|
-
if (seen.has(prop.argument.name)) return false;
|
|
133
|
-
const variable = findVariable2(prop.argument.name, initialScope);
|
|
134
|
-
const variableNode = getVariableInitNode(variable, 0);
|
|
135
|
-
if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
|
|
136
|
-
seen.add(prop.argument.name);
|
|
137
|
-
return findPropertyInProperties(
|
|
138
|
-
name,
|
|
139
|
-
variableNode.properties,
|
|
140
|
-
initialScope,
|
|
141
|
-
seen
|
|
142
|
-
) != null;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
case types.AST_NODE_TYPES.ObjectExpression: {
|
|
147
|
-
return findPropertyInProperties(
|
|
148
|
-
name,
|
|
149
|
-
prop.argument.properties,
|
|
150
|
-
initialScope,
|
|
151
|
-
seen
|
|
152
|
-
) != null;
|
|
153
|
-
}
|
|
154
|
-
default:
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return false;
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/get-child-scopes.ts
|
|
163
|
-
function getChidScopes(scope) {
|
|
164
|
-
const scopes = [scope];
|
|
165
|
-
for (const childScope of scope.childScopes) {
|
|
166
|
-
scopes.push(...getChidScopes(childScope));
|
|
167
|
-
}
|
|
168
|
-
return scopes;
|
|
169
|
-
}
|
|
170
166
|
function getVariableDeclaratorId(node, prev) {
|
|
171
167
|
if (node == null) return eff._;
|
|
172
168
|
switch (true) {
|
|
@@ -180,14 +176,14 @@ function getVariableDeclaratorId(node, prev) {
|
|
|
180
176
|
return getVariableDeclaratorId(node.parent, node);
|
|
181
177
|
}
|
|
182
178
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
179
|
+
|
|
180
|
+
// src/var-scope.ts
|
|
181
|
+
function getChidScopes(scope) {
|
|
182
|
+
const scopes = [scope];
|
|
183
|
+
for (const childScope of scope.childScopes) {
|
|
184
|
+
scopes.push(...getChidScopes(childScope));
|
|
189
185
|
}
|
|
190
|
-
return
|
|
186
|
+
return scopes;
|
|
191
187
|
}
|
|
192
188
|
function toStaticValue(lazyValue) {
|
|
193
189
|
const { kind, node, initialScope } = lazyValue;
|
|
@@ -197,8 +193,6 @@ function toStaticValue(lazyValue) {
|
|
|
197
193
|
const staticValue = initialScope == null ? ASTUtils.getStaticValue(node) : ASTUtils.getStaticValue(node, initialScope);
|
|
198
194
|
return staticValue == null ? { kind: "none", node, initialScope } : { kind: "some", node, initialScope, value: staticValue.value };
|
|
199
195
|
}
|
|
200
|
-
|
|
201
|
-
// src/is-node-value-equal.ts
|
|
202
196
|
var thisBlockTypes = [
|
|
203
197
|
types.AST_NODE_TYPES.FunctionDeclaration,
|
|
204
198
|
types.AST_NODE_TYPES.FunctionExpression,
|
|
@@ -285,7 +279,7 @@ exports.ConstructionDetectionHint = ConstructionDetectionHint;
|
|
|
285
279
|
exports.findPropertyInProperties = findPropertyInProperties;
|
|
286
280
|
exports.findVariable = findVariable2;
|
|
287
281
|
exports.getChidScopes = getChidScopes;
|
|
288
|
-
exports.
|
|
282
|
+
exports.getConstruction = getConstruction;
|
|
289
283
|
exports.getVariableDeclaratorId = getVariableDeclaratorId;
|
|
290
284
|
exports.getVariableInitNode = getVariableInitNode;
|
|
291
285
|
exports.getVariables = getVariables;
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { dual, _ } from '@eslint-react/eff';
|
|
2
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
3
2
|
import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
|
|
3
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
4
4
|
import * as ASTUtils from '@typescript-eslint/utils/ast-utils';
|
|
5
5
|
import { getStaticValue } from '@typescript-eslint/utils/ast-utils';
|
|
6
6
|
import * as AST from '@eslint-react/ast';
|
|
7
7
|
|
|
8
|
-
// src/
|
|
8
|
+
// src/var-collect.ts
|
|
9
9
|
function getVariableInitNode(variable, at) {
|
|
10
10
|
if (variable == null) return _;
|
|
11
11
|
const def = variable.defs.at(at);
|
|
@@ -22,14 +22,62 @@ function getVariableInitNode(variable, at) {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
// src/
|
|
25
|
+
// src/var-collect.ts
|
|
26
|
+
function getVariables(initialScope) {
|
|
27
|
+
let scope = initialScope;
|
|
28
|
+
const variables = [...scope.variables];
|
|
29
|
+
while (scope.type !== ScopeType.global) {
|
|
30
|
+
scope = scope.upper;
|
|
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
|
+
});
|
|
75
|
+
}
|
|
26
76
|
var ConstructionDetectionHint = {
|
|
27
77
|
None: 0n,
|
|
28
78
|
StrictCallExpression: 1n << 0n
|
|
29
79
|
};
|
|
30
|
-
|
|
31
|
-
// src/construction/construction-detection.ts
|
|
32
|
-
function getConstructionDetectionResult(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
80
|
+
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
|
|
33
81
|
if (node == null) return _;
|
|
34
82
|
switch (node.type) {
|
|
35
83
|
case AST_NODE_TYPES.JSXElement:
|
|
@@ -54,22 +102,22 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
54
102
|
}
|
|
55
103
|
case AST_NODE_TYPES.MemberExpression: {
|
|
56
104
|
if (!("object" in node)) return _;
|
|
57
|
-
return
|
|
105
|
+
return getConstruction(node.object, initialScope, hint);
|
|
58
106
|
}
|
|
59
107
|
case AST_NODE_TYPES.AssignmentExpression:
|
|
60
108
|
case AST_NODE_TYPES.AssignmentPattern: {
|
|
61
109
|
if (!("right" in node)) return _;
|
|
62
|
-
return
|
|
110
|
+
return getConstruction(node.right, initialScope, hint);
|
|
63
111
|
}
|
|
64
112
|
case AST_NODE_TYPES.LogicalExpression: {
|
|
65
|
-
const lvc =
|
|
113
|
+
const lvc = getConstruction(node.left, initialScope, hint);
|
|
66
114
|
if (lvc == null) return _;
|
|
67
|
-
return
|
|
115
|
+
return getConstruction(node.right, initialScope, hint);
|
|
68
116
|
}
|
|
69
117
|
case AST_NODE_TYPES.ConditionalExpression: {
|
|
70
|
-
const cvc =
|
|
118
|
+
const cvc = getConstruction(node.consequent, initialScope, hint);
|
|
71
119
|
if (cvc == null) return _;
|
|
72
|
-
return
|
|
120
|
+
return getConstruction(node.alternate, initialScope, hint);
|
|
73
121
|
}
|
|
74
122
|
case AST_NODE_TYPES.Identifier: {
|
|
75
123
|
if (!("name" in node) || typeof node.name !== "string") {
|
|
@@ -77,7 +125,7 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
77
125
|
}
|
|
78
126
|
const variable = initialScope.set.get(node.name);
|
|
79
127
|
const variableNode = getVariableInitNode(variable, -1);
|
|
80
|
-
return
|
|
128
|
+
return getConstruction(variableNode, initialScope, hint);
|
|
81
129
|
}
|
|
82
130
|
case AST_NODE_TYPES.Literal: {
|
|
83
131
|
if ("regex" in node) {
|
|
@@ -89,62 +137,10 @@ function getConstructionDetectionResult(node, initialScope, hint = ConstructionD
|
|
|
89
137
|
if (!("expression" in node) || typeof node.expression !== "object") {
|
|
90
138
|
return _;
|
|
91
139
|
}
|
|
92
|
-
return
|
|
140
|
+
return getConstruction(node.expression, initialScope, hint);
|
|
93
141
|
}
|
|
94
142
|
}
|
|
95
143
|
}
|
|
96
|
-
var findVariable2 = dual(2, (nameOrNode, initialScope) => {
|
|
97
|
-
if (nameOrNode == null) return _;
|
|
98
|
-
return ASTUtils.findVariable(initialScope, nameOrNode) ?? _;
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// src/find-property-in-properties.ts
|
|
102
|
-
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
|
|
103
|
-
return properties.findLast((prop) => {
|
|
104
|
-
if (prop.type === AST_NODE_TYPES.Property) {
|
|
105
|
-
return "name" in prop.key && prop.key.name === name;
|
|
106
|
-
}
|
|
107
|
-
if (prop.type === AST_NODE_TYPES.SpreadElement) {
|
|
108
|
-
switch (prop.argument.type) {
|
|
109
|
-
case AST_NODE_TYPES.Identifier: {
|
|
110
|
-
if (seen.has(prop.argument.name)) return false;
|
|
111
|
-
const variable = findVariable2(prop.argument.name, initialScope);
|
|
112
|
-
const variableNode = getVariableInitNode(variable, 0);
|
|
113
|
-
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
114
|
-
seen.add(prop.argument.name);
|
|
115
|
-
return findPropertyInProperties(
|
|
116
|
-
name,
|
|
117
|
-
variableNode.properties,
|
|
118
|
-
initialScope,
|
|
119
|
-
seen
|
|
120
|
-
) != null;
|
|
121
|
-
}
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
case AST_NODE_TYPES.ObjectExpression: {
|
|
125
|
-
return findPropertyInProperties(
|
|
126
|
-
name,
|
|
127
|
-
prop.argument.properties,
|
|
128
|
-
initialScope,
|
|
129
|
-
seen
|
|
130
|
-
) != null;
|
|
131
|
-
}
|
|
132
|
-
default:
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return false;
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// src/get-child-scopes.ts
|
|
141
|
-
function getChidScopes(scope) {
|
|
142
|
-
const scopes = [scope];
|
|
143
|
-
for (const childScope of scope.childScopes) {
|
|
144
|
-
scopes.push(...getChidScopes(childScope));
|
|
145
|
-
}
|
|
146
|
-
return scopes;
|
|
147
|
-
}
|
|
148
144
|
function getVariableDeclaratorId(node, prev) {
|
|
149
145
|
if (node == null) return _;
|
|
150
146
|
switch (true) {
|
|
@@ -158,14 +154,14 @@ function getVariableDeclaratorId(node, prev) {
|
|
|
158
154
|
return getVariableDeclaratorId(node.parent, node);
|
|
159
155
|
}
|
|
160
156
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
157
|
+
|
|
158
|
+
// src/var-scope.ts
|
|
159
|
+
function getChidScopes(scope) {
|
|
160
|
+
const scopes = [scope];
|
|
161
|
+
for (const childScope of scope.childScopes) {
|
|
162
|
+
scopes.push(...getChidScopes(childScope));
|
|
167
163
|
}
|
|
168
|
-
return
|
|
164
|
+
return scopes;
|
|
169
165
|
}
|
|
170
166
|
function toStaticValue(lazyValue) {
|
|
171
167
|
const { kind, node, initialScope } = lazyValue;
|
|
@@ -175,8 +171,6 @@ function toStaticValue(lazyValue) {
|
|
|
175
171
|
const staticValue = initialScope == null ? getStaticValue(node) : getStaticValue(node, initialScope);
|
|
176
172
|
return staticValue == null ? { kind: "none", node, initialScope } : { kind: "some", node, initialScope, value: staticValue.value };
|
|
177
173
|
}
|
|
178
|
-
|
|
179
|
-
// src/is-node-value-equal.ts
|
|
180
174
|
var thisBlockTypes = [
|
|
181
175
|
AST_NODE_TYPES.FunctionDeclaration,
|
|
182
176
|
AST_NODE_TYPES.FunctionExpression,
|
|
@@ -259,4 +253,4 @@ function getVariableInitNodeLoose(variable, at) {
|
|
|
259
253
|
return _;
|
|
260
254
|
}
|
|
261
255
|
|
|
262
|
-
export { ConstructionDetectionHint, findPropertyInProperties, findVariable2 as findVariable, getChidScopes,
|
|
256
|
+
export { ConstructionDetectionHint, findPropertyInProperties, findVariable2 as findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/var",
|
|
3
|
-
"version": "1.48.2-
|
|
3
|
+
"version": "1.48.2-next.1",
|
|
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": {
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"@typescript-eslint/utils": "^8.30.1",
|
|
41
41
|
"string-ts": "^2.2.1",
|
|
42
42
|
"ts-pattern": "^5.7.0",
|
|
43
|
-
"@eslint-react/ast": "1.48.2-
|
|
44
|
-
"@eslint-react/eff": "1.48.2-
|
|
43
|
+
"@eslint-react/ast": "1.48.2-next.1",
|
|
44
|
+
"@eslint-react/eff": "1.48.2-next.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"tsup": "^8.4.0",
|