Skip to content

Commit 4fc6d3d

Browse files
committed
Catch non React exports defined as call expressions [publish]
1 parent 0397bde commit 4fc6d3d

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
node-version: 22
1414
- uses: oven-sh/setup-bun@v2
1515
- run: bun install
16-
- run: bun ci
16+
- run: bun run ci

CHANGELOG.md

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

3+
## 0.4.25
4+
5+
- Report cases like `export const ENUM = Object.keys(TABLE) as EnumType[];` (fixes [#93](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/issues/93))
6+
- Allow `_` in component names ([#94](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/pull/94))
7+
38
## 0.4.24
49

510
- Add `"generateImageMetadata"`, `"generateSitemaps"` & `"generateStaticParams"` to `allowExportNames` in Next config

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-react-refresh",
3-
"version": "0.4.24",
3+
"version": "0.4.25",
44
"type": "module",
55
"license": "MIT",
66
"scripts": {

src/only-export-components.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ const invalid = [
338338
code: "const MyComponent = () => {}; export default observer(MyComponent);",
339339
errorId: ["localComponents", "anonymousExport"],
340340
},
341+
{
342+
name: "Object.keys",
343+
code: "const MyComponent = () => {}; export const ENUM = Object.keys(TABLE) as EnumType[];",
344+
errorId: "localComponents",
345+
},
341346
];
342347

343348
const it = (name: string, cases: Parameters<typeof ruleTester.run>[2]) => {

src/only-export-components.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ export const onlyExportComponents: TSESLint.RuleModule<
106106
const handleExportIdentifier = (
107107
identifierNode: TSESTree.BindingName | TSESTree.StringLiteral,
108108
isFunction?: boolean,
109-
init?: TSESTree.Expression | null,
109+
initParam?: TSESTree.Expression | null,
110110
) => {
111+
const init = initParam ? skipTSWrapper(initParam) : null;
111112
if (identifierNode.type !== "Identifier") {
112113
nonComponentExports.push(identifierNode);
113114
return;
@@ -116,7 +117,7 @@ export const onlyExportComponents: TSESLint.RuleModule<
116117
if (
117118
allowConstantExport
118119
&& init
119-
&& constantExportExpressions.has(skipTSWrapper(init).type)
120+
&& constantExportExpressions.has(init.type)
120121
) {
121122
return;
122123
}
@@ -141,6 +142,14 @@ export const onlyExportComponents: TSESLint.RuleModule<
141142
reactContextExports.push(identifierNode);
142143
return;
143144
}
145+
if (init && init.type === "CallExpression") {
146+
if (isHOCCallExpression(init)) {
147+
hasReactExport = true;
148+
} else {
149+
nonComponentExports.push(identifierNode);
150+
}
151+
return;
152+
}
144153
if (
145154
init
146155
// Switch to allowList?

0 commit comments

Comments
 (0)