-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcomponent-id.ts
More file actions
42 lines (40 loc) · 1.41 KB
/
Copy pathcomponent-id.ts
File metadata and controls
42 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as ast from "@eslint-react/ast";
import { unit } from "@eslint-react/eff";
import type { RuleContext } from "@eslint-react/shared";
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
import { isComponentWrapperCallLoose } from "./component-wrapper";
/**
* Get function component identifier from `const Component = memo(() => {});`
* @param context The rule context
* @param node The function node to analyze
* @returns The function identifier or `unit` if not found
*/
export function getFunctionComponentId(
context: RuleContext,
node: ast.TSESTreeFunction,
): ast.FunctionID | unit {
const functionId = ast.getFunctionId(node);
if (functionId != null) {
return functionId;
}
const { parent } = node;
// Get function component identifier from `const Component = memo(() => {});`
if (
parent.type === AST.CallExpression
&& isComponentWrapperCallLoose(context, parent)
&& parent.parent.type === AST.VariableDeclarator
) {
return parent.parent.id;
}
// Get function component identifier from `const Component = memo(forwardRef(() => {}));`
if (
parent.type === AST.CallExpression
&& isComponentWrapperCallLoose(context, parent)
&& parent.parent.type === AST.CallExpression
&& isComponentWrapperCallLoose(context, parent.parent)
&& parent.parent.parent.type === AST.VariableDeclarator
) {
return parent.parent.parent.id;
}
return unit;
}