Skip to content

Commit

Permalink
fix: πŸ› set allowExportNames for react-refresh & nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman committed Dec 3, 2024
1 parent 020f0c1 commit 957e5af
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
3 changes: 2 additions & 1 deletion 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 @@ -195,6 +195,7 @@ exports[`should create react rules 1`] = `
"warn",
{
"allowConstantExport": true,
"allowExportNames": [],
},
],
"react/boolean-prop-naming": "off",
Expand Down
39 changes: 37 additions & 2 deletions src/rules/react.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
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",
]
`);
});
});
23 changes: 22 additions & 1 deletion src/rules/react.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import type { Rules } from "../types";

import { hasNext } 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();

return {
...jsxA11yPlugin.configs.recommended.rules,
Expand All @@ -18,7 +36,10 @@ export const reactRules = async () => {
"react-hooks/rules-of-hooks": "error",
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
{
allowConstantExport: true,
allowExportNames: isUsingNext ? nextAllowedExportNames : [],
},
],
"react/boolean-prop-naming": "off", // revisit
"react/button-has-type": "error",
Expand Down
4 changes: 4 additions & 0 deletions src/utils/has-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ export const hasPlaywright = () => {
export const hasStorybook = () => {
return isPackageExists("storybook");
};

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

0 comments on commit 957e5af

Please sign in to comment.