Skip to content

Commit 26deac7

Browse files
committed
refactor: replace isMatching with direct type checks for improved performance
1 parent 29d1087 commit 26deac7

File tree

5 files changed

+19
-29
lines changed

5 files changed

+19
-29
lines changed

packages/core/src/render-prop/hierarchy.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as AST from "@eslint-react/ast";
22
import { F, O } from "@eslint-react/eff";
33
import type { TSESTree } from "@typescript-eslint/types";
44
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
5-
import { isMatching, P } from "ts-pattern";
65

76
/**
87
* Unsafe check whether given node is declared directly inside a render property
@@ -17,15 +16,12 @@ import { isMatching, P } from "ts-pattern";
1716
* @returns `true` if component is declared inside a render property, `false` if not
1817
*/
1918
export function isDirectValueOfRenderPropertyLoose(node: TSESTree.Node) {
20-
const matching = isMatching({
21-
key: {
22-
type: T.Identifier,
23-
name: P.string.startsWith("render"),
24-
},
25-
type: T.Property,
26-
});
27-
28-
return matching(node) || matching(node.parent);
19+
const matching = (node: TSESTree.Node) => {
20+
return node.type === T.Property
21+
&& node.key.type === T.Identifier
22+
&& node.key.name.startsWith("render");
23+
};
24+
return matching(node) || (node.parent != null && matching(node.parent));
2925
}
3026

3127
/**
@@ -49,9 +45,9 @@ export function isDeclaredInRenderPropLoose(node: TSESTree.Node) {
4945
O.flatMapNullable(c => c.parent),
5046
O.filter(AST.is(T.JSXAttribute)),
5147
O.flatMapNullable(a => a.name),
52-
O.exists(isMatching({
53-
type: T.JSXIdentifier,
54-
name: P.string.startsWith("render"),
55-
})),
48+
O.exists(n =>
49+
n.type === T.JSXIdentifier
50+
&& n.name.startsWith("render")
51+
),
5652
);
5753
}

packages/plugins/eslint-plugin-react-hooks-extra/src/utils/is-set-function-call.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { RuleContext } from "@eslint-react/types";
44
import * as VAR from "@eslint-react/var";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
7-
import { isMatching } from "ts-pattern";
87

98
import { isFromUseStateCall } from "./is-from-use-state-call";
109

@@ -22,13 +21,7 @@ export function isSetFunctionCall(context: RuleContext, settings: ESLintReactSet
2221
if (!("name" in callee.object)) {
2322
return false;
2423
}
25-
const isAt = isMatching({
26-
type: T.MemberExpression,
27-
property: {
28-
type: T.Identifier,
29-
name: "at",
30-
},
31-
}, callee);
24+
const isAt = callee.property.type === T.Identifier && callee.property.name === "at";
3225
const [index] = node.callee.arguments;
3326
if (!isAt || index == null) {
3427
return false;

packages/plugins/eslint-plugin-react-x/src/rules/no-leaked-conditional-rendering.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ export default createRule<[], MessageID>({
243243
return match<typeof node, O.Option<ReportDescriptor<MessageID>>>(node)
244244
.when(AST.isJSX, O.none)
245245
.with({ type: T.LogicalExpression, operator: "&&" }, ({ left, right }) => {
246-
const isLeftUnaryNot = isMatching({ type: T.UnaryExpression, operator: "!" }, left);
246+
const isLeftUnaryNot = left.type === T.UnaryExpression && left.operator === "!";
247247
if (isLeftUnaryNot) {
248248
return getReportDescriptor(right);
249249
}
250250
const initialScope = context.sourceCode.getScope(left);
251-
const isLeftNan = isMatching({ type: T.Identifier, name: "NaN" }, left)
251+
const isLeftNan = (left.type === T.Identifier && left.name === "NaN")
252252
|| O.exists(VAR.getStaticValue(left, initialScope), v => v === "NaN");
253253
if (isLeftNan) {
254254
return O.some({
@@ -283,7 +283,7 @@ export default createRule<[], MessageID>({
283283
})
284284
.otherwise(O.none);
285285
}
286-
const visitorFunction = F.flow(getReportDescriptor, O.map(context.report), F.constVoid);
286+
const visitorFunction = F.flow(getReportDescriptor, O.map(context.report));
287287
return {
288288
"JSXExpressionContainer > ConditionalExpression": visitorFunction,
289289
"JSXExpressionContainer > LogicalExpression": visitorFunction,

packages/plugins/eslint-plugin-react-x/src/rules/prefer-destructuring-assignment.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Scope } from "@typescript-eslint/scope-manager";
66
import type { TSESTree } from "@typescript-eslint/types";
77
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
88
import type { CamelCase } from "string-ts";
9-
import { isMatching } from "ts-pattern";
109

1110
import { createRule } from "../utils";
1211

@@ -85,7 +84,10 @@ export default createRule<[], MessageID>({
8584
continue;
8685
}
8786
const [props, ctx] = component.params;
88-
const isMatch = isMatching({ name: memberExpression.object.name });
87+
const isMatch = (node: null | TSESTree.Node | undefined) =>
88+
node != null
89+
&& node.type === T.Identifier
90+
&& node.name === memberExpression.object.name;
8991
if (isMatch(props)) {
9092
context.report({
9193
messageId: "preferDestructuringAssignment",

packages/utilities/var/src/is-initialized-from-source.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { F, O } from "@eslint-react/eff";
33
import type { Scope } from "@typescript-eslint/scope-manager";
44
import type { TSESTree } from "@typescript-eslint/types";
55
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
6-
import { isMatching } from "ts-pattern";
76

87
import { findVariable } from "./find-variable";
98

@@ -44,7 +43,7 @@ export function isInitializedFromSource(
4443
);
4544
}
4645
// latest definition is an import declaration: import { variable } from 'source'
47-
return isMatching({ type: "ImportDeclaration", source: { value: source } }, parent);
46+
return parent?.type === T.ImportDeclaration && parent.source.value === source;
4847
}
4948

5049
function getRequireExpressionArguments(node: TSESTree.Node): O.Option<TSESTree.CallExpressionArgument[]> {

0 commit comments

Comments
 (0)