@eslint-react/var 2.2.3 → 2.2.4-next.0

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 CHANGED
@@ -2,56 +2,49 @@ import { unit } from "@eslint-react/eff";
2
2
  import { TSESTree } from "@typescript-eslint/types";
3
3
  import { Scope, Variable } from "@typescript-eslint/scope-manager";
4
4
 
5
- //#region src/construction-detection.d.ts
6
- declare const ConstructionDetectionHint: {
7
- None: bigint;
8
- StrictCallExpression: bigint;
9
- };
10
- type Construction = {
11
- kind: "ArrayExpression";
5
+ //#region src/find-assignment-target.d.ts
6
+ declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
7
+ //#endregion
8
+ //#region src/find-property.d.ts
9
+ declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
10
+ //#endregion
11
+ //#region src/get-child-scopes.d.ts
12
+ declare function getChildScopes(scope: Scope): readonly Scope[];
13
+ //#endregion
14
+ //#region src/get-object-type.d.ts
15
+ type ObjectType = {
16
+ kind: "jsx";
17
+ node: TSESTree.JSXElement | TSESTree.JSXFragment;
18
+ } | {
19
+ kind: "array";
12
20
  node: TSESTree.ArrayExpression;
13
21
  } | {
14
- kind: "CallExpression";
15
- node: TSESTree.CallExpression;
22
+ kind: "plain";
23
+ node: TSESTree.ObjectExpression;
16
24
  } | {
17
- kind: "ClassExpression";
25
+ kind: "class";
18
26
  node: TSESTree.ClassExpression;
19
27
  } | {
20
- kind: "FunctionDeclaration";
21
- node: TSESTree.FunctionDeclaration;
22
- } | {
23
- kind: "FunctionExpression";
24
- node: TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
28
+ kind: "instance";
29
+ node: TSESTree.NewExpression | TSESTree.ThisExpression;
25
30
  } | {
26
- kind: "JSXElement";
27
- node: TSESTree.JSXElement | TSESTree.JSXFragment;
28
- } | {
29
- kind: "NewExpression";
30
- node: TSESTree.NewExpression;
31
- } | {
32
- kind: "ObjectExpression";
33
- node: TSESTree.ObjectExpression;
31
+ kind: "function";
32
+ node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression;
34
33
  } | {
35
- kind: "RegExpLiteral";
34
+ kind: "regexp";
36
35
  node: TSESTree.RegExpLiteral;
36
+ } | {
37
+ kind: "unknown";
38
+ node: TSESTree.Node;
39
+ reason: "call-expression" | "unsupported-node";
37
40
  };
38
41
  /**
39
- * Detects the construction type of a given node.
40
- * @param node The node to check.
41
- * @param initialScope The initial scope to check for variable declarations.
42
- * @param hint Optional hint to control the detection behavior.
43
- * @returns The construction type of the node, or `_` if not found.
42
+ * Detects the ObjectType of a given node
43
+ * @param node The node to check
44
+ * @param initialScope The initial scope to check for variable declarations
45
+ * @returns The ObjectType of the node, or undefined if not detected
44
46
  */
45
- declare function getConstruction(node: TSESTree.Node | unit, initialScope: Scope, hint?: bigint): Construction | unit;
46
- //#endregion
47
- //#region src/find-assignment-target.d.ts
48
- declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
49
- //#endregion
50
- //#region src/find-property.d.ts
51
- declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
52
- //#endregion
53
- //#region src/get-child-scopes.d.ts
54
- declare function getChildScopes(scope: Scope): readonly Scope[];
47
+ declare function getObjectType(node: TSESTree.Node | unit, initialScope: Scope): ObjectType | unit;
55
48
  //#endregion
56
49
  //#region src/get-variable-definition-node.d.ts
57
50
  declare function getVariableDefinitionNode(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.ClassDeclarationWithName | TSESTree.ClassDeclarationWithOptionalName | TSESTree.Expression | TSESTree.FunctionDeclaration | TSESTree.FunctionDeclarationWithName | TSESTree.FunctionDeclarationWithOptionalName;
@@ -79,4 +72,4 @@ declare const findVariable: {
79
72
  */
80
73
  declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
81
74
  //#endregion
82
- export { Construction, ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
75
+ export { ObjectType, findAssignmentTarget, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
package/dist/index.js CHANGED
@@ -5,6 +5,18 @@ import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
5
5
  import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
6
6
  import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
7
7
 
8
+ //#region src/find-assignment-target.ts
9
+ function findAssignmentTarget(node, prev) {
10
+ if (node == null) return unit;
11
+ switch (true) {
12
+ case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
13
+ case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
14
+ case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
15
+ default: return findAssignmentTarget(node.parent, node);
16
+ }
17
+ }
18
+
19
+ //#endregion
8
20
  //#region src/get-variable-definition-node.ts
9
21
  function getVariableDefinitionNode(variable, at) {
10
22
  if (variable == null) return unit;
@@ -26,94 +38,6 @@ function getVariableDefinitionNodeLoose(variable, at) {
26
38
  return unit;
27
39
  }
28
40
 
29
- //#endregion
30
- //#region src/construction-detection.ts
31
- const ConstructionDetectionHint = {
32
- None: 0n,
33
- StrictCallExpression: 1n << 0n
34
- };
35
- /**
36
- * Detects the construction type of a given node.
37
- * @param node The node to check.
38
- * @param initialScope The initial scope to check for variable declarations.
39
- * @param hint Optional hint to control the detection behavior.
40
- * @returns The construction type of the node, or `_` if not found.
41
- */
42
- function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) {
43
- if (node == null) return unit;
44
- switch (node.type) {
45
- case AST_NODE_TYPES.JSXElement:
46
- case AST_NODE_TYPES.JSXFragment: return {
47
- kind: "JSXElement",
48
- node
49
- };
50
- case AST_NODE_TYPES.ArrayExpression: return {
51
- kind: "ArrayExpression",
52
- node
53
- };
54
- case AST_NODE_TYPES.ObjectExpression: return {
55
- kind: "ObjectExpression",
56
- node
57
- };
58
- case AST_NODE_TYPES.ClassExpression: return {
59
- kind: "ClassExpression",
60
- node
61
- };
62
- case AST_NODE_TYPES.NewExpression: return {
63
- kind: "NewExpression",
64
- node
65
- };
66
- case AST_NODE_TYPES.FunctionExpression:
67
- case AST_NODE_TYPES.ArrowFunctionExpression: return {
68
- kind: "FunctionExpression",
69
- node
70
- };
71
- case AST_NODE_TYPES.CallExpression:
72
- if (hint & ConstructionDetectionHint.StrictCallExpression) return {
73
- kind: "CallExpression",
74
- node
75
- };
76
- return unit;
77
- case AST_NODE_TYPES.MemberExpression:
78
- if (!("object" in node)) return unit;
79
- return getConstruction(node.object, initialScope, hint);
80
- case AST_NODE_TYPES.AssignmentExpression:
81
- case AST_NODE_TYPES.AssignmentPattern:
82
- if (!("right" in node)) return unit;
83
- return getConstruction(node.right, initialScope, hint);
84
- case AST_NODE_TYPES.LogicalExpression:
85
- if (getConstruction(node.left, initialScope, hint) == null) return unit;
86
- return getConstruction(node.right, initialScope, hint);
87
- case AST_NODE_TYPES.ConditionalExpression:
88
- if (getConstruction(node.consequent, initialScope, hint) == null) return unit;
89
- return getConstruction(node.alternate, initialScope, hint);
90
- case AST_NODE_TYPES.Identifier:
91
- if (!("name" in node) || typeof node.name !== "string") return unit;
92
- return getConstruction(getVariableDefinitionNode(initialScope.set.get(node.name), -1), initialScope, hint);
93
- case AST_NODE_TYPES.Literal:
94
- if ("regex" in node) return {
95
- kind: "RegExpLiteral",
96
- node
97
- };
98
- return unit;
99
- default:
100
- if (!("expression" in node) || typeof node.expression !== "object") return unit;
101
- return getConstruction(node.expression, initialScope, hint);
102
- }
103
- }
104
-
105
- //#endregion
106
- //#region src/find-assignment-target.ts
107
- function findAssignmentTarget(node, prev) {
108
- if (node == null) return unit;
109
- switch (true) {
110
- case node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev: return node.id;
111
- case node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev: return node.left;
112
- case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
113
- default: return findAssignmentTarget(node.parent, node);
114
- }
115
- }
116
-
117
41
  //#endregion
118
42
  //#region src/get-variables-from-scope.ts
119
43
  /**
@@ -165,6 +89,77 @@ function getChildScopes(scope) {
165
89
  return scopes;
166
90
  }
167
91
 
92
+ //#endregion
93
+ //#region src/get-object-type.ts
94
+ /**
95
+ * Detects the ObjectType of a given node
96
+ * @param node The node to check
97
+ * @param initialScope The initial scope to check for variable declarations
98
+ * @returns The ObjectType of the node, or undefined if not detected
99
+ */
100
+ function getObjectType(node, initialScope) {
101
+ if (node == null) return unit;
102
+ switch (node.type) {
103
+ case AST_NODE_TYPES.JSXElement:
104
+ case AST_NODE_TYPES.JSXFragment: return {
105
+ kind: "jsx",
106
+ node
107
+ };
108
+ case AST_NODE_TYPES.ArrayExpression: return {
109
+ kind: "array",
110
+ node
111
+ };
112
+ case AST_NODE_TYPES.ObjectExpression: return {
113
+ kind: "plain",
114
+ node
115
+ };
116
+ case AST_NODE_TYPES.ClassExpression: return {
117
+ kind: "class",
118
+ node
119
+ };
120
+ case AST_NODE_TYPES.NewExpression:
121
+ case AST_NODE_TYPES.ThisExpression: return {
122
+ kind: "instance",
123
+ node
124
+ };
125
+ case AST_NODE_TYPES.FunctionDeclaration:
126
+ case AST_NODE_TYPES.FunctionExpression:
127
+ case AST_NODE_TYPES.ArrowFunctionExpression: return {
128
+ kind: "function",
129
+ node
130
+ };
131
+ case AST_NODE_TYPES.Literal:
132
+ if ("regex" in node) return {
133
+ kind: "regexp",
134
+ node
135
+ };
136
+ return unit;
137
+ case AST_NODE_TYPES.Identifier:
138
+ if (!("name" in node) || typeof node.name !== "string") return unit;
139
+ return getObjectType(getVariableDefinitionNode(initialScope.set.get(node.name), -1), initialScope);
140
+ case AST_NODE_TYPES.MemberExpression:
141
+ if (!("object" in node)) return unit;
142
+ return getObjectType(node.object, initialScope);
143
+ case AST_NODE_TYPES.AssignmentExpression:
144
+ case AST_NODE_TYPES.AssignmentPattern:
145
+ if (!("right" in node)) return unit;
146
+ return getObjectType(node.right, initialScope);
147
+ case AST_NODE_TYPES.LogicalExpression: return getObjectType(node.right, initialScope);
148
+ case AST_NODE_TYPES.ConditionalExpression: return getObjectType(node.consequent, initialScope) ?? getObjectType(node.alternate, initialScope);
149
+ case AST_NODE_TYPES.SequenceExpression:
150
+ if (node.expressions.length === 0) return unit;
151
+ return getObjectType(node.expressions[node.expressions.length - 1], initialScope);
152
+ case AST_NODE_TYPES.CallExpression: return {
153
+ kind: "unknown",
154
+ node,
155
+ reason: "call-expression"
156
+ };
157
+ default:
158
+ if (!("expression" in node) || typeof node.expression !== "object") return unit;
159
+ return getObjectType(node.expression, initialScope);
160
+ }
161
+ }
162
+
168
163
  //#endregion
169
164
  //#region src/is-node-value-equal.ts
170
165
  const thisBlockTypes = [
@@ -230,4 +225,4 @@ function isNodeValueEqual(a, b, initialScopes) {
230
225
  }
231
226
 
232
227
  //#endregion
233
- export { ConstructionDetectionHint, findAssignmentTarget, findProperty, findVariable, getChildScopes, getConstruction, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
228
+ export { findAssignmentTarget, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "2.2.3",
3
+ "version": "2.2.4-next.0",
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": {
@@ -31,8 +31,8 @@
31
31
  "@typescript-eslint/types": "^8.46.2",
32
32
  "@typescript-eslint/utils": "^8.46.2",
33
33
  "ts-pattern": "^5.8.0",
34
- "@eslint-react/ast": "2.2.3",
35
- "@eslint-react/eff": "2.2.3"
34
+ "@eslint-react/ast": "2.2.4-next.0",
35
+ "@eslint-react/eff": "2.2.4-next.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "tsdown": "^0.15.9",