Skip to content

Commit 4161c0c

Browse files
authored
refactor: remove unnecessary utils and its usage (#912)
1 parent 65dec46 commit 4161c0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+174
-8422
lines changed

packages/core/docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
- [isReactHookCallWithNameLoose](functions/isReactHookCallWithNameLoose.md)
9696
- [isReactHookName](functions/isReactHookName.md)
9797
- [isRenderFunctionLoose](functions/isRenderFunctionLoose.md)
98+
- [isRenderMethodLike](functions/isRenderMethodLike.md)
9899
- [isRenderPropLoose](functions/isRenderPropLoose.md)
99100
- [isSetupFunction](functions/isSetupFunction.md)
100101
- [isThisSetState](functions/isThisSetState.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[**@eslint-react/core**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/core](../README.md) / isRenderMethodLike
6+
7+
# Function: isRenderMethodLike()
8+
9+
> **isRenderMethodLike**(`node`): node is MethodDefinition \| PropertyDefinition
10+
11+
## Parameters
12+
13+
### node
14+
15+
`Node`
16+
17+
## Returns
18+
19+
node is MethodDefinition \| PropertyDefinition

packages/core/src/component/component-collector-legacy.ts

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,11 @@
11
import * as AST from "@eslint-react/ast";
22
import { O } from "@eslint-react/eff";
3-
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
43
import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
5-
import { match, P } from "ts-pattern";
64

75
import { getId } from "../utils";
86
import type { ERClassComponent } from "./component";
97
import { ERClassComponentFlag } from "./component-flag";
10-
11-
/**
12-
* Check if a node is a React class component
13-
* @param node The AST node to check
14-
* @returns `true` if the node is a class component, `false` otherwise
15-
*/
16-
export function isClassComponent(node: TSESTree.Node): node is AST.TSESTreeClass {
17-
if (!("superClass" in node && node.superClass)) {
18-
return false;
19-
}
20-
const { superClass } = node;
21-
return match(superClass)
22-
.with({
23-
type: T.Identifier,
24-
name: P.string,
25-
}, ({ name }) => /^(?:Pure)?Component$/u.test(name))
26-
.with({
27-
type: T.MemberExpression,
28-
property: { name: P.string },
29-
}, ({ property }) => /^(?:Pure)?Component$/u.test(property.name))
30-
.otherwise(() => false);
31-
}
32-
33-
/**
34-
* Check if a node is a React PureComponent
35-
* @param node The AST node to check
36-
* @returns `true` if the node is a pure component, `false` otherwise
37-
*/
38-
export function isPureComponent(node: TSESTree.Node) {
39-
if ("superClass" in node && node.superClass) {
40-
return match(node.superClass)
41-
.with({
42-
type: T.Identifier,
43-
name: P.string,
44-
}, ({ name }) => /^PureComponent$/u.test(name))
45-
.with({
46-
type: T.MemberExpression,
47-
property: { name: P.string },
48-
}, ({ property }) => /^PureComponent$/u.test(property.name))
49-
.otherwise(() => false);
50-
}
51-
return false;
52-
}
53-
54-
export function isComponentDidMount(
55-
node: TSESTree.Node,
56-
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
57-
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
58-
&& node.key.type === T.Identifier
59-
&& node.key.name === "componentDidMount";
60-
}
61-
62-
export function isComponentWillUnmount(
63-
node: TSESTree.Node,
64-
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
65-
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
66-
&& node.key.type === T.Identifier
67-
&& node.key.name === "componentWillUnmount";
68-
}
69-
70-
export function isComponentDidMountFunction(node: TSESTree.Node) {
71-
return AST.isFunction(node)
72-
&& isComponentDidMount(node.parent)
73-
&& node.parent.value === node;
74-
}
75-
76-
export function isComponentWillUnmountFunction(node: TSESTree.Node) {
77-
return AST.isFunction(node)
78-
&& isComponentWillUnmount(node.parent)
79-
&& node.parent.value === node;
80-
}
8+
import { isClassComponent, isPureComponent } from "./is";
819

8210
export function useComponentCollectorLegacy() {
8311
const components = new Map<string, ERClassComponent>();

packages/core/src/component/component-render-method.ts

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

7-
import { isClassComponent } from "./component-collector-legacy";
6+
import { isClassComponent } from "./is";
87

9-
const isRenderMethodLike = isMatching({
10-
key: {
11-
type: T.Identifier,
12-
name: "render",
13-
},
14-
type: P.union(T.MethodDefinition, T.PropertyDefinition),
15-
parent: {
16-
type: T.ClassBody,
17-
parent: {
18-
type: T.ClassDeclaration,
19-
},
20-
},
21-
});
8+
export function isRenderMethodLike(node: TSESTree.Node): node is
9+
| TSESTree.MethodDefinition
10+
| TSESTree.PropertyDefinition
11+
{
12+
return (node.type === T.MethodDefinition || node.type === T.PropertyDefinition)
13+
&& node.key.type === T.Identifier
14+
&& node.key.name === "render"
15+
&& node.parent.parent.type === T.ClassDeclaration;
16+
}
2217

2318
export function isFunctionOfRenderMethod(node: AST.TSESTreeFunction) {
2419
if (!isRenderMethodLike(node.parent)) {
@@ -44,7 +39,7 @@ export function isFunctionOfRenderMethod(node: AST.TSESTreeFunction) {
4439
* @returns `true` if node is inside class component's render block, `false` if not
4540
*/
4641
export function isInsideRenderMethod(node: TSESTree.Node) {
47-
return O.isSome(AST.findParentNode(node, (node: TSESTree.Node) => {
48-
return isRenderMethodLike(node) && isClassComponent(node.parent.parent);
49-
}));
42+
return O.isSome(AST.findParentNode(node, (node) =>
43+
isRenderMethodLike(node)
44+
&& isClassComponent(node.parent.parent)));
5045
}

packages/core/src/component/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export type * from "./component-kind";
88
export type * from "./component-method-kind";
99
export * from "./component-name";
1010
export * from "./component-render-method";
11+
export * from "./is";
1112
export * from "./misc";

packages/core/src/component/is.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type * as AST from "@eslint-react/ast";
2+
import type { TSESTree } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
4+
5+
/**
6+
* Check if a node is a React class component
7+
* @param node The AST node to check
8+
* @returns `true` if the node is a class component, `false` otherwise
9+
*/
10+
export function isClassComponent(node: TSESTree.Node): node is AST.TSESTreeClass {
11+
if ("superClass" in node && node.superClass) {
12+
const re = /^(?:Pure)?Component$/u;
13+
switch (true) {
14+
case node.superClass.type === T.Identifier:
15+
return re.test(node.superClass.name);
16+
case node.superClass.type === T.MemberExpression
17+
&& node.superClass.property.type === T.Identifier:
18+
return re.test(node.superClass.property.name);
19+
}
20+
}
21+
return false;
22+
}
23+
24+
/**
25+
* Check if a node is a React PureComponent
26+
* @param node The AST node to check
27+
* @returns `true` if the node is a pure component, `false` otherwise
28+
*/
29+
export function isPureComponent(node: TSESTree.Node) {
30+
if ("superClass" in node && node.superClass) {
31+
const re = /^PureComponent$/u;
32+
switch (true) {
33+
case node.superClass.type === T.Identifier:
34+
return re.test(node.superClass.name);
35+
case node.superClass.type === T.MemberExpression
36+
&& node.superClass.property.type === T.Identifier:
37+
return re.test(node.superClass.property.name);
38+
}
39+
}
40+
return false;
41+
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./component";
22
export * from "./effect";
33
export * from "./element";
44
export * from "./hook";
5+
export * from "./lifecycle";
56
export * from "./phase";
67
export * from "./render-prop";
78
export type * from "./semantic-entry";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./is";

packages/core/src/lifecycle/is.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as AST from "@eslint-react/ast";
2+
import type { TSESTree } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
4+
5+
export function isComponentDidMount(
6+
node: TSESTree.Node,
7+
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
8+
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
9+
&& node.key.type === T.Identifier
10+
&& node.key.name === "componentDidMount";
11+
}
12+
13+
export function isComponentWillUnmount(
14+
node: TSESTree.Node,
15+
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
16+
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
17+
&& node.key.type === T.Identifier
18+
&& node.key.name === "componentWillUnmount";
19+
}
20+
21+
export function isComponentDidMountFunction(node: TSESTree.Node) {
22+
return AST.isFunction(node)
23+
&& isComponentDidMount(node.parent)
24+
&& node.parent.value === node;
25+
}
26+
27+
export function isComponentWillUnmountFunction(node: TSESTree.Node) {
28+
return AST.isFunction(node)
29+
&& isComponentWillUnmount(node.parent)
30+
&& node.parent.value === node;
31+
}

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-effect.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export default createRule<[], MessageID>({
145145
}
146146
}
147147
})
148-
// .with(P.union("useMemo", "useCallback"), () => {})
149148
.with("useEffect", () => {
150149
if (AST.isFunction(node.arguments.at(0))) {
151150
return;

0 commit comments

Comments
 (0)