@eslint-react/var 1.22.0 → 1.22.1-beta.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as effect_Unify from 'effect/Unify';
2
2
  import { Data, O } from '@eslint-react/eff';
3
3
  import * as _typescript_eslint_scope_manager from '@typescript-eslint/scope-manager';
4
- import { Scope, Variable, ScopeManager, Definition } from '@typescript-eslint/scope-manager';
4
+ import { Scope, Variable, ScopeManager } from '@typescript-eslint/scope-manager';
5
5
  import { TSESTree } from '@typescript-eslint/types';
6
6
 
7
7
  type Construction = Data.TaggedEnum<{
@@ -456,13 +456,6 @@ declare function getStaticValue(node: TSESTree.Node, initialScope: Scope): O.Opt
456
456
 
457
457
  declare function getVariableDeclaratorID(node: TSESTree.Node, prev?: TSESTree.Node): O.Option<TSESTree.BindingName | TSESTree.Expression>;
458
458
 
459
- /**
460
- * Get the init node of the nth definition of a variable
461
- * @param at The index number of def in defs
462
- * @returns A function that takes a variable and returns the init node of the nth definition of that variable
463
- */
464
- declare function getVariableDef(at: number): (variable: Variable) => O.Option<Definition>;
465
-
466
459
  /**
467
460
  * Get the init node of the nth definition of a variable
468
461
  * @param at The index number of def in defs
@@ -498,4 +491,4 @@ declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialSco
498
491
  bScope: Scope
499
492
  ]): boolean;
500
493
 
501
- export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getStaticValue, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
494
+ export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getStaticValue, getVariableDeclaratorID, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as effect_Unify from 'effect/Unify';
2
2
  import { Data, O } from '@eslint-react/eff';
3
3
  import * as _typescript_eslint_scope_manager from '@typescript-eslint/scope-manager';
4
- import { Scope, Variable, ScopeManager, Definition } from '@typescript-eslint/scope-manager';
4
+ import { Scope, Variable, ScopeManager } from '@typescript-eslint/scope-manager';
5
5
  import { TSESTree } from '@typescript-eslint/types';
6
6
 
7
7
  type Construction = Data.TaggedEnum<{
@@ -456,13 +456,6 @@ declare function getStaticValue(node: TSESTree.Node, initialScope: Scope): O.Opt
456
456
 
457
457
  declare function getVariableDeclaratorID(node: TSESTree.Node, prev?: TSESTree.Node): O.Option<TSESTree.BindingName | TSESTree.Expression>;
458
458
 
459
- /**
460
- * Get the init node of the nth definition of a variable
461
- * @param at The index number of def in defs
462
- * @returns A function that takes a variable and returns the init node of the nth definition of that variable
463
- */
464
- declare function getVariableDef(at: number): (variable: Variable) => O.Option<Definition>;
465
-
466
459
  /**
467
460
  * Get the init node of the nth definition of a variable
468
461
  * @param at The index number of def in defs
@@ -498,4 +491,4 @@ declare function isNodeValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialSco
498
491
  bScope: Scope
499
492
  ]): boolean;
500
493
 
501
- export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getStaticValue, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
494
+ export { Construction, ConstructionHint, findVariable, getChidScopes, getScope, getStaticValue, getVariableDeclaratorID, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/dist/index.js CHANGED
@@ -157,16 +157,11 @@ function getVariableDeclaratorID(node, prev) {
157
157
  return getVariableDeclaratorID(node.parent, node);
158
158
  }
159
159
  }
160
- function getVariableDef(at) {
161
- return (variable) => {
162
- return eff.O.fromNullable(variable.defs[at]);
163
- };
164
- }
165
160
  function getVariableNode(at) {
166
161
  return (variable) => {
167
162
  return eff.F.pipe(
168
163
  eff.O.some(variable),
169
- eff.O.flatMap(getVariableDef(at)),
164
+ eff.O.flatMapNullable((v) => v.defs.at(at)),
170
165
  eff.O.flatMapNullable((def) => {
171
166
  switch (true) {
172
167
  case (def.type === scopeManager.DefinitionType.FunctionName && def.node.type === types.AST_NODE_TYPES.FunctionDeclaration):
@@ -196,8 +191,7 @@ function getVariables(initialScope) {
196
191
  function isInitializedFromSource(name, source, initialScope) {
197
192
  const maybeLatestDef = eff.O.flatMapNullable(findVariable2(name, initialScope), (v) => v.defs.at(-1));
198
193
  if (eff.O.isNone(maybeLatestDef)) return false;
199
- const latestDef = maybeLatestDef.value;
200
- const { node, parent } = latestDef;
194
+ const { node, parent } = maybeLatestDef.value;
201
195
  if (node.type === types.AST_NODE_TYPES.VariableDeclarator && node.init) {
202
196
  const { init } = node;
203
197
  if (init.type === types.AST_NODE_TYPES.MemberExpression && init.object.type === types.AST_NODE_TYPES.Identifier) {
@@ -206,27 +200,29 @@ function isInitializedFromSource(name, source, initialScope) {
206
200
  if (init.type === types.AST_NODE_TYPES.Identifier) {
207
201
  return isInitializedFromSource(init.name, source, initialScope);
208
202
  }
209
- const maybeRequireExpression = tsPattern.match(init).with({
210
- type: types.AST_NODE_TYPES.CallExpression,
211
- callee: { type: types.AST_NODE_TYPES.Identifier, name: "require" }
212
- }, (exp) => eff.O.some(exp)).with(
213
- {
214
- type: types.AST_NODE_TYPES.MemberExpression,
215
- object: {
216
- type: types.AST_NODE_TYPES.CallExpression,
217
- callee: { type: types.AST_NODE_TYPES.Identifier, name: "require" }
218
- }
219
- },
220
- ({ object }) => eff.O.some(object)
221
- ).otherwise(eff.O.none);
222
- if (eff.O.isNone(maybeRequireExpression)) return false;
223
- const requireExpression = maybeRequireExpression.value;
224
- const [firstArg] = requireExpression.arguments;
225
- if (firstArg?.type !== types.AST_NODE_TYPES.Literal || !eff.isString(firstArg.value)) return false;
226
- return firstArg.value === source || firstArg.value.startsWith(`${source}/`);
203
+ return eff.F.pipe(
204
+ getRequireExpressionArguments(init),
205
+ eff.O.flatMapNullable((args) => args[0]),
206
+ eff.O.filter(AST__namespace.isStringLiteral),
207
+ // check for: `require('source')` or `require('source/...')`
208
+ eff.O.exists((arg) => arg.value === source || arg.value.startsWith(`${source}/`))
209
+ );
227
210
  }
228
211
  return tsPattern.isMatching({ type: "ImportDeclaration", source: { value: source } }, parent);
229
212
  }
213
+ function getRequireExpressionArguments(node) {
214
+ switch (true) {
215
+ // require('source')
216
+ case (node.type === types.AST_NODE_TYPES.CallExpression && node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === "require"): {
217
+ return eff.O.some(node.arguments);
218
+ }
219
+ // require('source').variable
220
+ case node.type === types.AST_NODE_TYPES.MemberExpression: {
221
+ return getRequireExpressionArguments(node.object);
222
+ }
223
+ }
224
+ return eff.O.none();
225
+ }
230
226
  var thisBlockTypes = [
231
227
  types.AST_NODE_TYPES.FunctionDeclaration,
232
228
  types.AST_NODE_TYPES.FunctionExpression,
@@ -249,8 +245,8 @@ function isNodeValueEqual(a, b, initialScopes) {
249
245
  const bVarNode = eff.O.flatMap(bVar, getVariableNode(0));
250
246
  const aVarNodeParent = eff.O.flatMapNullable(aVarNode, (n) => n.parent);
251
247
  const bVarNodeParent = eff.O.flatMapNullable(bVarNode, (n) => n.parent);
252
- const aDef = eff.O.flatMap(aVar, getVariableDef(0));
253
- const bDef = eff.O.flatMap(bVar, getVariableDef(0));
248
+ const aDef = eff.O.flatMapNullable(aVar, (v) => v.defs.at(0));
249
+ const bDef = eff.O.flatMapNullable(bVar, (v) => v.defs.at(0));
254
250
  const aDefParentParent = eff.O.flatMapNullable(aDef, (d) => d.parent?.parent);
255
251
  const bDefParentParent = eff.O.flatMapNullable(bDef, (d) => d.parent?.parent);
256
252
  switch (true) {
@@ -318,7 +314,6 @@ exports.getChidScopes = getChidScopes;
318
314
  exports.getScope = getScope;
319
315
  exports.getStaticValue = getStaticValue2;
320
316
  exports.getVariableDeclaratorID = getVariableDeclaratorID;
321
- exports.getVariableDef = getVariableDef;
322
317
  exports.getVariableNode = getVariableNode;
323
318
  exports.getVariables = getVariables;
324
319
  exports.inspectConstruction = inspectConstruction;
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as AST from '@eslint-react/ast';
2
- import { Data, F, O, isString, Equal, isNullable, isObject } from '@eslint-react/eff';
2
+ import { Data, F, O, Equal, isNullable, isString, isObject } from '@eslint-react/eff';
3
3
  import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager';
4
4
  import { AST_NODE_TYPES } from '@typescript-eslint/types';
5
- import { match, isMatching } from 'ts-pattern';
5
+ import { isMatching, match } from 'ts-pattern';
6
6
  import * as ASTUtils from '@typescript-eslint/utils/ast-utils';
7
7
 
8
8
  // src/construction.ts
@@ -134,16 +134,11 @@ function getVariableDeclaratorID(node, prev) {
134
134
  return getVariableDeclaratorID(node.parent, node);
135
135
  }
136
136
  }
137
- function getVariableDef(at) {
138
- return (variable) => {
139
- return O.fromNullable(variable.defs[at]);
140
- };
141
- }
142
137
  function getVariableNode(at) {
143
138
  return (variable) => {
144
139
  return F.pipe(
145
140
  O.some(variable),
146
- O.flatMap(getVariableDef(at)),
141
+ O.flatMapNullable((v) => v.defs.at(at)),
147
142
  O.flatMapNullable((def) => {
148
143
  switch (true) {
149
144
  case (def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration):
@@ -173,8 +168,7 @@ function getVariables(initialScope) {
173
168
  function isInitializedFromSource(name, source, initialScope) {
174
169
  const maybeLatestDef = O.flatMapNullable(findVariable2(name, initialScope), (v) => v.defs.at(-1));
175
170
  if (O.isNone(maybeLatestDef)) return false;
176
- const latestDef = maybeLatestDef.value;
177
- const { node, parent } = latestDef;
171
+ const { node, parent } = maybeLatestDef.value;
178
172
  if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init) {
179
173
  const { init } = node;
180
174
  if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) {
@@ -183,27 +177,29 @@ function isInitializedFromSource(name, source, initialScope) {
183
177
  if (init.type === AST_NODE_TYPES.Identifier) {
184
178
  return isInitializedFromSource(init.name, source, initialScope);
185
179
  }
186
- const maybeRequireExpression = match(init).with({
187
- type: AST_NODE_TYPES.CallExpression,
188
- callee: { type: AST_NODE_TYPES.Identifier, name: "require" }
189
- }, (exp) => O.some(exp)).with(
190
- {
191
- type: AST_NODE_TYPES.MemberExpression,
192
- object: {
193
- type: AST_NODE_TYPES.CallExpression,
194
- callee: { type: AST_NODE_TYPES.Identifier, name: "require" }
195
- }
196
- },
197
- ({ object }) => O.some(object)
198
- ).otherwise(O.none);
199
- if (O.isNone(maybeRequireExpression)) return false;
200
- const requireExpression = maybeRequireExpression.value;
201
- const [firstArg] = requireExpression.arguments;
202
- if (firstArg?.type !== AST_NODE_TYPES.Literal || !isString(firstArg.value)) return false;
203
- return firstArg.value === source || firstArg.value.startsWith(`${source}/`);
180
+ return F.pipe(
181
+ getRequireExpressionArguments(init),
182
+ O.flatMapNullable((args) => args[0]),
183
+ O.filter(AST.isStringLiteral),
184
+ // check for: `require('source')` or `require('source/...')`
185
+ O.exists((arg) => arg.value === source || arg.value.startsWith(`${source}/`))
186
+ );
204
187
  }
205
188
  return isMatching({ type: "ImportDeclaration", source: { value: source } }, parent);
206
189
  }
190
+ function getRequireExpressionArguments(node) {
191
+ switch (true) {
192
+ // require('source')
193
+ case (node.type === AST_NODE_TYPES.CallExpression && node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === "require"): {
194
+ return O.some(node.arguments);
195
+ }
196
+ // require('source').variable
197
+ case node.type === AST_NODE_TYPES.MemberExpression: {
198
+ return getRequireExpressionArguments(node.object);
199
+ }
200
+ }
201
+ return O.none();
202
+ }
207
203
  var thisBlockTypes = [
208
204
  AST_NODE_TYPES.FunctionDeclaration,
209
205
  AST_NODE_TYPES.FunctionExpression,
@@ -226,8 +222,8 @@ function isNodeValueEqual(a, b, initialScopes) {
226
222
  const bVarNode = O.flatMap(bVar, getVariableNode(0));
227
223
  const aVarNodeParent = O.flatMapNullable(aVarNode, (n) => n.parent);
228
224
  const bVarNodeParent = O.flatMapNullable(bVarNode, (n) => n.parent);
229
- const aDef = O.flatMap(aVar, getVariableDef(0));
230
- const bDef = O.flatMap(bVar, getVariableDef(0));
225
+ const aDef = O.flatMapNullable(aVar, (v) => v.defs.at(0));
226
+ const bDef = O.flatMapNullable(bVar, (v) => v.defs.at(0));
231
227
  const aDefParentParent = O.flatMapNullable(aDef, (d) => d.parent?.parent);
232
228
  const bDefParentParent = O.flatMapNullable(bDef, (d) => d.parent?.parent);
233
229
  switch (true) {
@@ -288,4 +284,4 @@ function isNodeValueEqual(a, b, initialScopes) {
288
284
  }
289
285
  }
290
286
 
291
- export { Construction, ConstructionHint, findVariable2 as findVariable, getChidScopes, getScope, getStaticValue2 as getStaticValue, getVariableDeclaratorID, getVariableDef, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
287
+ export { Construction, ConstructionHint, findVariable2 as findVariable, getChidScopes, getScope, getStaticValue2 as getStaticValue, getVariableDeclaratorID, getVariableNode, getVariables, inspectConstruction, isInitializedFromSource, isNodeValueEqual };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/var",
3
- "version": "1.22.0",
3
+ "version": "1.22.1-beta.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": {
@@ -39,9 +39,9 @@
39
39
  "@typescript-eslint/types": "^8.18.1",
40
40
  "@typescript-eslint/utils": "^8.18.1",
41
41
  "ts-pattern": "^5.6.0",
42
- "@eslint-react/eff": "1.22.0",
43
- "@eslint-react/ast": "1.22.0",
44
- "@eslint-react/types": "1.22.0"
42
+ "@eslint-react/ast": "1.22.1-beta.1",
43
+ "@eslint-react/types": "1.22.1-beta.1",
44
+ "@eslint-react/eff": "1.22.1-beta.1"
45
45
  },
46
46
  "devDependencies": {
47
47
  "string-ts": "^2.2.0",