Skip to content

Commit 639e772

Browse files
committed
Fix detection of local components to not generate warning on for variable inside JSX files that follow React component naming (fixes #75)
1 parent d939cc4 commit 639e772

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix detection of local components to not generate warning on for variable inside JSX files that follow React component naming
6+
37
## 0.4.16
48

59
Fix CJS/ESM interop issue. Sorry everyone for the trouble.

src/only-export-components.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ const valid = [
194194
code: "const MyComponent = () => {}; export default observer(MyComponent);",
195195
options: [{ customHOCs: ["observer"] }],
196196
},
197+
{
198+
name: "Local constant with component casing and non component function",
199+
code: "const SomeConstant = 42; export function someUtility() { return SomeConstant }",
200+
},
197201
];
198202

199203
const invalid = [

src/only-export-components.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,6 @@ export const onlyExportComponents: TSESLint.RuleModule<
9999
)[] = [];
100100
const reactContextExports: TSESTree.Identifier[] = [];
101101

102-
const handleLocalIdentifier = (
103-
identifierNode: TSESTree.BindingName,
104-
) => {
105-
if (identifierNode.type !== "Identifier") return;
106-
if (reactComponentNameRE.test(identifierNode.name)) {
107-
localComponents.push(identifierNode);
108-
}
109-
};
110-
111102
const handleExportIdentifier = (
112103
identifierNode: TSESTree.BindingName | TSESTree.StringLiteral,
113104
isFunction?: boolean,
@@ -264,10 +255,18 @@ export const onlyExportComponents: TSESLint.RuleModule<
264255
}
265256
} else if (node.type === "VariableDeclaration") {
266257
for (const variable of node.declarations) {
267-
handleLocalIdentifier(variable.id);
258+
if (
259+
variable.id.type === "Identifier" &&
260+
reactComponentNameRE.test(variable.id.name) &&
261+
canBeReactFunctionComponent(variable.init)
262+
) {
263+
localComponents.push(variable.id);
264+
}
268265
}
269266
} else if (node.type === "FunctionDeclaration") {
270-
handleLocalIdentifier(node.id);
267+
if (reactComponentNameRE.test(node.id.name)) {
268+
localComponents.push(node.id);
269+
}
271270
} else if (
272271
node.type === "ImportDeclaration" &&
273272
node.source.value === "react"

0 commit comments

Comments
 (0)