Skip to content

Commit

Permalink
feat: add configuration for Next.js (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfeltscher authored Dec 6, 2024
1 parent 2eb1d23 commit 9adebc0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default configs.typescript;

// .. or React applications
export default configs.react;

// .. or Next.js applications
export default configs.next;
```

### Legacy Config (`.eslintrc`)
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"homepage": "https://github.com/smartive/eslint-config#readme",
"dependencies": {
"@eslint/eslintrc": "^3.2.0",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -50,17 +51,20 @@
"typescript-eslint": "^8.15.0"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0"
"eslint": "^8.57.0 || ^9.0.0",
"eslint-config-next": "^14.0.0 || ^15.0.0"
},
"devDependencies": {
"@commitlint/cli": "^19.6.0",
"@commitlint/config-conventional": "^19.6.0",
"@eslint/js": "^9.15.0",
"@smartive/prettier-config": "^3.0.0",
"@types/eslint__eslintrc": "^2.1.2",
"@types/node": "^22.9.1",
"cz-conventional-changelog": "^3.3.0",
"esbuild": "0.24.0",
"eslint": "^9.15.0",
"eslint-config-next": "^15.0.4",
"husky": "^9.1.7",
"prettier": "^3.3.3",
"typescript": "^5.6.3"
Expand Down
33 changes: 26 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FlatCompat } from '@eslint/eslintrc';
import js from '@eslint/js';
import type { Linter } from 'eslint';
import { flatConfigs as eslintPluginImportConfigs } from 'eslint-plugin-import';
Expand Down Expand Up @@ -86,16 +87,34 @@ const flatConfigTypescript = tsEslint.config(
},
);

const flatConfigReact = tsEslint.config(
flatConfigTypescript,
reactPlugin.configs.flat!.recommended as unknown as Linter.Config,
reactPlugin.configs.flat!['jsx-runtime'] as unknown as Linter.Config,
{ rules: reactRules },
);
// This is just a workaround until Next.js decides to provide a flat config
// the same way the rest of the world does..
// Nid hässig, nur entüüscht.
const flatConfigNext = () =>
new FlatCompat().extends('next').map((config) => {
const { plugins } = config;
if (plugins?.import) {
// ugly workaround to fix an issue when reusing the import plugin
// see https://github.com/eslint/eslintrc/issues/135
plugins.import = eslintPluginImportConfigs.errors.plugins!.import;
}

return config;
});

const flatConfigReact = (includeNextConfig = false) =>
tsEslint.config(
flatConfigTypescript,
reactPlugin.configs.flat!.recommended as unknown as Linter.Config,
reactPlugin.configs.flat!['jsx-runtime'] as unknown as Linter.Config,
...(includeNextConfig ? flatConfigNext() : []),
{ rules: reactRules },
);

export const configs = {
typescript: flatConfigTypescript,
react: flatConfigReact,
react: flatConfigReact(),
next: flatConfigReact(true),
};

export const generateLegacyConfig = (react: boolean): Linter.LegacyConfig => ({
Expand Down
6 changes: 6 additions & 0 deletions src/typings/eslint-config-next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Linter } from 'eslint';

declare module 'eslint-config-next' {
const plugin: Linter.LegacyConfig;
export = plugin;
}

0 comments on commit 9adebc0

Please sign in to comment.