@eslint-react/var 1.17.2-next.4 → 1.17.2-next.6

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 CHANGED
@@ -475,6 +475,15 @@ declare function getVariableNode(at: number): (variable: Variable) => O.Option<T
475
475
  */
476
476
  declare function getVariables(initialScope: Scope): Variable[];
477
477
 
478
+ /**
479
+ * Check if an identifier is initialized from the given source
480
+ * @param name The top-level identifier's name
481
+ * @param source The import source to check against
482
+ * @param initialScope Initial scope to search for the identifier
483
+ * @returns Whether the identifier is initialized from the given source
484
+ */
485
+ declare function isInitializedFromSource(name: string, source: string, initialScope: Scope): boolean;
486
+
478
487
  /**
479
488
  * Determines whether node value equals to another node value
480
489
  * @param a node to compare
@@ -484,4 +493,4 @@ declare function getVariables(initialScope: Scope): Variable[];
484
493
  */
485
494
  declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [Scope, Scope]): boolean;
486
495
 
487
- export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isNodeValueEqual };
496
+ export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/dist/index.d.ts CHANGED
@@ -475,6 +475,15 @@ declare function getVariableNode(at: number): (variable: Variable) => O.Option<T
475
475
  */
476
476
  declare function getVariables(initialScope: Scope): Variable[];
477
477
 
478
+ /**
479
+ * Check if an identifier is initialized from the given source
480
+ * @param name The top-level identifier's name
481
+ * @param source The import source to check against
482
+ * @param initialScope Initial scope to search for the identifier
483
+ * @returns Whether the identifier is initialized from the given source
484
+ */
485
+ declare function isInitializedFromSource(name: string, source: string, initialScope: Scope): boolean;
486
+
478
487
  /**
479
488
  * Determines whether node value equals to another node value
480
489
  * @param a node to compare
@@ -484,4 +493,4 @@ declare function getVariables(initialScope: Scope): Variable[];
484
493
  */
485
494
  declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [Scope, Scope]): boolean;
486
495
 
487
- export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isNodeValueEqual };
496
+ export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/dist/index.js CHANGED
@@ -184,6 +184,40 @@ function getVariables(initialScope) {
184
184
  }
185
185
  return variables.reverse();
186
186
  }
187
+ function isInitializedFromSource(name, source, initialScope) {
188
+ const maybeLatestDef = tools.O.flatMapNullable(findVariable(name, initialScope), (v) => v.defs.at(-1));
189
+ if (tools.O.isNone(maybeLatestDef)) return false;
190
+ const latestDef = maybeLatestDef.value;
191
+ const { node, parent } = latestDef;
192
+ if (node.type === types.AST_NODE_TYPES.VariableDeclarator && node.init) {
193
+ const { init } = node;
194
+ if (init.type === types.AST_NODE_TYPES.MemberExpression && init.object.type === types.AST_NODE_TYPES.Identifier) {
195
+ return isInitializedFromSource(init.object.name, source, initialScope);
196
+ }
197
+ if (init.type === types.AST_NODE_TYPES.Identifier) {
198
+ return isInitializedFromSource(init.name, source, initialScope);
199
+ }
200
+ const maybeRequireExpression = tsPattern.match(init).with({
201
+ type: types.AST_NODE_TYPES.CallExpression,
202
+ callee: { type: types.AST_NODE_TYPES.Identifier, name: "require" }
203
+ }, (exp) => tools.O.some(exp)).with(
204
+ {
205
+ type: types.AST_NODE_TYPES.MemberExpression,
206
+ object: {
207
+ type: types.AST_NODE_TYPES.CallExpression,
208
+ callee: { type: types.AST_NODE_TYPES.Identifier, name: "require" }
209
+ }
210
+ },
211
+ ({ object }) => tools.O.some(object)
212
+ ).otherwise(tools.O.none);
213
+ if (tools.O.isNone(maybeRequireExpression)) return false;
214
+ const requireExpression = maybeRequireExpression.value;
215
+ const [firstArg] = requireExpression.arguments;
216
+ if (firstArg?.type !== types.AST_NODE_TYPES.Literal || !tools.isString(firstArg.value)) return false;
217
+ return firstArg.value === source || firstArg.value.startsWith(`${source}/`);
218
+ }
219
+ return tsPattern.isMatching({ type: "ImportDeclaration", source: { value: source } }, parent);
220
+ }
187
221
  var thisBlockTypes = [
188
222
  types.AST_NODE_TYPES.FunctionDeclaration,
189
223
  types.AST_NODE_TYPES.FunctionExpression,
@@ -230,4 +264,5 @@ exports.getVariableDef = getVariableDef;
230
264
  exports.getVariableNode = getVariableNode;
231
265
  exports.getVariables = getVariables;
232
266
  exports.inspectConstruction = inspectConstruction;
267
+ exports.isInitializedFromSource = isInitializedFromSource;
233
268
  exports.isNodeValueEqual = isNodeValueEqual;
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as AST from '@eslint-react/ast';
2
- import { Data, F, O, isNullable, isString, isObject } from '@eslint-react/tools';
2
+ import { Data, F, O, isString, isNullable, isObject } from '@eslint-react/tools';
3
3
  import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
4
4
  import { AST_NODE_TYPES } from '@typescript-eslint/types';
5
- import { match } from 'ts-pattern';
5
+ import { match, isMatching } from 'ts-pattern';
6
6
  import { findVariable as findVariable$1, getStaticValue } from '@typescript-eslint/utils/ast-utils';
7
7
 
8
8
  // src/construction.ts
@@ -162,6 +162,40 @@ function getVariables(initialScope) {
162
162
  }
163
163
  return variables.reverse();
164
164
  }
165
+ function isInitializedFromSource(name, source, initialScope) {
166
+ const maybeLatestDef = O.flatMapNullable(findVariable(name, initialScope), (v) => v.defs.at(-1));
167
+ if (O.isNone(maybeLatestDef)) return false;
168
+ const latestDef = maybeLatestDef.value;
169
+ const { node, parent } = latestDef;
170
+ if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init) {
171
+ const { init } = node;
172
+ if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) {
173
+ return isInitializedFromSource(init.object.name, source, initialScope);
174
+ }
175
+ if (init.type === AST_NODE_TYPES.Identifier) {
176
+ return isInitializedFromSource(init.name, source, initialScope);
177
+ }
178
+ const maybeRequireExpression = match(init).with({
179
+ type: AST_NODE_TYPES.CallExpression,
180
+ callee: { type: AST_NODE_TYPES.Identifier, name: "require" }
181
+ }, (exp) => O.some(exp)).with(
182
+ {
183
+ type: AST_NODE_TYPES.MemberExpression,
184
+ object: {
185
+ type: AST_NODE_TYPES.CallExpression,
186
+ callee: { type: AST_NODE_TYPES.Identifier, name: "require" }
187
+ }
188
+ },
189
+ ({ object }) => O.some(object)
190
+ ).otherwise(O.none);
191
+ if (O.isNone(maybeRequireExpression)) return false;
192
+ const requireExpression = maybeRequireExpression.value;
193
+ const [firstArg] = requireExpression.arguments;
194
+ if (firstArg?.type !== AST_NODE_TYPES.Literal || !isString(firstArg.value)) return false;
195
+ return firstArg.value === source || firstArg.value.startsWith(`${source}/`);
196
+ }
197
+ return isMatching({ type: "ImportDeclaration", source: { value: source } }, parent);
198
+ }
165
199
  var thisBlockTypes = [
166
200
  AST_NODE_TYPES.FunctionDeclaration,
167
201
  AST_NODE_TYPES.FunctionExpression,
@@ -198,4 +232,4 @@ function isNodeValueEqual(a, b, initialScopes) {
198
232
  return false;
199
233
  }
200
234
 
201
- export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isNodeValueEqual };
235
+ export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "1.17.2-next.4",
3
+ "version": "1.17.2-next.6",
4
4
  "description": "TSESTree AST utility module for static analysis of variables",
5
5
  "homepage": "https://github.com/rel1cx/eslint-react",
6
6
  "bugs": {
@@ -39,9 +39,9 @@
39
39
  "@typescript-eslint/types": "^8.16.0",
40
40
  "@typescript-eslint/utils": "^8.16.0",
41
41
  "ts-pattern": "^5.5.0",
42
- "@eslint-react/ast": "1.17.2-next.4",
43
- "@eslint-react/tools": "1.17.2-next.4",
44
- "@eslint-react/types": "1.17.2-next.4"
42
+ "@eslint-react/ast": "1.17.2-next.6",
43
+ "@eslint-react/types": "1.17.2-next.6",
44
+ "@eslint-react/tools": "1.17.2-next.6"
45
45
  },
46
46
  "devDependencies": {
47
47
  "string-ts": "^2.2.0",