@eslint-react/var 2.4.0-next.9 → 2.4.1-beta.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
@@ -5,6 +5,15 @@ import { Scope, Variable } from "@typescript-eslint/scope-manager";
5
5
  //#region src/find-assignment-target.d.ts
6
6
  declare function findAssignmentTarget(node: TSESTree.Node | unit, prev?: TSESTree.Node): TSESTree.BindingName | TSESTree.Expression | unit;
7
7
  //#endregion
8
+ //#region src/find-import-source.d.ts
9
+ /**
10
+ * Find the import source of a variable
11
+ * @param name The variable name
12
+ * @param initialScope The initial scope to search
13
+ * @returns The import source or undefined if not found
14
+ */
15
+ declare function findImportSource(name: string, initialScope: Scope): string | undefined;
16
+ //#endregion
8
17
  //#region src/find-property.d.ts
9
18
  declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
10
19
  //#endregion
@@ -72,4 +81,4 @@ declare const findVariable: {
72
81
  */
73
82
  declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
74
83
  //#endregion
75
- export { ObjectType, findAssignmentTarget, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
84
+ export { ObjectType, findAssignmentTarget, findImportSource, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import { dual, unit } from "@eslint-react/eff";
1
+ import { dual, identity, unit } from "@eslint-react/eff";
2
2
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
3
3
  import * as AST from "@eslint-react/ast";
4
+ import { P, match } from "ts-pattern";
4
5
  import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
5
6
  import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
6
7
  import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
@@ -16,6 +17,69 @@ function findAssignmentTarget(node, prev) {
16
17
  }
17
18
  }
18
19
 
20
+ //#endregion
21
+ //#region src/get-variables-from-scope.ts
22
+ /**
23
+ * Get all variables from the given scope up to the global scope
24
+ * @param initialScope The scope to start from
25
+ * @returns All variables from the given scope up to the global scope
26
+ */
27
+ function getVariables(initialScope) {
28
+ let scope = initialScope;
29
+ const variables = [...scope.variables];
30
+ while (scope.type !== ScopeType.global) {
31
+ scope = scope.upper;
32
+ variables.push(...scope.variables);
33
+ }
34
+ return variables.reverse();
35
+ }
36
+ const findVariable = dual(2, (nameOrNode, initialScope) => {
37
+ if (nameOrNode == null) return unit;
38
+ return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
39
+ });
40
+
41
+ //#endregion
42
+ //#region src/find-import-source.ts
43
+ /**
44
+ * Get the arguments of a require expression
45
+ * @param node The node to match
46
+ * @returns The require expression arguments or undefined if the node is not a require expression
47
+ */
48
+ function getRequireExpressionArguments(node) {
49
+ return match(node).with({
50
+ type: AST_NODE_TYPES.CallExpression,
51
+ arguments: P.select(),
52
+ callee: {
53
+ type: AST_NODE_TYPES.Identifier,
54
+ name: "require"
55
+ }
56
+ }, identity).with({
57
+ type: AST_NODE_TYPES.MemberExpression,
58
+ object: P.select()
59
+ }, getRequireExpressionArguments).otherwise(() => null);
60
+ }
61
+ /**
62
+ * Find the import source of a variable
63
+ * @param name The variable name
64
+ * @param initialScope The initial scope to search
65
+ * @returns The import source or undefined if not found
66
+ */
67
+ function findImportSource(name, initialScope) {
68
+ const latestDef = findVariable(name, initialScope)?.defs.at(-1);
69
+ if (latestDef == null) return unit;
70
+ const { node, parent } = latestDef;
71
+ if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
72
+ const { init } = node;
73
+ if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return findImportSource(init.object.name, initialScope);
74
+ if (init.type === AST_NODE_TYPES.Identifier) return findImportSource(init.name, initialScope);
75
+ const arg0 = getRequireExpressionArguments(init)?.[0];
76
+ if (arg0 == null || !AST.isLiteral(arg0, "string")) return unit;
77
+ return arg0.value;
78
+ }
79
+ if (parent?.type === AST_NODE_TYPES.ImportDeclaration) return parent.source.value;
80
+ return unit;
81
+ }
82
+
19
83
  //#endregion
20
84
  //#region src/get-variable-definition-node.ts
21
85
  function getVariableDefinitionNode(variable, at) {
@@ -38,27 +102,6 @@ function getVariableDefinitionNodeLoose(variable, at) {
38
102
  return unit;
39
103
  }
40
104
 
41
- //#endregion
42
- //#region src/get-variables-from-scope.ts
43
- /**
44
- * Get all variables from the given scope up to the global scope
45
- * @param initialScope The scope to start from
46
- * @returns All variables from the given scope up to the global scope
47
- */
48
- function getVariables(initialScope) {
49
- let scope = initialScope;
50
- const variables = [...scope.variables];
51
- while (scope.type !== ScopeType.global) {
52
- scope = scope.upper;
53
- variables.push(...scope.variables);
54
- }
55
- return variables.reverse();
56
- }
57
- const findVariable = dual(2, (nameOrNode, initialScope) => {
58
- if (nameOrNode == null) return unit;
59
- return ASTUtils.findVariable(initialScope, nameOrNode) ?? unit;
60
- });
61
-
62
105
  //#endregion
63
106
  //#region src/find-property.ts
64
107
  function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
@@ -227,4 +270,4 @@ function isNodeValueEqual(a, b, initialScopes) {
227
270
  }
228
271
 
229
272
  //#endregion
230
- export { findAssignmentTarget, findProperty, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, getVariableDefinitionNodeLoose, getVariables, isNodeValueEqual };
273
+ export { findAssignmentTarget, findImportSource, 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.4.0-next.9",
3
+ "version": "2.4.1-beta.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": {
@@ -30,15 +30,15 @@
30
30
  "./package.json"
31
31
  ],
32
32
  "dependencies": {
33
- "@typescript-eslint/scope-manager": "^8.50.0",
34
- "@typescript-eslint/types": "^8.50.0",
35
- "@typescript-eslint/utils": "^8.50.0",
33
+ "@typescript-eslint/scope-manager": "^8.50.1",
34
+ "@typescript-eslint/types": "^8.50.1",
35
+ "@typescript-eslint/utils": "^8.50.1",
36
36
  "ts-pattern": "^5.9.0",
37
- "@eslint-react/ast": "2.4.0-next.9",
38
- "@eslint-react/eff": "2.4.0-next.9"
37
+ "@eslint-react/ast": "2.4.1-beta.0",
38
+ "@eslint-react/eff": "2.4.1-beta.0"
39
39
  },
40
40
  "devDependencies": {
41
- "tsdown": "^0.18.0",
41
+ "tsdown": "^0.18.2",
42
42
  "@local/configs": "0.0.0"
43
43
  },
44
44
  "peerDependencies": {