Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
249 changes: 162 additions & 87 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { fileURLToPath } from "node:url";

// React Compiler plugin is optional; keep linting resilient if dependency is missing.
let reactCompilerPlugin;
let reactCompilerPluginLoadError = null;
try {
({ default: reactCompilerPlugin } =
await import("eslint-plugin-react-compiler"));
Expand All @@ -27,6 +28,13 @@ try {
if (!moduleNotFound) {
throw error;
}

reactCompilerPluginLoadError = error;
const loadErrorMessage =
error instanceof Error ? error.message : "Unknown module load failure";
console.warn(
`[eslint.config] Optional plugin "eslint-plugin-react-compiler" could not be loaded: ${loadErrorMessage}`
);
}

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -35,7 +43,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
// PLUGINS
// =============================================================================
// Note: jsx-a11y is already included via eslint-config-next, so we don't register it again
const plugins = {
export const basePlugins = {
"unused-imports": unusedImports,
"react-hooks": reactHooks,
"@typescript-eslint": tseslint.plugin,
Expand All @@ -46,14 +54,10 @@ const plugins = {
tailwindcss: tailwindcss,
};

if (reactCompilerPlugin) {
plugins["react-compiler"] = reactCompilerPlugin;
}

// =============================================================================
// RULES
// =============================================================================
const rules = {
export const baseRules = {
// -------------------------------------------------------------------------
// Next.js Rules - Production Grade
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -247,6 +251,42 @@ const rules = {
// Organization
"import/first": "off",
"import/no-useless-path-segments": ["off", { noUselessIndex: true }],
"import/order": [
"error",
{
groups: [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object",
"type",
],
pathGroups: [
{
pattern: "@/**",
group: "internal",
position: "before",
},
],
pathGroupsExcludedImportTypes: ["builtin"],
alphabetize: {
order: "asc",
caseInsensitive: true,
},
},
],
"sort-imports": [
"error",
{
ignoreCase: true,
ignoreDeclarationSort: true,
ignoreMemberSort: false,
allowSeparatedGroups: true,
},
],

// -------------------------------------------------------------------------
// Accessibility (jsx-a11y) - handled by eslint-config-next
Expand Down Expand Up @@ -395,93 +435,128 @@ const rules = {
"no-array-constructor": "error",
};

// Add React Compiler rule if available
if (reactCompilerPlugin) {
rules["react-compiler/react-compiler"] = "off";
}

// =============================================================================
// EXPORT CONFIG
// =============================================================================
export default defineConfig([
// Global ignores
globalIgnores([
"**/node_modules",
"**/.next",
"**/dist",
"**/out",
"**/public",
"**/coverage",
"**/generated",
"**/__tests__/**",
"**/tests/**",
"**/__mocks__/**",
"**/e2e/**",
"**/test-results/**",
"config/**",
"*.js",
"*.mjs",
"*.ts",
"*.tsx",
"scripts/**",
"stubs/**",
".claude/**",
".codex/**",
]),

// Base config with Next.js rules
{
extends: [...nextCoreWebVitals],
plugins,
rules,
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: `${__dirname}/tsconfig.json`,
const DEFAULT_GLOBAL_IGNORES = [
"**/node_modules",
"**/.next",
"**/dist",
"**/out",
"**/public",
"**/coverage",
"**/generated",
"**/__tests__/**",
"**/tests/**",
"**/__mocks__/**",
"**/e2e/**",
"config/**/*.js",
"config/**/*.mjs",
"config/**/*.ts",
"*.config.js",
"*.config.mjs",
"*.config.ts",
"scripts/**",
"stubs/**",
".claude/**",
".codex/**",
];

const TYPECHECKED_FILE_IGNORES = [
"scripts/**",
"**/next.config.*",
"config/env.ts",
"config/serverEnv.ts",
"config/alchemyEnv.ts",
"__tests__/config/env.base-endpoint.test.ts",
"**/playwright.config.ts",
"tests/**",
];

export const createEslintConfig = ({
rulesOverride = {},
extraPlugins = {},
additionalConfigs = [],
includeTestResultsIgnore = true,
reactCompilerRule = "off",
} = {}) => {
if (reactCompilerRule !== "off" && !reactCompilerPlugin) {
const loadErrorMessage =
reactCompilerPluginLoadError instanceof Error
? reactCompilerPluginLoadError.message
: "Module was not found";
throw new Error(
`Cannot apply reactCompilerRule="${reactCompilerRule}" because "eslint-plugin-react-compiler" is unavailable. Install the plugin or set reactCompilerRule to "off". Load error: ${loadErrorMessage}`
);
}

const plugins = { ...basePlugins, ...extraPlugins };
if (reactCompilerPlugin) {
plugins["react-compiler"] = reactCompilerPlugin;
}

const rules = { ...baseRules, ...rulesOverride };
if (reactCompilerPlugin) {
rules["react-compiler/react-compiler"] = reactCompilerRule;
}

const globalIgnorePatterns = includeTestResultsIgnore
? [...DEFAULT_GLOBAL_IGNORES, "**/test-results/**"]
: [...DEFAULT_GLOBAL_IGNORES];

return defineConfig([
// Global ignores
globalIgnores(globalIgnorePatterns),

// Base config with Next.js rules
{
extends: [...nextCoreWebVitals],
plugins,
rules,
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: `${__dirname}/tsconfig.json`,
},
node: true,
},
tailwindcss: {
config: `${__dirname}/tailwind.config.js`,
callees: ["classnames", "clsx", "cn", "cva"],
},
node: true,
},
tailwindcss: {
config: `${__dirname}/tailwind.config.js`,
callees: ["classnames", "clsx", "cn", "cva"],
},
},
},

// TypeScript-specific rules with type-checking
{
files: ["**/*.{ts,tsx}"],
ignores: [
"scripts/**",
"**/next.config.*",
"config/env.ts",
"config/serverEnv.ts",
"config/alchemyEnv.ts",
"__tests__/config/env.base-endpoint.test.ts",
"**/playwright.config.ts",
"tests/**",
],
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: `${__dirname}/tsconfig.json`,
tsconfigRootDir: __dirname,
},
},
rules: {
"no-restricted-syntax": [
"error",
{
selector:
"MemberExpression[object.name='process'][property.name='env']",
message:
"Accessing process.env is restricted. Use environment variables safely.",

// TypeScript-specific rules with type-checking
{
files: ["**/*.{ts,tsx}"],
ignores: TYPECHECKED_FILE_IGNORES,
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: `${__dirname}/tsconfig.json`,
tsconfigRootDir: __dirname,
},
],
},
rules: {
"no-restricted-syntax": [
"error",
{
selector:
"MemberExpression[object.name='process'][property.name='env']",
message:
"Accessing process.env is restricted. Use environment variables safely.",
},
],
},
},
},

// Prettier - MUST be last to override formatting rules
eslintConfigPrettier,
]);
...additionalConfigs,

// Prettier - MUST be last to override formatting rules
eslintConfigPrettier,
]);
};

export default createEslintConfig();
Loading