-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfunction-component.ts
More file actions
64 lines (58 loc) · 1.77 KB
/
Copy pathfunction-component.ts
File metadata and controls
64 lines (58 loc) · 1.77 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as core from "@eslint-react/core";
import { type RuleContext, type RuleFeature, merge } from "@eslint-react/eslint";
import { createRule } from "@/utils/create-rule";
import { stringify } from "@/utils/stringify";
export const RULE_NAME = "function-component";
export const RULE_FEATURES = [
"DBG",
] as const satisfies RuleFeature[];
export type MessageID = "default";
export default createRule<[], MessageID>({
meta: {
type: "problem",
docs: {
description: "Reports all function components in JSON format.",
},
messages: {
default: "{{json}}",
// "[function component] name: {{name}}, memo: {{memo}}, forwardRef: {{forwardRef}}, hookCalls: {{hookCalls}}, displayName: {{displayName}}.",
},
schema: [],
},
name: RULE_NAME,
create,
defaultOptions: [],
});
export function create(context: RuleContext<MessageID, []>) {
const { api, visitor } = core.getFunctionComponentCollector(
context,
{
collectDisplayName: true,
hint: core.DEFAULT_COMPONENT_DETECTION_HINT,
},
);
return merge(
visitor,
{
"Program:exit"(program) {
for (const { name, displayName, flag, hookCalls, node } of api.getAllComponents(program)) {
context.report({
data: {
json: stringify({
name: name ?? "anonymous",
displayName: displayName == null
? "none"
: context.sourceCode.getText(displayName),
forwardRef: (flag & core.FunctionComponentFlag.ForwardRef) > 0n,
hookCalls: hookCalls.length,
memo: (flag & core.FunctionComponentFlag.Memo) > 0n,
}),
},
messageId: "default",
node,
});
}
},
},
);
}