Skip to content

Commit

Permalink
refactor: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx committed Feb 25, 2025
1 parent 70e1a58 commit a46f5cc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 233 deletions.
34 changes: 8 additions & 26 deletions packages/core/src/utils/is-from-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ export function isFromReactMember(
const settings = unsafeDecodeSettings(context.settings);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!settings.strictImportCheck) {
if (node.property.type !== T.Identifier || node.property.name !== name) {
return false;
}
if (node.object.type === T.Identifier && node.object.name === memberName) {
return true;
}
if (node.property.type !== T.Identifier || node.property.name !== name) return false;
if (node.object.type === T.Identifier && node.object.name === memberName) return true;
if (
node.object.type === T.MemberExpression
&& node.object.object.type === T.Identifier
Expand Down Expand Up @@ -103,12 +99,8 @@ export declare namespace isCallFromReact {

export function isCallFromReact(name: string): isCallFromReact.ReturnType {
return dual(2, (node: TSESTree.Node, context: RuleContext): node is TSESTree.CallExpression => {
if (node.type !== T.CallExpression) {
return false;
}
if (!AST.isOneOf([T.Identifier, T.MemberExpression])(node.callee)) {
return false;
}
if (node.type !== T.CallExpression) return false;
if (!AST.isOneOf([T.Identifier, T.MemberExpression])(node.callee)) return false;
return isFromReact(name)(node.callee, context);
});
}
Expand All @@ -118,10 +110,7 @@ export declare namespace isCallFromReactMember {
(context: RuleContext): (node: TSESTree.Node) => node is
& TSESTree.CallExpression
& { callee: TSESTree.MemberExpression };
(
node: TSESTree.Node,
context: RuleContext,
): node is
(node: TSESTree.Node, context: RuleContext): node is
& TSESTree.CallExpression
& { callee: TSESTree.MemberExpression };
};
Expand All @@ -131,19 +120,12 @@ export function isCallFromReactMember(
pragmaMemberName: string,
name: string,
): isCallFromReactMember.ReturnType {
return dual(2, (
node: TSESTree.Node,
context: RuleContext,
): node is
return dual(2, (node: TSESTree.Node, context: RuleContext): node is
& TSESTree.CallExpression
& { callee: TSESTree.MemberExpression } =>
{
if (node.type !== T.CallExpression) {
return false;
}
if (!AST.is(T.MemberExpression)(node.callee)) {
return false;
}
if (node.type !== T.CallExpression) return false;
if (!AST.is(T.MemberExpression)(node.callee)) return false;
return isFromReactMember(pragmaMemberName, name)(node.callee, context);
});
}
38 changes: 17 additions & 21 deletions packages/core/src/utils/is-initialized-from-react.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import * as AST from "@eslint-react/ast";
import { _ } from "@eslint-react/eff";
import { _, identity } from "@eslint-react/eff";
import * as VAR from "@eslint-react/var";
import type { Scope } from "@typescript-eslint/scope-manager";
import type { TSESTree } from "@typescript-eslint/types";
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
import { match, P } from "ts-pattern";

/**
* Get the arguments of a require expression
* @param node The node to match
* @returns The require expression arguments or undefined if the node is not a require expression
*/
function getRequireExpressionArguments(node: TSESTree.Node) {
return match<typeof node, TSESTree.CallExpressionArgument[] | _>(node)
// require("source")
.with({ type: T.CallExpression, arguments: P.select(), callee: { type: T.Identifier, name: "require" } }, identity)
// require("source").variable
.with({ type: T.MemberExpression, object: P.select() }, getRequireExpressionArguments)
.otherwise(() => _);
}

/**
* Check if an identifier is initialized from react
Expand Down Expand Up @@ -38,27 +53,8 @@ export function isInitializedFromReact(
return false;
}
// check for: `require('source')` or `require('source/...')`
return arg0.value === source
|| arg0
.value
.startsWith(`${source}/`);
return arg0.value === source || arg0.value.startsWith(`${source}/`);
}
// latest definition is an import declaration: import { variable } from 'source'
return parent?.type === T.ImportDeclaration && parent.source.value === source;
}

function getRequireExpressionArguments(node: TSESTree.Node): TSESTree.CallExpressionArgument[] | _ {
switch (true) {
// require('source')
case node.type === T.CallExpression
&& node.callee.type === T.Identifier
&& node.callee.name === "require": {
return node.arguments;
}
// require('source').variable
case node.type === T.MemberExpression: {
return getRequireExpressionArguments(node.object);
}
}
return _;
}
Loading

0 comments on commit a46f5cc

Please sign in to comment.