Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use real eslint plugin react refresh config #111

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-compiler": "19.0.0-beta-df7b47d-20241124",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "0.4.14",
"eslint-plugin-react-refresh": "0.4.16",
"eslint-plugin-regexp": "^2.7.0",
"eslint-plugin-storybook": "0.11.1",
"eslint-plugin-testing-library": "^7.0.0",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const reactConfig = async () => {
interopDefault(import("eslint-plugin-react")),
interopDefault(import("eslint-plugin-jsx-a11y")),
import("eslint-plugin-react-hooks"),
import("eslint-plugin-react-refresh"),
interopDefault(import("eslint-plugin-react-refresh")),
interopDefault(import("eslint-plugin-react-compiler")),
]);

Expand Down
3 changes: 2 additions & 1 deletion src/rules.gen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9802,9 +9802,10 @@ type ReactHooksExhaustiveDeps = []|[{
}]
// ----- react-refresh/only-export-components -----
type ReactRefreshOnlyExportComponents = []|[{
allowExportNames?: string[]
allowConstantExport?: boolean
customHOCs?: string[]
checkJS?: boolean
allowExportNames?: string[]
}]
// ----- react/boolean-prop-naming -----
type ReactBooleanPropNaming = []|[{
Expand Down
5 changes: 3 additions & 2 deletions src/rules/__snapshots__/react.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should create react rules 1`] = `
exports[`reactRules > should create react rules 1`] = `
{
"jsx-a11y/alt-text": "error",
"jsx-a11y/anchor-ambiguous-text": "off",
Expand Down Expand Up @@ -194,7 +194,8 @@ exports[`should create react rules 1`] = `
"react-refresh/only-export-components": [
"warn",
{
"allowConstantExport": true,
"allowConstantExport": undefined,
"allowExportNames": [],
},
],
"react/boolean-prop-naming": "off",
Expand Down
52 changes: 50 additions & 2 deletions src/rules/react.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
import { isPackageExists } from "local-pkg";

import { reactRules } from "./react";

test("should create react rules", async () => {
await expect(reactRules()).resolves.toMatchSnapshot();
vi.mock("local-pkg");

describe("reactRules", () => {
it("should create react rules", async () => {
await expect(reactRules()).resolves.toMatchSnapshot();
});

it("should add allowExportNames for nextjs", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "next";
});

const rules = await reactRules();

const allowExportNames =
rules["react-refresh/only-export-components"][1].allowExportNames;

expect(allowExportNames).toMatchInlineSnapshot(`
[
"dynamic",
"dynamicParams",
"revalidate",
"fetchCache",
"runtime",
"preferredRegion",
"maxDuration",
"config",
"generateStaticParams",
"metadata",
"generateMetadata",
"viewport",
"generateViewport",
]
`);
});

it("should only enable allowConstantExport for vite", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "vite";
});

const rules = await reactRules();

const allowConstantExport =
rules["react-refresh/only-export-components"][1].allowConstantExport;

expect(allowConstantExport).toBeTruthy();
});
});
24 changes: 23 additions & 1 deletion src/rules/react.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import type { Rules } from "../types";

import { hasNext, hasVite } from "../utils/has-dependency";
import { interopDefault } from "../utils/interop-default";
import { normalizeRuleEntries } from "../utils/normalize-rule-entries";

const nextAllowedExportNames = [
"dynamic",
"dynamicParams",
"revalidate",
"fetchCache",
"runtime",
"preferredRegion",
"maxDuration",
"config",
"generateStaticParams",
"metadata",
"generateMetadata",
"viewport",
"generateViewport",
];

export const reactRules = async () => {
const [reactPlugin, jsxA11yPlugin] = await Promise.all([
interopDefault(import("eslint-plugin-react")),
interopDefault(import("eslint-plugin-jsx-a11y")),
]);
const isUsingNext = hasNext();
const isUsingVite = hasVite();

return {
...jsxA11yPlugin.configs.recommended.rules,
Expand All @@ -18,7 +37,10 @@ export const reactRules = async () => {
"react-hooks/rules-of-hooks": "error",
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
{
allowConstantExport: isUsingVite,
allowExportNames: isUsingNext ? nextAllowedExportNames : [],
},
],
"react/boolean-prop-naming": "off", // revisit
"react/button-has-type": "error",
Expand Down
8 changes: 0 additions & 8 deletions src/stubs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ declare module "eslint-plugin-jsx-a11y" {
export default plugin;
}

declare module "eslint-plugin-react-refresh" {
import type { ESLint } from "eslint";

const plugin: ESLint.Plugin;

export default plugin;
}

// TODO: remove when https://github.com/eslint-community/eslint-plugin-eslint-comments/issues/214 is resolved.
declare module "@eslint-community/eslint-plugin-eslint-comments/configs" {
import type { Linter } from "eslint";
Expand Down
8 changes: 8 additions & 0 deletions src/utils/has-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ export const hasPlaywright = () => {
export const hasStorybook = () => {
return isPackageExists("storybook");
};

export const hasNext = () => {
return isPackageExists("next");
};

export const hasVite = () => {
return isPackageExists("vite");
};
Loading