@eslint-react/var
Advanced tools
Comparing version
@@ -1,39 +0,17 @@ | ||
'use strict'; | ||
import { dual, _ } from '@eslint-react/eff'; | ||
import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
import * as ASTUtils from '@typescript-eslint/utils/ast-utils'; | ||
import { getStaticValue } from '@typescript-eslint/utils/ast-utils'; | ||
import * as AST from '@eslint-react/ast'; | ||
var eff = require('@eslint-react/eff'); | ||
var scopeManager = require('@typescript-eslint/scope-manager'); | ||
var types = require('@typescript-eslint/types'); | ||
var ASTUtils = require('@typescript-eslint/utils/ast-utils'); | ||
var AST = require('@eslint-react/ast'); | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var ASTUtils__namespace = /*#__PURE__*/_interopNamespace(ASTUtils); | ||
var AST__namespace = /*#__PURE__*/_interopNamespace(AST); | ||
// src/var-collect.ts | ||
function getVariableInitNode(variable, at) { | ||
if (variable == null) return eff._; | ||
if (variable == null) return _; | ||
const def = variable.defs.at(at); | ||
if (def == null) return eff._; | ||
if (def == null) return _; | ||
switch (true) { | ||
case (def.type === scopeManager.DefinitionType.FunctionName && def.node.type === types.AST_NODE_TYPES.FunctionDeclaration): | ||
case (def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration): | ||
return def.node; | ||
case (def.type === scopeManager.DefinitionType.ClassName && def.node.type === types.AST_NODE_TYPES.ClassDeclaration): | ||
case (def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration): | ||
return def.node; | ||
@@ -43,3 +21,3 @@ case ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)): | ||
default: | ||
return eff._; | ||
return _; | ||
} | ||
@@ -52,3 +30,3 @@ } | ||
const variables = [...scope.variables]; | ||
while (scope.type !== scopeManager.ScopeType.global) { | ||
while (scope.type !== ScopeType.global) { | ||
scope = scope.upper; | ||
@@ -59,18 +37,18 @@ variables.push(...scope.variables); | ||
} | ||
var findVariable2 = eff.dual(2, (nameOrNode, initialScope) => { | ||
if (nameOrNode == null) return eff._; | ||
return ASTUtils__namespace.findVariable(initialScope, nameOrNode) ?? eff._; | ||
var findVariable2 = dual(2, (nameOrNode, initialScope) => { | ||
if (nameOrNode == null) return _; | ||
return ASTUtils.findVariable(initialScope, nameOrNode) ?? _; | ||
}); | ||
function findPropertyInProperties(name, properties, initialScope, seen = /* @__PURE__ */ new Set()) { | ||
return properties.findLast((prop) => { | ||
if (prop.type === types.AST_NODE_TYPES.Property) { | ||
if (prop.type === AST_NODE_TYPES.Property) { | ||
return "name" in prop.key && prop.key.name === name; | ||
} | ||
if (prop.type === types.AST_NODE_TYPES.SpreadElement) { | ||
if (prop.type === AST_NODE_TYPES.SpreadElement) { | ||
switch (prop.argument.type) { | ||
case types.AST_NODE_TYPES.Identifier: { | ||
case AST_NODE_TYPES.Identifier: { | ||
if (seen.has(prop.argument.name)) return false; | ||
const variable = findVariable2(prop.argument.name, initialScope); | ||
const variableNode = getVariableInitNode(variable, 0); | ||
if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) { | ||
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) { | ||
seen.add(prop.argument.name); | ||
@@ -86,3 +64,3 @@ return findPropertyInProperties( | ||
} | ||
case types.AST_NODE_TYPES.ObjectExpression: { | ||
case AST_NODE_TYPES.ObjectExpression: { | ||
return findPropertyInProperties( | ||
@@ -107,46 +85,46 @@ name, | ||
function getConstruction(node, initialScope, hint = ConstructionDetectionHint.None) { | ||
if (node == null) return eff._; | ||
if (node == null) return _; | ||
switch (node.type) { | ||
case types.AST_NODE_TYPES.JSXElement: | ||
case types.AST_NODE_TYPES.JSXFragment: | ||
case AST_NODE_TYPES.JSXElement: | ||
case AST_NODE_TYPES.JSXFragment: | ||
return { kind: "JSXElement", node }; | ||
case types.AST_NODE_TYPES.ArrayExpression: | ||
case AST_NODE_TYPES.ArrayExpression: | ||
return { kind: "ArrayExpression", node }; | ||
case types.AST_NODE_TYPES.ObjectExpression: | ||
case AST_NODE_TYPES.ObjectExpression: | ||
return { kind: "ObjectExpression", node }; | ||
case types.AST_NODE_TYPES.ClassExpression: | ||
case AST_NODE_TYPES.ClassExpression: | ||
return { kind: "ClassExpression", node }; | ||
case types.AST_NODE_TYPES.NewExpression: | ||
case AST_NODE_TYPES.NewExpression: | ||
return { kind: "NewExpression", node }; | ||
case types.AST_NODE_TYPES.FunctionExpression: | ||
case types.AST_NODE_TYPES.ArrowFunctionExpression: | ||
case AST_NODE_TYPES.FunctionExpression: | ||
case AST_NODE_TYPES.ArrowFunctionExpression: | ||
return { kind: "FunctionExpression", node }; | ||
case types.AST_NODE_TYPES.CallExpression: { | ||
case AST_NODE_TYPES.CallExpression: { | ||
if (hint & ConstructionDetectionHint.StrictCallExpression) { | ||
return { kind: "CallExpression", node }; | ||
} | ||
return eff._; | ||
return _; | ||
} | ||
case types.AST_NODE_TYPES.MemberExpression: { | ||
if (!("object" in node)) return eff._; | ||
case AST_NODE_TYPES.MemberExpression: { | ||
if (!("object" in node)) return _; | ||
return getConstruction(node.object, initialScope, hint); | ||
} | ||
case types.AST_NODE_TYPES.AssignmentExpression: | ||
case types.AST_NODE_TYPES.AssignmentPattern: { | ||
if (!("right" in node)) return eff._; | ||
case AST_NODE_TYPES.AssignmentExpression: | ||
case AST_NODE_TYPES.AssignmentPattern: { | ||
if (!("right" in node)) return _; | ||
return getConstruction(node.right, initialScope, hint); | ||
} | ||
case types.AST_NODE_TYPES.LogicalExpression: { | ||
case AST_NODE_TYPES.LogicalExpression: { | ||
const lvc = getConstruction(node.left, initialScope, hint); | ||
if (lvc == null) return eff._; | ||
if (lvc == null) return _; | ||
return getConstruction(node.right, initialScope, hint); | ||
} | ||
case types.AST_NODE_TYPES.ConditionalExpression: { | ||
case AST_NODE_TYPES.ConditionalExpression: { | ||
const cvc = getConstruction(node.consequent, initialScope, hint); | ||
if (cvc == null) return eff._; | ||
if (cvc == null) return _; | ||
return getConstruction(node.alternate, initialScope, hint); | ||
} | ||
case types.AST_NODE_TYPES.Identifier: { | ||
case AST_NODE_TYPES.Identifier: { | ||
if (!("name" in node) || typeof node.name !== "string") { | ||
return eff._; | ||
return _; | ||
} | ||
@@ -157,11 +135,11 @@ const variable = initialScope.set.get(node.name); | ||
} | ||
case types.AST_NODE_TYPES.Literal: { | ||
case AST_NODE_TYPES.Literal: { | ||
if ("regex" in node) { | ||
return { kind: "RegExpLiteral", node }; | ||
} | ||
return eff._; | ||
return _; | ||
} | ||
default: { | ||
if (!("expression" in node) || typeof node.expression !== "object") { | ||
return eff._; | ||
return _; | ||
} | ||
@@ -173,10 +151,10 @@ return getConstruction(node.expression, initialScope, hint); | ||
function getVariableDeclaratorId(node, prev) { | ||
if (node == null) return eff._; | ||
if (node == null) return _; | ||
switch (true) { | ||
case (node.type === types.AST_NODE_TYPES.VariableDeclarator && node.init === prev): | ||
case (node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev): | ||
return node.id; | ||
case (node.type === types.AST_NODE_TYPES.AssignmentExpression && node.right === prev): | ||
case (node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev): | ||
return node.left; | ||
case (node.type === types.AST_NODE_TYPES.BlockStatement || node.type === types.AST_NODE_TYPES.Program || node.parent === node): | ||
return eff._; | ||
case (node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node): | ||
return _; | ||
default: | ||
@@ -200,10 +178,10 @@ return getVariableDeclaratorId(node.parent, node); | ||
} | ||
const staticValue = initialScope == null ? ASTUtils.getStaticValue(node) : ASTUtils.getStaticValue(node, initialScope); | ||
const staticValue = initialScope == null ? getStaticValue(node) : getStaticValue(node, initialScope); | ||
return staticValue == null ? { kind: "none", node, initialScope } : { kind: "some", node, initialScope, value: staticValue.value }; | ||
} | ||
var thisBlockTypes = [ | ||
types.AST_NODE_TYPES.FunctionDeclaration, | ||
types.AST_NODE_TYPES.FunctionExpression, | ||
types.AST_NODE_TYPES.ClassBody, | ||
types.AST_NODE_TYPES.Program | ||
AST_NODE_TYPES.FunctionDeclaration, | ||
AST_NODE_TYPES.FunctionExpression, | ||
AST_NODE_TYPES.ClassBody, | ||
AST_NODE_TYPES.Program | ||
]; | ||
@@ -216,9 +194,9 @@ function isNodeValueEqual(a, b, initialScopes) { | ||
} | ||
case (a.type === types.AST_NODE_TYPES.Literal && b.type === types.AST_NODE_TYPES.Literal): { | ||
case (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal): { | ||
return a.value === b.value; | ||
} | ||
case (a.type === types.AST_NODE_TYPES.TemplateElement && b.type === types.AST_NODE_TYPES.TemplateElement): { | ||
case (a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement): { | ||
return a.value.cooked === b.value.cooked; | ||
} | ||
case (a.type === types.AST_NODE_TYPES.Identifier && b.type === types.AST_NODE_TYPES.Identifier): { | ||
case (a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier): { | ||
const aVar = findVariable2(a, aScope); | ||
@@ -235,4 +213,4 @@ const bVar = findVariable2(b, bScope); | ||
switch (true) { | ||
case (aVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === types.AST_NODE_TYPES.CallExpression && AST__namespace.isFunction(aVarNode) && AST__namespace.isFunction(bVarNode)): { | ||
if (!AST__namespace.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) { | ||
case (aVarNodeParent?.type === AST_NODE_TYPES.CallExpression && bVarNodeParent?.type === AST_NODE_TYPES.CallExpression && AST.isFunction(aVarNode) && AST.isFunction(bVarNode)): { | ||
if (!AST.isNodeEqual(aVarNodeParent.callee, bVarNodeParent.callee)) { | ||
return false; | ||
@@ -242,7 +220,7 @@ } | ||
const bParams = bVarNode.params; | ||
const aPos = aParams.findIndex((x) => AST__namespace.isNodeEqual(x, a)); | ||
const bPos = bParams.findIndex((x) => AST__namespace.isNodeEqual(x, b)); | ||
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 === types.AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === types.AST_NODE_TYPES.ForOfStatement): { | ||
case (aDefParentParent?.type === AST_NODE_TYPES.ForOfStatement && bDefParentParent?.type === AST_NODE_TYPES.ForOfStatement): { | ||
const aLeft = aDefParentParent.left; | ||
@@ -255,3 +233,3 @@ const bLeft = bDefParentParent.left; | ||
const bRight = bDefParentParent.right; | ||
return AST__namespace.isNodeEqual(aRight, bRight); | ||
return AST.isNodeEqual(aRight, bRight); | ||
} | ||
@@ -263,11 +241,11 @@ default: { | ||
} | ||
case (a.type === types.AST_NODE_TYPES.MemberExpression && b.type === types.AST_NODE_TYPES.MemberExpression): { | ||
return AST__namespace.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes); | ||
case (a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression): { | ||
return AST.isNodeEqual(a.property, b.property) && isNodeValueEqual(a.object, b.object, initialScopes); | ||
} | ||
case (a.type === types.AST_NODE_TYPES.ThisExpression && b.type === types.AST_NODE_TYPES.ThisExpression): { | ||
case (a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression): { | ||
if (aScope.block === bScope.block) { | ||
return true; | ||
} | ||
const aFunction = AST__namespace.findParentNode(a, AST__namespace.isOneOf(thisBlockTypes)); | ||
const bFunction = AST__namespace.findParentNode(b, AST__namespace.isOneOf(thisBlockTypes)); | ||
const aFunction = AST.findParentNode(a, AST.isOneOf(thisBlockTypes)); | ||
const bFunction = AST.findParentNode(b, AST.isOneOf(thisBlockTypes)); | ||
return aFunction === bFunction; | ||
@@ -283,19 +261,10 @@ } | ||
function getVariableInitNodeLoose(variable, at) { | ||
if (variable == null) return eff._; | ||
if (variable == null) return _; | ||
const node = getVariableInitNode(variable, at); | ||
if (node != null) return node; | ||
const def = variable.defs.at(at); | ||
if (def?.type === scopeManager.DefinitionType.Parameter && AST__namespace.isFunction(def.node)) return def.node; | ||
return eff._; | ||
if (def?.type === DefinitionType.Parameter && AST.isFunction(def.node)) return def.node; | ||
return _; | ||
} | ||
exports.ConstructionDetectionHint = ConstructionDetectionHint; | ||
exports.findPropertyInProperties = findPropertyInProperties; | ||
exports.findVariable = findVariable2; | ||
exports.getChidScopes = getChidScopes; | ||
exports.getConstruction = getConstruction; | ||
exports.getVariableDeclaratorId = getVariableDeclaratorId; | ||
exports.getVariableInitNode = getVariableInitNode; | ||
exports.getVariables = getVariables; | ||
exports.isNodeValueEqual = isNodeValueEqual; | ||
exports.toStaticValue = toStaticValue; | ||
export { ConstructionDetectionHint, findPropertyInProperties, findVariable2 as findVariable, getChidScopes, getConstruction, getVariableDeclaratorId, getVariableInitNode, getVariables, isNodeValueEqual, toStaticValue }; |
{ | ||
"name": "@eslint-react/var", | ||
"version": "1.49.0-next.0", | ||
"version": "2.0.0-beta.13", | ||
"description": "ESLint React's TSESTree AST utility module for static analysis of variables.", | ||
@@ -17,18 +17,10 @@ "homepage": "https://github.com/Rel1cx/eslint-react", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
@@ -44,4 +36,4 @@ "dist", | ||
"ts-pattern": "^5.7.0", | ||
"@eslint-react/ast": "1.49.0-next.0", | ||
"@eslint-react/eff": "1.49.0-next.0" | ||
"@eslint-react/ast": "2.0.0-beta.13", | ||
"@eslint-react/eff": "2.0.0-beta.13" | ||
}, | ||
@@ -54,3 +46,3 @@ "devDependencies": { | ||
"bun": ">=1.0.15", | ||
"node": ">=18.18.0" | ||
"node": ">=20.19.0" | ||
}, | ||
@@ -57,0 +49,0 @@ "scripts": { |
Yes
NaN16108
-48.63%4
-33.33%347
-44.66%+ Added
+ Added
- Removed
- Removed