Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-react-refresh

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-react-refresh - npm Package Compare versions

Comparing version 0.4.14 to 0.4.15

index.d.ts

94

index.js

@@ -23,4 +23,3 @@ "use strict";

__export(src_exports, {
default: () => src_default,
rules: () => rules
default: () => src_default
});

@@ -30,4 +29,3 @@ module.exports = __toCommonJS(src_exports);

// src/only-export-components.ts
var possibleReactExportRE = /^[A-Z][a-zA-Z0-9]*$/u;
var strictReactExportRE = /^[A-Z][a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*$/u;
var reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
var onlyExportComponents = {

@@ -48,5 +46,6 @@ meta: {

properties: {
allowExportNames: { type: "array", items: { type: "string" } },
allowConstantExport: { type: "boolean" },
checkJS: { type: "boolean" },
allowExportNames: { type: "array", items: { type: "string" } }
customHOCs: { type: "array", items: { type: "string" } },
checkJS: { type: "boolean" }
},

@@ -60,5 +59,6 @@ additionalProperties: false

const {
allowExportNames,
allowConstantExport = false,
checkJS = false,
allowExportNames
customHOCs = [],
checkJS = false
} = context.options[0] ?? {};

@@ -73,6 +73,17 @@ const filename = context.filename;

const allowExportNamesSet = allowExportNames ? new Set(allowExportNames) : void 0;
const reactHOCs = ["memo", "forwardRef", ...customHOCs];
const canBeReactFunctionComponent = (init) => {
if (!init)
return false;
if (init.type === "ArrowFunctionExpression")
return true;
if (init.type === "CallExpression" && init.callee.type === "Identifier") {
return reactHOCs.includes(init.callee.name);
}
return false;
};
return {
Program(program) {
let hasExports = false;
let mayHaveReactExport = false;
let hasReactExport = false;
let reactIsInScope = false;

@@ -85,3 +96,3 @@ const localComponents = [];

return;
if (possibleReactExportRE.test(identifierNode.name)) {
if (reactComponentNameRE.test(identifierNode.name)) {
localComponents.push(identifierNode);

@@ -104,4 +115,4 @@ }

if (isFunction) {
if (possibleReactExportRE.test(identifierNode.name)) {
mayHaveReactExport = true;
if (reactComponentNameRE.test(identifierNode.name)) {
hasReactExport = true;
} else {

@@ -121,6 +132,5 @@ nonComponentExports.push(identifierNode);

}
if (!mayHaveReactExport && possibleReactExportRE.test(identifierNode.name)) {
mayHaveReactExport = true;
}
if (!strictReactExportRE.test(identifierNode.name)) {
if (reactComponentNameRE.test(identifierNode.name)) {
hasReactExport = true;
} else {
nonComponentExports.push(identifierNode);

@@ -148,10 +158,10 @@ }

if (node.callee.type === "CallExpression" && node.callee.callee.type === "Identifier" && node.callee.callee.name === "connect") {
mayHaveReactExport = true;
hasReactExport = true;
} else if (node.callee.type !== "Identifier") {
if (node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && reactHOCs.has(node.callee.property.name)) {
mayHaveReactExport = true;
if (node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && reactHOCs.includes(node.callee.property.name)) {
hasReactExport = true;
} else {
context.report({ messageId: "anonymousExport", node });
}
} else if (!reactHOCs.has(node.callee.name)) {
} else if (!reactHOCs.includes(node.callee.name)) {
context.report({ messageId: "anonymousExport", node });

@@ -161,3 +171,3 @@ } else if (((_a = node.arguments[0]) == null ? void 0 : _a.type) === "FunctionExpression" && node.arguments[0].id) {

} else if (((_b = node.arguments[0]) == null ? void 0 : _b.type) === "Identifier") {
mayHaveReactExport = true;
hasReactExport = true;
} else {

@@ -212,3 +222,3 @@ context.report({ messageId: "anonymousExport", node });

if (hasExports) {
if (mayHaveReactExport) {
if (hasReactExport) {
for (const node of nonComponentExports) {

@@ -234,13 +244,2 @@ context.report({ messageId: "namedExport", node });

};
var reactHOCs = /* @__PURE__ */ new Set(["memo", "forwardRef"]);
var canBeReactFunctionComponent = (init) => {
if (!init)
return false;
if (init.type === "ArrowFunctionExpression")
return true;
if (init.type === "CallExpression" && init.callee.type === "Identifier") {
return reactHOCs.has(init.callee.name);
}
return false;
};
var notReactComponentExpression = /* @__PURE__ */ new Set([

@@ -262,9 +261,24 @@ "ArrayExpression",

// src/index.ts
var rules = {
"only-export-components": onlyExportComponents
var plugin = {
rules: {
"only-export-components": onlyExportComponents
}
};
var src_default = { rules };
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rules
});
var src_default = {
rules: plugin.rules,
configs: {
recommended: {
plugins: { "react-refresh": plugin },
rules: { "react-refresh/only-export-components": "error" }
},
vite: {
plugins: { "react-refresh": plugin },
rules: {
"react-refresh/only-export-components": [
"error",
{ allowConstantExport: true }
]
}
}
}
};
{
"name": "eslint-plugin-react-refresh",
"description": "Validate that your components can safely be updated with fast refresh",
"version": "0.4.14",
"description": "Validate that your components can safely be updated with Fast Refresh",
"version": "0.4.15",
"type": "commonjs",
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",

@@ -9,2 +10,3 @@ "license": "MIT",

"main": "index.js",
"types": "index.d.ts",
"keywords": [

@@ -18,4 +20,4 @@ "eslint",

"peerDependencies": {
"eslint": ">=7"
"eslint": ">=8.40"
}
}
# eslint-plugin-react-refresh [![npm](https://img.shields.io/npm/v/eslint-plugin-react-refresh)](https://www.npmjs.com/package/eslint-plugin-react-refresh)
Validate that your components can safely be updated with fast refresh.
Validate that your components can safely be updated with Fast Refresh.
## Limitations
## Explainer
⚠️ To avoid false positive, by default this plugin is only applied on `tsx` & `jsx` files. See options to run on JS files. ⚠️
"Fast Refresh", also known as "hot reloading", is a feature in many modern bundlers.
If you update some React component(s) on disk, then the bundler will know to update only the impacted parts of your page -- without a full page reload.
The plugin rely on naming conventions (i.e. use PascalCase for components, camelCase for util functions). This is why there are some limitations:
`eslint-plugin-react-refresh` enforces that your components are structured in a way that integrations such as [react-refresh](https://www.npmjs.com/package/react-refresh) expect.
### Limitations
⚠️ To avoid false positives, by default this plugin is only applied on `tsx` & `jsx` files. See [Options](#options) to run on JS files. ⚠️
The plugin relies on naming conventions (i.e. use PascalCase for components, camelCase for util functions). This is why there are some limitations:
- `export *` are not supported and will be reported as an error

@@ -24,13 +31,19 @@ - Anonymous function are not supported (i.e `export default function() {}`)

```json
{
"plugins": ["react-refresh"],
"rules": {
"react-refresh/only-export-components": "warn"
}
}
This plugin provides a single rule, `react-refresh/only-export-components`. There are multiple ways to enable it.
### Recommended config
```js
import reactRefresh from "eslint-plugin-react-refresh";
export default [
/* Main config */
reactRefresh.configs.recommended,
];
```
### Flat config
### Vite config
This enables the `allowConstantExport` option which is supported by Vite React plugins.
```js

@@ -40,2 +53,13 @@ import reactRefresh from "eslint-plugin-react-refresh";

export default [
/* Main config */
reactRefresh.configs.vite,
];
```
### Without config
```js
import reactRefresh from "eslint-plugin-react-refresh";
export default [
{

@@ -47,3 +71,3 @@ // in main config for TSX/JSX source files

rules: {
"react-refresh/only-export-components": "warn",
"react-refresh/only-export-components": "error",
},

@@ -54,4 +78,19 @@ },

## Fail
### Legacy config
```jsonc
{
"plugins": ["react-refresh"],
"rules": {
"react-refresh/only-export-components": "error"
}
}
```
## Examples
These examples are from enabling `react-refresh/only-exports-components`.
### Fail
```jsx

@@ -81,12 +120,5 @@ export const foo = () => {};

## Pass with allowConstantExport
### Pass
```jsx
export const CONSTANT = 3;
export const Foo = () => <></>;
```
## Pass
```jsx
export default function Foo() {

@@ -109,7 +141,27 @@ return <></>;

These options are all present on `react-refresh/only-exports-components`.
```ts
interface Options {
allowExportNames?: string[];
allowConstantExport?: boolean;
customHOCs?: string[];
checkJS?: boolean;
}
const defaultOptions: Options = {
allowExportNames: [],
allowConstantExport: false,
customHOCs: [],
checkJS: false,
};
```
### allowExportNames <small>(v0.4.4)</small>
> Default: `[]`
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):
Example for [Remix](https://remix.run/docs/en/main/discussion/hot-module-replacement#supported-exports):

@@ -119,3 +171,3 @@ ```json

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

@@ -128,2 +180,4 @@ ]

> Default: `false` (`true` in `vite` config)
Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.

@@ -136,3 +190,3 @@

"react-refresh/only-export-components": [
"warn",
"error",
{ "allowConstantExport": true }

@@ -143,4 +197,13 @@ ]

Enabling this option allows code such as the following:
```jsx
export const CONSTANT = 3;
export const Foo = () => <></>;
```
### checkJS <small>(v0.3.3)</small>
> Default: `false`
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.

@@ -150,4 +213,17 @@

{
"react-refresh/only-export-components": ["warn", { "checkJS": true }]
"react-refresh/only-export-components": ["error", { "checkJS": true }]
}
```
### customHOCs <small>(v0.4.15)</small>
If you're exporting a component wrapped in a custom HOC, you can use this option to avoid false positives.
```json
{
"react-refresh/only-export-components": [
"error",
{ "customHOCs": ["observer", "withAuth"] }
]
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc