|
1 | 1 | import * as AST from "@eslint-react/ast"; |
2 | 2 | import { O } from "@eslint-react/eff"; |
3 | | -import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
4 | 3 | import type { ESLintUtils, TSESTree } from "@typescript-eslint/utils"; |
5 | | -import { match, P } from "ts-pattern"; |
6 | 4 |
|
7 | 5 | import { getId } from "../utils"; |
8 | 6 | import type { ERClassComponent } from "./component"; |
9 | 7 | 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"; |
81 | 9 |
|
82 | 10 | export function useComponentCollectorLegacy() { |
83 | 11 | const components = new Map<string, ERClassComponent>(); |
|
0 commit comments