Skip to content

Commit 8763332

Browse files
committed
Support memo default export function components (fixes #27)
1 parent 8254172 commit 8763332

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Support memo default export function components (fixes #27)
6+
37
## 0.4.3
48

59
- Add warning for TS enums exports

src/only-export-components.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ const valid = [
8181
name: "Direct export default AF",
8282
code: "export default function foo () {};",
8383
},
84+
{
85+
name: "export default memo function",
86+
code: "export default memo(function Foo () {});",
87+
},
8488
{
8589
name: "export type *",
8690
code: "export type * from './module';",
@@ -146,6 +150,11 @@ const invalid = [
146150
code: "export default () => {};",
147151
errorId: "anonymousExport",
148152
},
153+
{
154+
name: "export default anonymous memo AF",
155+
code: "export default memo(() => {});",
156+
errorId: "anonymousExport",
157+
},
149158
{
150159
name: "Export default anonymous function",
151160
code: "export default function () {};",

src/only-export-components.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ export const onlyExportComponents: TSESLint.RuleModule<
126126
handleExportIdentifier(node.id, true);
127127
}
128128
} else if (node.type === "CallExpression") {
129-
context.report({ messageId: "anonymousExport", node });
129+
if (
130+
node.callee.type === "Identifier" &&
131+
reactHOCs.includes(node.callee.name) &&
132+
node.arguments[0]?.type === "FunctionExpression" &&
133+
node.arguments[0].id
134+
) {
135+
handleExportIdentifier(node.arguments[0].id, true);
136+
} else {
137+
context.report({ messageId: "anonymousExport", node });
138+
}
130139
} else if (node.type === "TSEnumDeclaration") {
131140
nonComponentExports.push(node.id);
132141
}
@@ -196,12 +205,13 @@ export const onlyExportComponents: TSESLint.RuleModule<
196205
},
197206
};
198207

208+
const reactHOCs = ["memo", "forwardRef"];
199209
const canBeReactFunctionComponent = (init: TSESTree.Expression | null) => {
200210
if (!init) return false;
201211
if (init.type === "ArrowFunctionExpression") return true;
202212
if (init.type === "CallExpression") {
203213
if (init.callee.type === "Identifier") {
204-
return ["memo", "forwardRef"].includes(init.callee.name);
214+
return reactHOCs.includes(init.callee.name);
205215
}
206216
}
207217
return false;

0 commit comments

Comments
 (0)