Skip to content

Commit a46f5cc

Browse files
committed
refactor: minor improvements
1 parent 70e1a58 commit a46f5cc

File tree

3 files changed

+87
-233
lines changed

3 files changed

+87
-233
lines changed

packages/core/src/utils/is-from-react.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ export function isFromReactMember(
5959
const settings = unsafeDecodeSettings(context.settings);
6060
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
6161
if (!settings.strictImportCheck) {
62-
if (node.property.type !== T.Identifier || node.property.name !== name) {
63-
return false;
64-
}
65-
if (node.object.type === T.Identifier && node.object.name === memberName) {
66-
return true;
67-
}
62+
if (node.property.type !== T.Identifier || node.property.name !== name) return false;
63+
if (node.object.type === T.Identifier && node.object.name === memberName) return true;
6864
if (
6965
node.object.type === T.MemberExpression
7066
&& node.object.object.type === T.Identifier
@@ -103,12 +99,8 @@ export declare namespace isCallFromReact {
10399

104100
export function isCallFromReact(name: string): isCallFromReact.ReturnType {
105101
return dual(2, (node: TSESTree.Node, context: RuleContext): node is TSESTree.CallExpression => {
106-
if (node.type !== T.CallExpression) {
107-
return false;
108-
}
109-
if (!AST.isOneOf([T.Identifier, T.MemberExpression])(node.callee)) {
110-
return false;
111-
}
102+
if (node.type !== T.CallExpression) return false;
103+
if (!AST.isOneOf([T.Identifier, T.MemberExpression])(node.callee)) return false;
112104
return isFromReact(name)(node.callee, context);
113105
});
114106
}
@@ -118,10 +110,7 @@ export declare namespace isCallFromReactMember {
118110
(context: RuleContext): (node: TSESTree.Node) => node is
119111
& TSESTree.CallExpression
120112
& { callee: TSESTree.MemberExpression };
121-
(
122-
node: TSESTree.Node,
123-
context: RuleContext,
124-
): node is
113+
(node: TSESTree.Node, context: RuleContext): node is
125114
& TSESTree.CallExpression
126115
& { callee: TSESTree.MemberExpression };
127116
};
@@ -131,19 +120,12 @@ export function isCallFromReactMember(
131120
pragmaMemberName: string,
132121
name: string,
133122
): isCallFromReactMember.ReturnType {
134-
return dual(2, (
135-
node: TSESTree.Node,
136-
context: RuleContext,
137-
): node is
123+
return dual(2, (node: TSESTree.Node, context: RuleContext): node is
138124
& TSESTree.CallExpression
139125
& { callee: TSESTree.MemberExpression } =>
140126
{
141-
if (node.type !== T.CallExpression) {
142-
return false;
143-
}
144-
if (!AST.is(T.MemberExpression)(node.callee)) {
145-
return false;
146-
}
127+
if (node.type !== T.CallExpression) return false;
128+
if (!AST.is(T.MemberExpression)(node.callee)) return false;
147129
return isFromReactMember(pragmaMemberName, name)(node.callee, context);
148130
});
149131
}

packages/core/src/utils/is-initialized-from-react.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import * as AST from "@eslint-react/ast";
2-
import { _ } from "@eslint-react/eff";
2+
import { _, identity } from "@eslint-react/eff";
33
import * as VAR from "@eslint-react/var";
44
import type { Scope } from "@typescript-eslint/scope-manager";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
7+
import { match, P } from "ts-pattern";
8+
9+
/**
10+
* Get the arguments of a require expression
11+
* @param node The node to match
12+
* @returns The require expression arguments or undefined if the node is not a require expression
13+
*/
14+
function getRequireExpressionArguments(node: TSESTree.Node) {
15+
return match<typeof node, TSESTree.CallExpressionArgument[] | _>(node)
16+
// require("source")
17+
.with({ type: T.CallExpression, arguments: P.select(), callee: { type: T.Identifier, name: "require" } }, identity)
18+
// require("source").variable
19+
.with({ type: T.MemberExpression, object: P.select() }, getRequireExpressionArguments)
20+
.otherwise(() => _);
21+
}
722

823
/**
924
* Check if an identifier is initialized from react
@@ -38,27 +53,8 @@ export function isInitializedFromReact(
3853
return false;
3954
}
4055
// check for: `require('source')` or `require('source/...')`
41-
return arg0.value === source
42-
|| arg0
43-
.value
44-
.startsWith(`${source}/`);
56+
return arg0.value === source || arg0.value.startsWith(`${source}/`);
4557
}
4658
// latest definition is an import declaration: import { variable } from 'source'
4759
return parent?.type === T.ImportDeclaration && parent.source.value === source;
4860
}
49-
50-
function getRequireExpressionArguments(node: TSESTree.Node): TSESTree.CallExpressionArgument[] | _ {
51-
switch (true) {
52-
// require('source')
53-
case node.type === T.CallExpression
54-
&& node.callee.type === T.Identifier
55-
&& node.callee.name === "require": {
56-
return node.arguments;
57-
}
58-
// require('source').variable
59-
case node.type === T.MemberExpression: {
60-
return getRequireExpressionArguments(node.object);
61-
}
62-
}
63-
return _;
64-
}

0 commit comments

Comments
 (0)