Skip to content

Commit

Permalink
Support memo default export function components (fixes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 24, 2023
1 parent 8254172 commit 8763332
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Support memo default export function components (fixes #27)

## 0.4.3

- Add warning for TS enums exports
Expand Down
9 changes: 9 additions & 0 deletions src/only-export-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ const valid = [
name: "Direct export default AF",
code: "export default function foo () {};",
},
{
name: "export default memo function",
code: "export default memo(function Foo () {});",
},
{
name: "export type *",
code: "export type * from './module';",
Expand Down Expand Up @@ -146,6 +150,11 @@ const invalid = [
code: "export default () => {};",
errorId: "anonymousExport",
},
{
name: "export default anonymous memo AF",
code: "export default memo(() => {});",
errorId: "anonymousExport",
},
{
name: "Export default anonymous function",
code: "export default function () {};",
Expand Down
14 changes: 12 additions & 2 deletions src/only-export-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,16 @@ export const onlyExportComponents: TSESLint.RuleModule<
handleExportIdentifier(node.id, true);
}
} else if (node.type === "CallExpression") {
context.report({ messageId: "anonymousExport", node });
if (
node.callee.type === "Identifier" &&
reactHOCs.includes(node.callee.name) &&
node.arguments[0]?.type === "FunctionExpression" &&
node.arguments[0].id
) {
handleExportIdentifier(node.arguments[0].id, true);
} else {
context.report({ messageId: "anonymousExport", node });
}
} else if (node.type === "TSEnumDeclaration") {
nonComponentExports.push(node.id);
}
Expand Down Expand Up @@ -196,12 +205,13 @@ export const onlyExportComponents: TSESLint.RuleModule<
},
};

const reactHOCs = ["memo", "forwardRef"];
const canBeReactFunctionComponent = (init: TSESTree.Expression | null) => {
if (!init) return false;
if (init.type === "ArrowFunctionExpression") return true;
if (init.type === "CallExpression") {
if (init.callee.type === "Identifier") {
return ["memo", "forwardRef"].includes(init.callee.name);
return reactHOCs.includes(init.callee.name);
}
}
return false;
Expand Down

0 comments on commit 8763332

Please sign in to comment.