@@ -3,23 +3,41 @@ import type { TSESTree } from "@typescript-eslint/types";
33import { AST_NODE_TYPES as T } from "@typescript-eslint/types" ;
44
55import { 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+
2341export 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 ;
0 commit comments