Skip to content

Commit

Permalink
Fix detection of local components to not generate warning on for vari…
Browse files Browse the repository at this point in the history
…able inside JSX files that follow React component naming (fixes #75)
  • Loading branch information
ArnaudBarre committed Jan 11, 2025
1 parent d939cc4 commit 639e772
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 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

- Fix detection of local components to not generate warning on for variable inside JSX files that follow React component naming

## 0.4.16

Fix CJS/ESM interop issue. Sorry everyone for the trouble.
Expand Down
4 changes: 4 additions & 0 deletions src/only-export-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ const valid = [
code: "const MyComponent = () => {}; export default observer(MyComponent);",
options: [{ customHOCs: ["observer"] }],
},
{
name: "Local constant with component casing and non component function",
code: "const SomeConstant = 42; export function someUtility() { return SomeConstant }",
},
];

const invalid = [
Expand Down
21 changes: 10 additions & 11 deletions src/only-export-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,6 @@ export const onlyExportComponents: TSESLint.RuleModule<
)[] = [];
const reactContextExports: TSESTree.Identifier[] = [];

const handleLocalIdentifier = (
identifierNode: TSESTree.BindingName,
) => {
if (identifierNode.type !== "Identifier") return;
if (reactComponentNameRE.test(identifierNode.name)) {
localComponents.push(identifierNode);
}
};

const handleExportIdentifier = (
identifierNode: TSESTree.BindingName | TSESTree.StringLiteral,
isFunction?: boolean,
Expand Down Expand Up @@ -264,10 +255,18 @@ export const onlyExportComponents: TSESLint.RuleModule<
}
} else if (node.type === "VariableDeclaration") {
for (const variable of node.declarations) {
handleLocalIdentifier(variable.id);
if (
variable.id.type === "Identifier" &&
reactComponentNameRE.test(variable.id.name) &&
canBeReactFunctionComponent(variable.init)
) {
localComponents.push(variable.id);
}
}
} else if (node.type === "FunctionDeclaration") {
handleLocalIdentifier(node.id);
if (reactComponentNameRE.test(node.id.name)) {
localComponents.push(node.id);
}
} else if (
node.type === "ImportDeclaration" &&
node.source.value === "react"
Expand Down

0 comments on commit 639e772

Please sign in to comment.