Skip to content

Commit

Permalink
add nextjs optional plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianwd committed Nov 7, 2024
1 parent bb8117a commit 3840267
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 45 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@eslint/compat": "^1.2.2",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.14.0",
"@next/eslint-plugin-next": "^15.0.2",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsx-a11y": "^6.10.2",
"eslint-plugin-prettier": "^5.2.1",
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

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

94 changes: 52 additions & 42 deletions src/flat-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,66 @@ import type { FlatConfig } from "@typescript-eslint/utils/ts-eslint";
import pluginReact from "eslint-plugin-react";
import jsxA11y from "eslint-plugin-jsx-a11y";
import tseslint from "typescript-eslint";
import nextPlugin from "@next/eslint-plugin-next";
import pluginTailwindcss from "eslint-plugin-tailwindcss";
import { FlatCompat } from "@eslint/eslintrc";
// @ts-expect-error - No types
import reactHooks from "eslint-plugin-react-hooks";

const compat = new FlatCompat();

export const react = tseslint.config(
{
files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"],
extends: [
// @ts-expect-error - Types are broken in eslint-plugin-react https://github.com/jsx-eslint/eslint-plugin-react/issues/3838
pluginReact.configs.flat.recommended,
// @ts-expect-error - See above
pluginReact.configs.flat["jsx-runtime"],
...compat.config(reactHooks.configs.recommended),
...pluginTailwindcss.configs["flat/recommended"],
],
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
export interface ReactPluginOptions {
framework?: "next";
}

console.log(nextPlugin.configs["core-web-vitals"]);

const eslintNext = compat.config(nextPlugin.configs["core-web-vitals"]);

export const react = ({ framework }: ReactPluginOptions) =>
tseslint.config(
{
files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"],
extends: [
// @ts-expect-error - Types are broken in eslint-plugin-react https://github.com/jsx-eslint/eslint-plugin-react/issues/3838
pluginReact.configs.flat.recommended,
// @ts-expect-error - See above
pluginReact.configs.flat["jsx-runtime"],
...compat.config(reactHooks.configs.recommended),
...pluginTailwindcss.configs["flat/recommended"],
...(framework === "next" ? eslintNext : []),
],
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
},
settings: {
react: {
version: "detect",
settings: {
react: {
version: "detect",
},
},
rules: {
"react/prop-types": "off",
"react/display-name": "warn",
"react/button-has-type": "warn",
"tailwindcss/no-custom-classname": [
"warn",
{
callees: ["classnames", "clsx", "cva", "twMerge", "cn"],
ignoredKeys: ["color", "variant", "size", "defaultVariants"],
},
],
},
},
rules: {
"react/prop-types": "off",
"react/display-name": "warn",
"react/button-has-type": "warn",
"tailwindcss/no-custom-classname": [
"warn",
{
callees: ["classnames", "clsx", "cva", "twMerge", "cn"],
ignoredKeys: ["color", "variant", "size", "defaultVariants"],
},
{
files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"],
ignores: [
"**/*.stories.@(ts|tsx|js|jsx|mjs|cjs)",
"**/*.test.@(ts|tsx|js|jsx|mjs|cjs)",
"**/*.spec.@(ts|tsx|js|jsx|mjs|cjs)",
],
},
},
{
files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"],
ignores: [
"**/*.stories.@(ts|tsx|js|jsx|mjs|cjs)",
"**/*.test.@(ts|tsx|js|jsx|mjs|cjs)",
"**/*.spec.@(ts|tsx|js|jsx|mjs|cjs)",
],
extends: [jsxA11y.flatConfigs.recommended],
}
) satisfies FlatConfig.ConfigArray;
extends: [jsxA11y.flatConfigs.recommended],
}
) satisfies FlatConfig.ConfigArray;
7 changes: 7 additions & 0 deletions src/types/eslint-plugin-next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module "@next/eslint-plugin-next" {
export const configs: {
recommended: { rules: import("eslint").Linter.RulesRecord };
"core-web-vitals": { rules: import("eslint").Linter.RulesRecord };
};
export const rules: Record<string, import("eslint").Rule.RuleModule>;
}
11 changes: 11 additions & 0 deletions src/types/eslint-plugin-react-hooks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare module "eslint-plugin-react-hooks" {
export const configs: {
recommended: {
rules: {
"rules-of-hooks": import("eslint").Linter.RuleEntry;
"exhaustive-deps": import("eslint").Linter.RuleEntry;
};
};
};
export const rules: Record<string, import("eslint").Rule.RuleModule>;
}
4 changes: 2 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"declaration": true,
"composite": true,
"noEmit": true,
/* If your code doesn't run in the DOM: */
"lib": [
"es2022"
"es2022",
"DOM"
],
},
"include": [
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"extends": "./tsconfig.build.json",
"compilerOptions": {
"composite": false,
"rootDir": "."
"rootDir": ".",
"typeRoots": [
"./src/types",
"./node_modules/@types"
]
}
}

0 comments on commit 3840267

Please sign in to comment.