eslint-plugin-react-refresh
Advanced tools
Comparing version 0.3.5 to 0.4.0
29
index.js
@@ -44,2 +44,3 @@ "use strict"; | ||
properties: { | ||
allowConstantExport: { type: "boolean" }, | ||
checkJS: { type: "boolean" } | ||
@@ -53,3 +54,3 @@ }, | ||
create: (context) => { | ||
const { checkJS } = context.options[0] || { checkJS: false }; | ||
const { allowConstantExport = false, checkJS = false } = context.options[0] || {}; | ||
const filename = context.getFilename(); | ||
@@ -68,3 +69,3 @@ if (filename.includes(".test.") || filename.includes(".spec.") || filename.includes(".stories.")) { | ||
const localComponents = []; | ||
const nonComponentExport = []; | ||
const nonComponentExports = []; | ||
const handleLocalIdentifier = (identifierNode) => { | ||
@@ -77,5 +78,5 @@ if (identifierNode.type !== "Identifier") | ||
}; | ||
const handleExportIdentifier = (identifierNode) => { | ||
const handleExportIdentifier = (identifierNode, isFunction, init) => { | ||
if (identifierNode.type !== "Identifier") { | ||
nonComponentExport.push(identifierNode); | ||
nonComponentExports.push(identifierNode); | ||
return; | ||
@@ -86,10 +87,20 @@ } | ||
} | ||
if (!strictReactExportRE.test(identifierNode.name)) { | ||
nonComponentExport.push(identifierNode); | ||
if (allowConstantExport && init && (init.type === "Literal" || init.type === "TemplateLiteral" || init.type === "BinaryExpression")) { | ||
return; | ||
} | ||
if (!(isFunction ? possibleReactExportRE : strictReactExportRE).test( | ||
identifierNode.name | ||
)) { | ||
nonComponentExports.push(identifierNode); | ||
} | ||
}; | ||
const handleExportDeclaration = (node) => { | ||
var _a; | ||
if (node.type === "VariableDeclaration") { | ||
for (const variable of node.declarations) { | ||
handleExportIdentifier(variable.id); | ||
handleExportIdentifier( | ||
variable.id, | ||
((_a = variable.init) == null ? void 0 : _a.type) === "ArrowFunctionExpression", | ||
variable.init | ||
); | ||
} | ||
@@ -100,3 +111,3 @@ } else if (node.type === "FunctionDeclaration") { | ||
} else { | ||
handleExportIdentifier(node.id); | ||
handleExportIdentifier(node.id, true); | ||
} | ||
@@ -145,3 +156,3 @@ } else if (node.type === "CallExpression") { | ||
if (mayHaveReactExport) { | ||
for (const node of nonComponentExport) { | ||
for (const node of nonComponentExports) { | ||
context.report({ messageId: "namedExport", node }); | ||
@@ -148,0 +159,0 @@ } |
{ | ||
"name": "eslint-plugin-react-refresh", | ||
"description": "Validate that your components can safely be updated with fast refresh", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"author": "Arnaud Barré (https://github.com/ArnaudBarre)", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -14,11 +14,4 @@ # eslint-plugin-react-refresh [![npm](https://img.shields.io/npm/v/eslint-plugin-react-refresh)](https://www.npmjs.com/package/eslint-plugin-react-refresh) | ||
- Class components are not supported | ||
- Full uppercase export would be considered as an error. It can be disabled locally when it's effectively a React component: | ||
- All-uppercase function export is considered an error when not using direct named export (ex `const CMS = () => <></>; export { CMS }`) | ||
```jsx | ||
// eslint-disable-next-line react-refresh/only-export-components | ||
export const CMS = () => <></>; | ||
``` | ||
I may publish a rule base on type information from [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) to improve some limitations and catch some naming convention issues (like non-component function starting with an uppercase). | ||
## Installation | ||
@@ -49,7 +42,2 @@ | ||
```jsx | ||
export const CONSTANT = 3; | ||
export const Foo = () => <></>; | ||
``` | ||
```jsx | ||
export default function () {} | ||
@@ -73,2 +61,9 @@ export default compose()(MainComponent) | ||
## Pass with allowConstantExport | ||
```jsx | ||
export const CONSTANT = 3; | ||
export const Foo = () => <></>; | ||
``` | ||
## Pass | ||
@@ -94,4 +89,19 @@ | ||
### checkJS | ||
### allowConstantExport <small>(v0.4.0)</small> | ||
Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components. | ||
This should be enabled if the fast refresh implementation correctly handles this case (HMR when the constant doesn't change, propagate update to importers when the constant changes.). Vite supports it, PR welcome if you notice other integrations works well. | ||
```json | ||
{ | ||
"react-refresh/only-export-components": [ | ||
"warn", | ||
{ "allowConstantExport": true } | ||
] | ||
} | ||
``` | ||
### checkJS <small>(v0.3.3)</small> | ||
If your using JSX inside `.js` files (which I don't recommend because it forces you to configure every tool you use to switch the parser), you can still use the plugin by enabling this option. To reduce the number of false positive, only files importing `react` are checked. | ||
@@ -98,0 +108,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11161
172
110