Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

@eslint-react/var

Package Overview
Dependencies
Maintainers
1
Versions
2289
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eslint-react/var - npm Package Compare versions

Comparing version
3.0.0-beta.58
to
3.0.0-beta.59
+43
-56
dist/index.d.ts

@@ -6,3 +6,3 @@ import { unit } from "@eslint-react/eff";

//#region src/binding-assignment.d.ts
//#region src/find-enclosing-assignment-target.d.ts
/**

@@ -20,14 +20,4 @@ * Finds the enclosing assignment target (variable, property, etc.) for a given node

type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>;
/**
* Check if two assignment targets are equal
* Compares nodes directly or by their values
* @param context The rule context
* @param a The first node to compare
* @param b The second node to compare
* @returns True if the assignment targets are equal
* @internal
*/
declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
//#endregion
//#region src/binding-import.d.ts
//#region src/find-import-source.d.ts
/**

@@ -41,23 +31,4 @@ * Find the import source of a variable

//#endregion
//#region src/binding-initializer.d.ts
//#region src/find-variable.d.ts
/**
* Get the initializer expression or statement of a variable definition at a specified index
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
*/
declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
/**
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
*/
declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
//#endregion
//#region src/binding-kind.d.ts
type BindingKind = "var" | "let" | "const" | "module" | "hoisted" | "param" | "local" | "unknown";
//#endregion
//#region src/helper.d.ts
/**
* Find a variable by name or identifier node in the scope chain

@@ -76,26 +47,4 @@ * @param initialScope The scope to start searching from

//#endregion
//#region src/value-equality.d.ts
//#region src/get-object-type.d.ts
/**
* Determine whether node value equals to another node value
* @param a node to compare
* @param b node to compare
* @param initialScopes initial scopes of the two nodes
* @returns `true` if node value equal
*/
declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
//#endregion
//#region src/value-property.d.ts
/**
* Find a property by name in an array of properties
* Handles spread elements by recursively resolving the referenced object
* @param name The property name to find
* @param properties The array of properties to search
* @param initialScope The scope to use for variable resolution
* @param seen Set of already seen variable names to prevent circular references
* @returns The found property or unit if not found
*/
declare function findProperty(name: string, properties: (TSESTree.Property | TSESTree.RestElement | TSESTree.SpreadElement)[], initialScope: Scope, seen?: Set<string>): (typeof properties)[number] | unit;
//#endregion
//#region src/value-type.d.ts
/**
* Represents the type classification of an object node

@@ -137,2 +86,40 @@ */

//#endregion
export { AssignmentTarget, BindingKind, ObjectType, findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };
//#region src/get-variable-initializer.d.ts
/**
* Get the initializer expression or statement of a variable definition at a specified index
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
*/
declare function getVariableInitializer(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
/**
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
*/
declare function getVariableInitializerLoose(variable: Variable | unit, at: number): unit | TSESTree.ClassDeclaration | TSESTree.Expression | TSESTree.FunctionDeclaration;
//#endregion
//#region src/is-assignment-target-equal.d.ts
/**
* Check if two assignment targets are equal
* Compares nodes directly or by their values
* @param context The rule context
* @param a The first node to compare
* @param b The second node to compare
* @returns True if the assignment targets are equal
* @internal
*/
declare function isAssignmentTargetEqual(context: RuleContext, a: TSESTree.Node, b: TSESTree.Node): boolean;
//#endregion
//#region src/is-value-equal.d.ts
/**
* Determine whether node value equals to another node value
* @param a node to compare
* @param b node to compare
* @param initialScopes initial scopes of the two nodes
* @returns `true` if node value equal
*/
declare function isValueEqual(a: TSESTree.Node, b: TSESTree.Node, initialScopes: [aScope: Scope, bScope: Scope]): boolean;
//#endregion
export { AssignmentTarget, ObjectType, findEnclosingAssignmentTarget, findImportSource, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };

@@ -1,44 +0,29 @@

import * as ast from "@eslint-react/ast";
import { dual, identity, unit } from "@eslint-react/eff";
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import * as ast from "@eslint-react/ast";
import { P, match } from "ts-pattern";
import * as astUtils from "@typescript-eslint/utils/ast-utils";
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
import { DefinitionType } from "@typescript-eslint/scope-manager";
import { P, match } from "ts-pattern";
//#region src/binding-initializer.ts
//#region src/find-enclosing-assignment-target.ts
/**
* Get the initializer expression or statement of a variable definition at a specified index
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
* Finds the enclosing assignment target (variable, property, etc.) for a given node
*
* @todo Verify correctness and completeness of this function
* @param node The starting node
* @returns The enclosing assignment target node, or undefined if not found
*/
function getVariableInitializer(variable, at) {
if (variable == null) return unit;
const def = variable.defs.at(at);
if (def == null) return unit;
function findEnclosingAssignmentTarget(node) {
switch (true) {
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
default: return unit;
case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id;
case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left;
case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key;
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
default: return findEnclosingAssignmentTarget(node.parent);
}
}
/**
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
*/
function getVariableInitializerLoose(variable, at) {
if (variable == null) return unit;
const node = getVariableInitializer(variable, at);
if (node != null) return node;
const def = variable.defs.at(at);
if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
return unit;
}
//#endregion
//#region src/helper.ts
//#region src/find-variable.ts
/**

@@ -59,102 +44,4 @@ * Find a variable by name or identifier node in the scope chain

//#endregion
//#region src/value-equality.ts
const thisBlockTypes = [
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ClassBody,
AST_NODE_TYPES.Program
];
//#region src/find-import-source.ts
/**
* Determine whether node value equals to another node value
* @param a node to compare
* @param b node to compare
* @param initialScopes initial scopes of the two nodes
* @returns `true` if node value equal
*/
function isValueEqual(a, b, initialScopes) {
a = ast.isTypeExpression(a) ? ast.getUnderlyingExpression(a) : a;
b = ast.isTypeExpression(b) ? ast.getUnderlyingExpression(b) : b;
const [aScope, bScope] = initialScopes;
switch (true) {
case a === b: return true;
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
const aVar = findVariable(a, aScope);
const bVar = findVariable(b, bScope);
const aVarInit = getVariableInitializerLoose(aVar, 0);
const bVarInit = getVariableInitializerLoose(bVar, 0);
const aVarInitParent = aVarInit?.parent;
const bVarInitParent = bVarInit?.parent;
const aDef = aVar?.defs.at(0);
const bDef = bVar?.defs.at(0);
const aDefParentParent = aDef?.parent?.parent;
const bDefParentParent = bDef?.parent?.parent;
switch (true) {
case aVarInitParent?.type === AST_NODE_TYPES.CallExpression && bVarInitParent?.type === AST_NODE_TYPES.CallExpression && ast.isFunction(aVarInit) && ast.isFunction(bVarInit): {
if (!ast.isNodeEqual(aVarInitParent.callee, bVarInitParent.callee)) return false;
const aParams = aVarInit.params;
const bParams = bVarInit.params;
const aPos = aParams.findIndex((x) => ast.isNodeEqual(x, a));
const bPos = bParams.findIndex((x) => ast.isNodeEqual(x, b));
return aPos !== -1 && bPos !== -1 && aPos === bPos;
}
case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
const aLeft = aDefParentParent.left;
const bLeft = bDefParentParent.left;
if (aLeft.type !== bLeft.type) return false;
const aRight = aDefParentParent.right;
const bRight = bDefParentParent.right;
return ast.isNodeEqual(aRight, bRight);
}
default: return aVar != null && bVar != null && aVar === bVar;
}
}
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(a.object, b.object, initialScopes);
case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
if (aScope.block === bScope.block) return true;
return ast.findParentNode(a, ast.isOneOf(thisBlockTypes)) === ast.findParentNode(b, ast.isOneOf(thisBlockTypes));
default: {
const aStatic = getStaticValue(a, aScope);
const bStatic = getStaticValue(b, bScope);
return aStatic != null && bStatic != null && aStatic.value === bStatic.value;
}
}
}
//#endregion
//#region src/binding-assignment.ts
/** eslint-disable jsdoc/require-param */
/**
* Finds the enclosing assignment target (variable, property, etc.) for a given node
*
* @todo Verify correctness and completeness of this function
* @param node The starting node
* @returns The enclosing assignment target node, or undefined if not found
*/
function findEnclosingAssignmentTarget(node) {
switch (true) {
case node.type === AST_NODE_TYPES.VariableDeclarator: return node.id;
case node.type === AST_NODE_TYPES.AssignmentExpression: return node.left;
case node.type === AST_NODE_TYPES.PropertyDefinition: return node.key;
case node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node: return unit;
default: return findEnclosingAssignmentTarget(node.parent);
}
}
/**
* Check if two assignment targets are equal
* Compares nodes directly or by their values
* @param context The rule context
* @param a The first node to compare
* @param b The second node to compare
* @returns True if the assignment targets are equal
* @internal
*/
function isAssignmentTargetEqual(context, a, b) {
return ast.isNodeEqual(a, b) || isValueEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
}
//#endregion
//#region src/binding-import.ts
/**
* Get the arguments of a require expression

@@ -200,34 +87,37 @@ * @param node The node to match

//#endregion
//#region src/value-property.ts
//#region src/get-variable-initializer.ts
/**
* Find a property by name in an array of properties
* Handles spread elements by recursively resolving the referenced object
* @param name The property name to find
* @param properties The array of properties to search
* @param initialScope The scope to use for variable resolution
* @param seen Set of already seen variable names to prevent circular references
* @returns The found property or unit if not found
* Get the initializer expression or statement of a variable definition at a specified index
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or unit if not found
*/
function findProperty(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) {
return properties.findLast((prop) => {
if (prop.type === AST_NODE_TYPES.Property) return "name" in prop.key && prop.key.name === name;
if (prop.type === AST_NODE_TYPES.SpreadElement) switch (prop.argument.type) {
case AST_NODE_TYPES.Identifier: {
if (seen.has(prop.argument.name)) return false;
const initNode = getVariableInitializer(findVariable(prop.argument.name, initialScope), 0);
if (initNode?.type === AST_NODE_TYPES.ObjectExpression) {
seen.add(prop.argument.name);
return findProperty(name, initNode.properties, initialScope, seen) != null;
}
return false;
}
case AST_NODE_TYPES.ObjectExpression: return findProperty(name, prop.argument.properties, initialScope, seen) != null;
default: return false;
}
return false;
});
function getVariableInitializer(variable, at) {
if (variable == null) return unit;
const def = variable.defs.at(at);
if (def == null) return unit;
switch (true) {
case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
default: return unit;
}
}
/**
* Get the initializer expression or statement of a variable definition at a specified index, or the function declaration if the variable is a parameter of a function
* @param variable The variable to get the initializer from
* @param at The index of the variable definition to get the initializer from
* @returns The initializer expression or statement of the variable definition at the specified index, or the function declaration if the variable is a parameter of a function, or unit if not found
*/
function getVariableInitializerLoose(variable, at) {
if (variable == null) return unit;
const node = getVariableInitializer(variable, at);
if (node != null) return node;
const def = variable.defs.at(at);
if (def?.type === DefinitionType.Parameter && ast.isFunction(def.node)) return def.node;
return unit;
}
//#endregion
//#region src/value-type.ts
//#region src/get-object-type.ts
/**

@@ -303,2 +193,83 @@ * Detect the ObjectType of a given node

//#endregion
export { findEnclosingAssignmentTarget, findImportSource, findProperty, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };
//#region src/is-value-equal.ts
const thisBlockTypes = [
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ClassBody,
AST_NODE_TYPES.Program
];
/**
* Determine whether node value equals to another node value
* @param a node to compare
* @param b node to compare
* @param initialScopes initial scopes of the two nodes
* @returns `true` if node value equal
*/
function isValueEqual(a, b, initialScopes) {
a = ast.isTypeExpression(a) ? ast.getUnderlyingExpression(a) : a;
b = ast.isTypeExpression(b) ? ast.getUnderlyingExpression(b) : b;
const [aScope, bScope] = initialScopes;
switch (true) {
case a === b: return true;
case a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal: return a.value === b.value;
case a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement: return a.value.cooked === b.value.cooked;
case a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier: {
const aVar = findVariable(a, aScope);
const bVar = findVariable(b, bScope);
const aVarInit = getVariableInitializerLoose(aVar, 0);
const bVarInit = getVariableInitializerLoose(bVar, 0);
const aVarInitParent = aVarInit?.parent;
const bVarInitParent = bVarInit?.parent;
const aDef = aVar?.defs.at(0);
const bDef = bVar?.defs.at(0);
const aDefParentParent = aDef?.parent?.parent;
const bDefParentParent = bDef?.parent?.parent;
switch (true) {
case aVarInitParent?.type === AST_NODE_TYPES.CallExpression && bVarInitParent?.type === AST_NODE_TYPES.CallExpression && ast.isFunction(aVarInit) && ast.isFunction(bVarInit): {
if (!ast.isNodeEqual(aVarInitParent.callee, bVarInitParent.callee)) return false;
const aParams = aVarInit.params;
const bParams = bVarInit.params;
const aPos = aParams.findIndex((x) => ast.isNodeEqual(x, a));
const bPos = bParams.findIndex((x) => ast.isNodeEqual(x, b));
return aPos !== -1 && bPos !== -1 && aPos === bPos;
}
case aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement: {
const aLeft = aDefParentParent.left;
const bLeft = bDefParentParent.left;
if (aLeft.type !== bLeft.type) return false;
const aRight = aDefParentParent.right;
const bRight = bDefParentParent.right;
return ast.isNodeEqual(aRight, bRight);
}
default: return aVar != null && bVar != null && aVar === bVar;
}
}
case a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression: return ast.isNodeEqual(a.property, b.property) && isValueEqual(a.object, b.object, initialScopes);
case a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression:
if (aScope.block === bScope.block) return true;
return ast.findParentNode(a, ast.isOneOf(thisBlockTypes)) === ast.findParentNode(b, ast.isOneOf(thisBlockTypes));
default: {
const aStatic = getStaticValue(a, aScope);
const bStatic = getStaticValue(b, bScope);
return aStatic != null && bStatic != null && aStatic.value === bStatic.value;
}
}
}
//#endregion
//#region src/is-assignment-target-equal.ts
/**
* Check if two assignment targets are equal
* Compares nodes directly or by their values
* @param context The rule context
* @param a The first node to compare
* @param b The second node to compare
* @returns True if the assignment targets are equal
* @internal
*/
function isAssignmentTargetEqual(context, a, b) {
return ast.isNodeEqual(a, b) || isValueEqual(a, b, [context.sourceCode.getScope(a), context.sourceCode.getScope(b)]);
}
//#endregion
export { findEnclosingAssignmentTarget, findImportSource, findVariable, getObjectType, getVariableInitializer, getVariableInitializerLoose, isAssignmentTargetEqual, isValueEqual };
{
"name": "@eslint-react/var",
"version": "3.0.0-beta.58",
"version": "3.0.0-beta.59",
"description": "ESLint React's TSESTree AST utility module for static analysis of variables.",

@@ -37,5 +37,5 @@ "homepage": "https://github.com/Rel1cx/eslint-react",

"ts-pattern": "^5.9.0",
"@eslint-react/ast": "3.0.0-beta.58",
"@eslint-react/shared": "3.0.0-beta.58",
"@eslint-react/eff": "3.0.0-beta.58"
"@eslint-react/eff": "3.0.0-beta.59",
"@eslint-react/ast": "3.0.0-beta.59",
"@eslint-react/shared": "3.0.0-beta.59"
},

@@ -42,0 +42,0 @@ "devDependencies": {