Skip to content

Commit c7dccdd

Browse files
committed
refactor: unify method and property type definitions under TSESTreeMethodOrProperty
1 parent a4f7322 commit c7dccdd

16 files changed

+112
-65
lines changed

packages/core/docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- [isClassComponent](functions/isClassComponent.md)
6363
- [isCloneElement](functions/isCloneElement.md)
6464
- [isCloneElementCall](functions/isCloneElementCall.md)
65+
- [isComponentDidCatch](functions/isComponentDidCatch.md)
6566
- [isComponentDidMount](functions/isComponentDidMount.md)
6667
- [isComponentName](functions/isComponentName.md)
6768
- [isComponentWillUnmount](functions/isComponentWillUnmount.md)
@@ -80,6 +81,8 @@
8081
- [isFunctionOfRenderMethod](functions/isFunctionOfRenderMethod.md)
8182
- [isFunctionOfUseEffectCleanup](functions/isFunctionOfUseEffectCleanup.md)
8283
- [isFunctionOfUseEffectSetup](functions/isFunctionOfUseEffectSetup.md)
84+
- [isGetDerivedStateFromError](functions/isGetDerivedStateFromError.md)
85+
- [isGetDerivedStateFromProps](functions/isGetDerivedStateFromProps.md)
8386
- [isInitializedFromReact](functions/isInitializedFromReact.md)
8487
- [isInsideCreateElementProps](functions/isInsideCreateElementProps.md)
8588
- [isInsideReactHook](functions/isInsideReactHook.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) / isComponentDidCatch
6+
7+
# Function: isComponentDidCatch()
8+
9+
> **isComponentDidCatch**(`node`): `node is TSESTreeMethodOrProperty`
10+
11+
## Parameters
12+
13+
### node
14+
15+
`Node`
16+
17+
## Returns
18+
19+
`node is TSESTreeMethodOrProperty`

packages/core/docs/functions/isComponentDidMount.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Function: isComponentDidMount()
88

9-
> **isComponentDidMount**(`node`): node is MethodDefinition \| PropertyDefinition
9+
> **isComponentDidMount**(`node`): `node is TSESTreeMethodOrProperty`
1010
1111
## Parameters
1212

@@ -16,4 +16,4 @@
1616

1717
## Returns
1818

19-
node is MethodDefinition \| PropertyDefinition
19+
`node is TSESTreeMethodOrProperty`

packages/core/docs/functions/isComponentWillUnmount.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Function: isComponentWillUnmount()
88

9-
> **isComponentWillUnmount**(`node`): node is MethodDefinition \| PropertyDefinition
9+
> **isComponentWillUnmount**(`node`): `node is TSESTreeMethodOrProperty`
1010
1111
## Parameters
1212

@@ -16,4 +16,4 @@
1616

1717
## Returns
1818

19-
node is MethodDefinition \| PropertyDefinition
19+
`node is TSESTreeMethodOrProperty`
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) / isGetDerivedStateFromError
6+
7+
# Function: isGetDerivedStateFromError()
8+
9+
> **isGetDerivedStateFromError**(`node`): `node is TSESTreeMethodOrProperty`
10+
11+
## Parameters
12+
13+
### node
14+
15+
`Node`
16+
17+
## Returns
18+
19+
`node is TSESTreeMethodOrProperty`
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) / isGetDerivedStateFromProps
6+
7+
# Function: isGetDerivedStateFromProps()
8+
9+
> **isGetDerivedStateFromProps**(`node`): `node is TSESTreeMethodOrProperty`
10+
11+
## Parameters
12+
13+
### node
14+
15+
`Node`
16+
17+
## Returns
18+
19+
`node is TSESTreeMethodOrProperty`

packages/core/docs/functions/isRenderMethodLike.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Function: isRenderMethodLike()
88

9-
> **isRenderMethodLike**(`node`): node is MethodDefinition \| PropertyDefinition
9+
> **isRenderMethodLike**(`node`): `node is TSESTreeMethodOrProperty`
1010
1111
## Parameters
1212

@@ -16,4 +16,4 @@
1616

1717
## Returns
1818

19-
node is MethodDefinition \| PropertyDefinition
19+
`node is TSESTreeMethodOrProperty`

packages/core/docs/interfaces/ERClassComponent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
### methods
7272

73-
> **methods**: (`MethodDefinition` \| `PropertyDefinition`)[]
73+
> **methods**: `TSESTreeMethodOrProperty`[]
7474
7575
***
7676

packages/core/src/component/component-lifecycle.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@ import type { TSESTree } from "@typescript-eslint/types";
33
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
44

55
import { isClassComponent } from "./is";
6+
import { isMatching, P } from "ts-pattern";
67

7-
export function isComponentDidMount(
8-
node: TSESTree.Node,
9-
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
10-
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
8+
export function isComponentDidMount(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
9+
return AST.isMethodOrProperty(node)
1110
&& node.key.type === T.Identifier
1211
&& node.key.name === "componentDidMount";
1312
}
1413

15-
export function isComponentWillUnmount(
16-
node: TSESTree.Node,
17-
): node is TSESTree.MethodDefinition | TSESTree.PropertyDefinition {
18-
return AST.isOneOf([T.MethodDefinition, T.PropertyDefinition])(node)
14+
export function isComponentWillUnmount(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
15+
return AST.isMethodOrProperty(node)
1916
&& node.key.type === T.Identifier
2017
&& node.key.name === "componentWillUnmount";
2118
}
2219

20+
export function isComponentDidCatch(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
21+
return AST.isMethodOrProperty(node)
22+
&& !node.static
23+
&& node.key.type === T.Identifier
24+
&& node.key.name === "componentDidCatch";
25+
}
26+
27+
export function isGetDerivedStateFromError(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
28+
return AST.isMethodOrProperty(node)
29+
&& node.static
30+
&& node.key.type === T.Identifier
31+
&& node.key.name === "getDerivedStateFromError";
32+
}
33+
34+
export function isGetDerivedStateFromProps(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
35+
return AST.isMethodOrProperty(node)
36+
&& node.static
37+
&& node.key.type === T.Identifier
38+
&& node.key.name === "getDerivedStateFromProps";
39+
}
40+
2341
export function isFunctionOfComponentDidMount(node: TSESTree.Node) {
2442
return AST.isFunction(node)
2543
&& isComponentDidMount(node.parent)
@@ -32,11 +50,8 @@ export function isFunctionOfComponentWillUnmount(node: TSESTree.Node) {
3250
&& node.parent.value === node;
3351
}
3452

35-
export function isRenderMethodLike(node: TSESTree.Node): node is
36-
| TSESTree.MethodDefinition
37-
| TSESTree.PropertyDefinition
38-
{
39-
return (node.type === T.MethodDefinition || node.type === T.PropertyDefinition)
53+
export function isRenderMethodLike(node: TSESTree.Node): node is AST.TSESTreeMethodOrProperty {
54+
return AST.isMethodOrProperty(node)
4055
&& node.key.type === T.Identifier
4156
&& node.key.name === "render"
4257
&& node.parent.parent.type === T.ClassDeclaration;

packages/core/src/component/component-semantic-node.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ export interface ERClassComponent extends ERSemanticNode {
3333
node: AST.TSESTreeClass;
3434
flag: ERClassComponentFlag;
3535
hint: ERComponentHint;
36-
methods: Array<
37-
| TSESTree.MethodDefinition
38-
| TSESTree.PropertyDefinition
39-
>;
36+
methods: AST.TSESTreeMethodOrProperty[];
4037
displayName:
4138
| _
4239
| TSESTree.Expression;

0 commit comments

Comments
 (0)