Skip to content

Commit b7ddbf9

Browse files
committed
Remove redundant code in component/hook collectors
1 parent 7218ff8 commit b7ddbf9

3 files changed

Lines changed: 36 additions & 88 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"editor.codeActionsOnSave": {
1616
"source.fixAll": "never",
1717
"source.fixAll.eslint": "explicit",
18-
"source.formatting": "explicit",
18+
"source.formatting": "always",
1919
"source.organizeImports": "never"
2020
},
2121
"eslint.rules.customizations": [

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

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@ import { hasNoneOrLooseComponentName } from "./component-name";
1818

1919
const idGen = new IdGenerator("function_component_");
2020

21-
type FunctionEntry = {
22-
key: string;
23-
node: AST.TSESTreeFunction;
24-
hookCalls: TSESTree.CallExpression[];
25-
isComponent: boolean;
21+
type FunctionEntry = FunctionComponentSemanticNode & {
2622
isComponentDefinition: boolean;
27-
isExportDefault: boolean;
28-
isExportDefaultDeclaration: boolean;
29-
rets: TSESTree.ReturnStatement["argument"][];
3023
};
3124

3225
export declare namespace useComponentCollector {
@@ -71,12 +64,21 @@ export function useComponentCollector(
7164
const exp = AST.findParentNode(node, (n) => n.type === T.ExportDefaultDeclaration);
7265
const isExportDefault = exp != null;
7366
const isExportDefaultDeclaration = exp != null && AST.getUnderlyingExpression(exp.declaration) === node;
67+
const id = getFunctionComponentId(context, node);
68+
const name = id == null ? unit : AST.toStringFormat(id, getText);
69+
const initPath = AST.getFunctionInitPath(node);
7470
functionEntries.push({
71+
id: getFunctionComponentId(context, node),
7572
key,
73+
kind: "function",
74+
name,
7675
node,
76+
displayName: unit,
77+
flag: getComponentFlagFromInitPath(initPath),
78+
hint,
7779
hookCalls: [],
78-
isComponent: false,
79-
isComponentDefinition: isComponentDefinition(context, node, hint),
80+
initPath,
81+
isComponentDefinition: hasNoneOrLooseComponentName(context, node) && isComponentDefinition(context, node, hint),
8082
isExportDefault,
8183
isExportDefaultDeclaration,
8284
rets: [],
@@ -103,32 +105,12 @@ export function useComponentCollector(
103105
"ArrowFunctionExpression[body.type!='BlockStatement']"() {
104106
const entry = getCurrentEntry();
105107
if (entry == null) return;
106-
// If the function is not a component definition, skip the rest of the checks
107-
if (!entry.isComponentDefinition) return;
108108
const { body } = entry.node;
109109
if (body.type === T.BlockStatement) return;
110-
const isComponent = hasNoneOrLooseComponentName(context, entry.node)
111-
&& isJsxLike(context.sourceCode, body, hint);
112-
if (!isComponent) return;
113-
const initPath = AST.getFunctionInitPath(entry.node);
114-
const id = getFunctionComponentId(context, entry.node);
115-
const key = entry.key;
116-
const name = id == null ? unit : AST.toStringFormat(id, getText);
117-
components.set(key, {
118-
id,
119-
key,
120-
kind: "function",
121-
name,
122-
node: entry.node,
123-
displayName: unit,
124-
flag: getComponentFlagFromInitPath(initPath),
125-
hint,
126-
hookCalls: entry.hookCalls,
127-
initPath,
128-
isExportDefault: entry.isExportDefault,
129-
isExportDefaultDeclaration: entry.isExportDefaultDeclaration,
130-
rets: [body],
131-
});
110+
entry.rets.push(body);
111+
if (!entry.isComponentDefinition) return;
112+
if (!components.has(entry.key) && !isJsxLike(context.sourceCode, body, hint)) return;
113+
components.set(entry.key, entry);
132114
},
133115
...collectDisplayName
134116
? {
@@ -157,33 +139,11 @@ export function useComponentCollector(
157139
ReturnStatement(node: TSESTree.ReturnStatement) {
158140
const entry = getCurrentEntry();
159141
if (entry == null) return;
160-
// If the function is not a component definition, skip the rest of the checks
142+
entry.rets.push(node.argument);
161143
if (!entry.isComponentDefinition) return;
162144
const { argument } = node;
163-
entry.rets.push(argument);
164-
const isComponent = hasNoneOrLooseComponentName(context, entry.node)
165-
&& isJsxLike(context.sourceCode, argument, hint);
166-
if (!isComponent) return;
167-
entry.isComponent = true;
168-
const initPath = AST.getFunctionInitPath(entry.node);
169-
const id = getFunctionComponentId(context, entry.node);
170-
const key = entry.key;
171-
const name = id == null ? unit : AST.toStringFormat(id, getText);
172-
components.set(key, {
173-
id,
174-
key,
175-
kind: "function",
176-
name,
177-
node: entry.node,
178-
displayName: unit,
179-
flag: getComponentFlagFromInitPath(initPath),
180-
hint,
181-
hookCalls: entry.hookCalls,
182-
initPath,
183-
isExportDefault: entry.isExportDefault,
184-
isExportDefaultDeclaration: entry.isExportDefaultDeclaration,
185-
rets: entry.rets,
186-
});
145+
if (!components.has(entry.key) && !isJsxLike(context.sourceCode, argument, hint)) return;
146+
components.set(entry.key, entry);
187147
},
188148
} as const satisfies ESLintUtils.RuleListener;
189149
return { ctx, visitor } as const;

packages/core/src/hook/hook-collector.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const idGen = new IdGenerator("hook_");
1212
type FunctionEntry = {
1313
key: string;
1414
node: AST.TSESTreeFunction;
15-
isHook: boolean;
1615
};
1716

1817
export declare namespace useHookCollector {
@@ -39,21 +38,18 @@ export function useHookCollector(context: RuleContext): useHookCollector.ReturnT
3938
const onFunctionEnter = (node: AST.TSESTreeFunction) => {
4039
const id = AST.getFunctionId(node);
4140
const key = idGen.next();
42-
if (id != null && isHookId(id)) {
43-
functionEntries.push({ key, node, isHook: true });
44-
hooks.set(key, {
45-
id,
46-
key,
47-
kind: "function",
48-
name: AST.toStringFormat(id, getText),
49-
node,
50-
flag: 0n,
51-
hint: 0n,
52-
hookCalls: [],
53-
});
54-
return;
55-
}
56-
functionEntries.push({ key, node, isHook: false });
41+
functionEntries.push({ key, node });
42+
if (id == null || !isHookId(id)) return;
43+
hooks.set(key, {
44+
id,
45+
key,
46+
kind: "function",
47+
name: AST.toStringFormat(id, getText),
48+
node,
49+
flag: 0n,
50+
hint: 0n,
51+
hookCalls: [],
52+
});
5753
};
5854
const onFunctionExit = () => {
5955
functionEntries.pop();
@@ -70,18 +66,10 @@ export function useHookCollector(context: RuleContext): useHookCollector.ReturnT
7066
":function": onFunctionEnter,
7167
":function:exit": onFunctionExit,
7268
CallExpression(node) {
73-
if (!isHookCall(node)) {
74-
return;
75-
}
76-
const fEntry = getCurrentEntry();
77-
if (fEntry?.key == null) {
78-
return;
79-
}
80-
const hook = hooks.get(fEntry.key);
81-
if (hook == null) {
82-
return;
83-
}
84-
hook.hookCalls.push(node);
69+
if (!isHookCall(node)) return;
70+
const entry = getCurrentEntry();
71+
if (entry == null) return;
72+
hooks.get(entry.key)?.hookCalls.push(node);
8573
},
8674
} as const satisfies ESLintUtils.RuleListener;
8775
return { ctx, visitor } as const;

0 commit comments

Comments
 (0)