@eslint-react/var
Advanced tools
Comparing version
@@ -28,11 +28,2 @@ import { _ } from '@eslint-react/eff'; | ||
/** | ||
* Check if an identifier is initialized from the given source | ||
* @param name The top-level identifier's name | ||
* @param source The import source to check against | ||
* @param initialScope Initial scope to search for the identifier | ||
* @returns Whether the identifier is initialized from the given source | ||
*/ | ||
declare function isInitializedFromSource(name: string, source: string, initialScope: Scope): boolean; | ||
/** | ||
* Determines whether node value equals to another node value | ||
@@ -121,2 +112,2 @@ * @param a node to compare | ||
export { type LazyValue, type ValueConstruction, ValueConstructionHint, findPropertyInProperties, findVariable, getChidScopes, getValueConstruction, getVariableDeclaratorId, getVariableId, getVariableNode, getVariables, isInitializedFromSource, isNodeValueEqual, isVariableIdEqual, toStaticValue }; | ||
export { type LazyValue, type ValueConstruction, ValueConstructionHint, findPropertyInProperties, findVariable, getChidScopes, getValueConstruction, getVariableDeclaratorId, getVariableId, getVariableNode, getVariables, isNodeValueEqual, isVariableIdEqual, toStaticValue }; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
var ASTUtils = require('@typescript-eslint/utils/ast-utils'); | ||
var AST3 = require('@eslint-react/ast'); | ||
var AST2 = require('@eslint-react/ast'); | ||
var scopeManager = require('@typescript-eslint/scope-manager'); | ||
@@ -29,3 +29,3 @@ | ||
var ASTUtils__namespace = /*#__PURE__*/_interopNamespace(ASTUtils); | ||
var AST3__namespace = /*#__PURE__*/_interopNamespace(AST3); | ||
var AST2__namespace = /*#__PURE__*/_interopNamespace(AST2); | ||
@@ -46,3 +46,3 @@ // src/find-property-in-properties.ts | ||
return def.node; | ||
case (def.type === scopeManager.DefinitionType.Parameter && AST3__namespace.isFunction(def.node)): | ||
case (def.type === scopeManager.DefinitionType.Parameter && AST2__namespace.isFunction(def.node)): | ||
return def.node; | ||
@@ -139,36 +139,2 @@ case ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)): | ||
} | ||
function isInitializedFromSource(name, source, initialScope) { | ||
const latestDef = findVariable2(name, initialScope)?.defs.at(-1); | ||
if (latestDef == null) return false; | ||
const { node, parent } = latestDef; | ||
if (node.type === types.AST_NODE_TYPES.VariableDeclarator && node.init != null) { | ||
const { init } = node; | ||
if (init.type === types.AST_NODE_TYPES.MemberExpression && init.object.type === types.AST_NODE_TYPES.Identifier) { | ||
return isInitializedFromSource(init.object.name, source, initialScope); | ||
} | ||
if (init.type === types.AST_NODE_TYPES.Identifier) { | ||
return isInitializedFromSource(init.name, source, initialScope); | ||
} | ||
const args = getRequireExpressionArguments(init); | ||
const arg0 = args?.[0]; | ||
if (arg0 == null || !AST3__namespace.isStringLiteral(arg0)) { | ||
return false; | ||
} | ||
return arg0.value === source || arg0.value.startsWith(`${source}/`); | ||
} | ||
return parent?.type === types.AST_NODE_TYPES.ImportDeclaration && parent.source.value === source; | ||
} | ||
function getRequireExpressionArguments(node) { | ||
switch (true) { | ||
// require('source') | ||
case (node.type === types.AST_NODE_TYPES.CallExpression && node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === "require"): { | ||
return node.arguments; | ||
} | ||
// require('source').variable | ||
case node.type === types.AST_NODE_TYPES.MemberExpression: { | ||
return getRequireExpressionArguments(node.object); | ||
} | ||
} | ||
return eff._; | ||
} | ||
function toStaticValue(lazyValue) { | ||
@@ -214,4 +180,4 @@ const { kind, node, initialScope } = lazyValue; | ||
switch (true) { | ||
case (aVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && AST3__namespace.isFunction(aVarNode) && AST3__namespace.isFunction(bVarNode)): { | ||
if (!AST3__namespace.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) { | ||
case (aVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && AST2__namespace.isFunction(aVarNode) && AST2__namespace.isFunction(bVarNode)): { | ||
if (!AST2__namespace.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) { | ||
return false; | ||
@@ -221,4 +187,4 @@ } | ||
const bParams = bVarNode.params; | ||
const aPos = aParams.findIndex((x) => AST3__namespace.isNodeEqual(x, a)); | ||
const bPos = bParams.findIndex((x) => AST3__namespace.isNodeEqual(x, b)); | ||
const aPos = aParams.findIndex((x) => AST2__namespace.isNodeEqual(x, a)); | ||
const bPos = bParams.findIndex((x) => AST2__namespace.isNodeEqual(x, b)); | ||
return aPos !== -1 && bPos !== -1 && aPos === bPos; | ||
@@ -234,3 +200,3 @@ } | ||
const bRight = bDefParentParent.right; | ||
return AST3__namespace.isNodeEqual(aRight, bRight); | ||
return AST2__namespace.isNodeEqual(aRight, bRight); | ||
} | ||
@@ -243,3 +209,3 @@ default: { | ||
case (a.type === types.AST_NODE_TYPES.MemberExpression && b.type === types.AST_NODE_TYPES.MemberExpression): { | ||
return AST3__namespace.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes); | ||
return AST2__namespace.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes); | ||
} | ||
@@ -250,4 +216,4 @@ case (a.type === types.AST_NODE_TYPES.ThisExpression && b.type === types.AST_NODE_TYPES.ThisExpression): { | ||
} | ||
const aFunction = AST3__namespace.findParentNode(a, AST3__namespace.isOneOf(thisBlockTypes)); | ||
const bFunction = AST3__namespace.findParentNode(b, AST3__namespace.isOneOf(thisBlockTypes)); | ||
const aFunction = AST2__namespace.findParentNode(a, AST2__namespace.isOneOf(thisBlockTypes)); | ||
const bFunction = AST2__namespace.findParentNode(b, AST2__namespace.isOneOf(thisBlockTypes)); | ||
return aFunction === bFunction; | ||
@@ -263,3 +229,3 @@ } | ||
function isVariableIdEqual(a, b, initialScopes) { | ||
return AST3__namespace.isNodeEqual(a, b) || isNodeValueEqual(a, b, initialScopes); | ||
return AST2__namespace.isNodeEqual(a, b) || isNodeValueEqual(a, b, initialScopes); | ||
} | ||
@@ -350,5 +316,4 @@ var ValueConstructionHint = { | ||
exports.getVariables = getVariables; | ||
exports.isInitializedFromSource = isInitializedFromSource; | ||
exports.isNodeValueEqual = isNodeValueEqual; | ||
exports.isVariableIdEqual = isVariableIdEqual; | ||
exports.toStaticValue = toStaticValue; |
{ | ||
"name": "@eslint-react/var", | ||
"version": "1.28.0-next.1", | ||
"version": "1.28.0-next.2", | ||
"description": "ESLint React's TSESTree AST utility module for static analysis of variables.", | ||
@@ -43,4 +43,4 @@ "homepage": "https://github.com/Rel1cx/eslint-react", | ||
"ts-pattern": "^5.6.2", | ||
"@eslint-react/ast": "1.28.0-next.1", | ||
"@eslint-react/eff": "1.28.0-next.1" | ||
"@eslint-react/ast": "1.28.0-next.2", | ||
"@eslint-react/eff": "1.28.0-next.2" | ||
}, | ||
@@ -47,0 +47,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35742
-9.69%674
-10.49%+ Added
+ Added
- Removed
- Removed