Skip to content

Commit

Permalink
Add allowExportNames option (fixes #29) [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 29, 2023
1 parent 05379a0 commit 14e01d9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,8 @@
# Changelog

## Unreleased
## 0.4.4

- Add `allowExportNames` option (fixes #29)
- Support memo default export function components (fixes #27)
- Warn on export expressions that are not React component (array, object, logical expression, ...) (fixes #26)

Expand Down
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -83,6 +83,21 @@ createRoot(document.getElementById("root")).render(<App />);

## Options

### allowExportNames <small>(v0.4.4)</small>

If you use a framework that handles HMR of some specific exports, you can use this option to avoid warning for them.

Example for [Remix](https://remix.run/docs/en/main/other-api/dev#:~:text=React%20Fast%20Refresh,-can%20only%20handle):

```json
{
"react-refresh/only-export-components": [
"warn",
{ "allowExportNames": ["meta", "links", "headers", "loader", "action"] }
]
}
```

### allowConstantExport <small>(v0.4.0)</small>

Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-react-refresh",
"version": "0.4.3",
"version": "0.4.4",
"license": "MIT",
"scripts": {
"build": "scripts/bundle.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/only-export-components.test.ts
Expand Up @@ -116,6 +116,21 @@ const valid = [
code: "const foo = 'world'; export const CONSTANT = `Hello ${foo}`; export const Foo = () => {};",
options: [{ allowConstantExport: true }],
},
{
name: "Component and allowed export",
code: "export const loader = () => {}; export const Bar = () => {};",
options: [{ allowExportNames: ["loader", "meta"] }],
},
{
name: "Component and allowed function export",
code: "export function loader() {}; export const Bar = () => {};",
options: [{ allowExportNames: ["loader", "meta"] }],
},
{
name: "Only allowed exports without component",
code: "export const loader = () => {}; export const meta = { title: 'Home' };",
options: [{ allowExportNames: ["loader", "meta"] }],
},
];

const invalid = [
Expand Down Expand Up @@ -201,6 +216,12 @@ const invalid = [
filename: "Test.jsx",
errorId: "anonymousExport",
},
{
name: "Component and export non in allowExportNames",
code: "export const loader = () => {}; export const Bar = () => {}; export const foo = () => {};",
options: [{ allowExportNames: ["loader", "meta"] }],
errorId: "namedExport",
},
];

const it = (name: string, cases: Parameters<typeof ruleTester.run>[2]) => {
Expand Down
20 changes: 17 additions & 3 deletions src/only-export-components.ts
Expand Up @@ -14,7 +14,14 @@ export const onlyExportComponents: TSESLint.RuleModule<
| "anonymousExport"
| "noExport"
| "localComponents",
[] | [{ allowConstantExport?: boolean; checkJS?: boolean }]
| []
| [
{
allowConstantExport?: boolean;
checkJS?: boolean;
allowExportNames?: string[];
},
]
> = {
meta: {
messages: {
Expand All @@ -36,15 +43,19 @@ export const onlyExportComponents: TSESLint.RuleModule<
properties: {
allowConstantExport: { type: "boolean" },
checkJS: { type: "boolean" },
allowExportNames: { type: "array", items: { type: "string" } },
},
additionalProperties: false,
},
],
},
defaultOptions: [],
create: (context) => {
const { allowConstantExport = false, checkJS = false } =
context.options[0] || {};
const {
allowConstantExport = false,
checkJS = false,
allowExportNames,
} = context.options[0] || {};
const filename = context.getFilename();
// Skip tests & stories files
if (
Expand Down Expand Up @@ -86,6 +97,9 @@ export const onlyExportComponents: TSESLint.RuleModule<
nonComponentExports.push(identifierNode);
return;
}
if (allowExportNames?.includes(identifierNode.name)) {
return;
}
if (
allowConstantExport &&
init &&
Expand Down

0 comments on commit 14e01d9

Please sign in to comment.