Skip to content

Instantly share code, notes, and snippets.

@ixahmedxi
Created May 15, 2024 17:24
Show Gist options
  • Save ixahmedxi/9d87c66a8245ca02a0f81b6ccb58a825 to your computer and use it in GitHub Desktop.
Save ixahmedxi/9d87c66a8245ca02a0f81b6ccb58a825 to your computer and use it in GitHub Desktop.
ESLint v9 Next.js
import path from 'path';
import { fileURLToPath } from 'url';
import comments from '@eslint-community/eslint-plugin-eslint-comments/configs';
import { fixupConfigRules } from '@eslint/compat';
import { FlatCompat } from '@eslint/eslintrc';
import js from '@eslint/js';
import eslintConfigPrettier from 'eslint-config-prettier';
import jsdoc from 'eslint-plugin-jsdoc';
import * as regexpPlugin from 'eslint-plugin-regexp';
import pluginSecurity from 'eslint-plugin-security';
import tseslint from 'typescript-eslint';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
resolvePluginsRelativeTo: __dirname,
});
export default tseslint.config(
{
ignores: ['.next'],
},
js.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
...fixupConfigRules(compat.extends('plugin:@next/next/recommended')),
...fixupConfigRules(compat.extends('plugin:react/recommended')),
...fixupConfigRules(compat.extends('plugin:react-hooks/recommended')),
...fixupConfigRules(compat.extends('plugin:tailwindcss/recommended')),
...fixupConfigRules(compat.extends('plugin:jsx-a11y/strict')),
comments.recommended,
regexpPlugin.configs['flat/recommended'],
jsdoc.configs['flat/recommended-typescript-error'],
pluginSecurity.configs.recommended,
eslintConfigPrettier,
{
linterOptions: {
reportUnusedDisableDirectives: true,
},
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
},
settings: {
react: {
version: 'detect',
},
},
rules: {
'@typescript-eslint/no-unused-vars': [
'error',
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
],
'@typescript-eslint/consistent-type-imports': [
'warn',
{ prefer: 'type-imports', fixStyle: 'separate-type-imports' },
],
'@typescript-eslint/no-misused-promises': [
'error',
{ checksVoidReturn: { attributes: false } },
],
'@typescript-eslint/no-unnecessary-condition': [
'error',
{
allowConstantLoopConditions: true,
},
],
'react/react-in-jsx-scope': 'off',
// prettier figures out the lines between tags
'jsdoc/tag-lines': 'off',
// we use some type tags in js files
'jsdoc/check-tag-names': ['error', { typed: false }],
},
},
);
/**
* Since ESLint and many plugins are written in JavaScript, we need to provide
* or change some types to make them work with TypeScript.
*/
// From: https://github.com/t3-oss/create-t3-turbo/blob/main/tooling/eslint/types.d.ts
declare module '@eslint/js' {
import type { Linter } from 'eslint';
export const configs: {
readonly recommended: { readonly rules: Readonly<Linter.RulesRecord> };
readonly all: { readonly rules: Readonly<Linter.RulesRecord> };
};
}
declare module '@eslint-community/eslint-plugin-eslint-comments/configs' {
import type { Linter } from 'eslint';
export const recommended: {
rules: Linter.RulesRecord;
};
}
declare module '@eslint/eslintrc' {
import type { Linter } from 'eslint';
export class FlatCompat {
constructor({
baseDirectory,
resolvePluginsRelativeTo,
}: {
baseDirectory: string;
resolvePluginsRelativeTo: string;
});
extends(extendsValue: string): Linter.FlatConfig & {
[Symbol.iterator]: () => IterableIterator<Linter.FlatConfig>;
};
}
}
declare module '@eslint/compat' {
import type { Linter } from 'eslint';
import type { ConfigWithExtends } from 'typescript-eslint';
export const fixupConfigRules: (
config: string | Linter.FlatConfig,
) => ConfigWithExtends[];
}
declare module 'eslint-plugin-regexp' {
import type { Linter } from 'eslint';
import type { ConfigWithExtends } from 'typescript-eslint';
export const configs: {
'flat/recommended': {
[Symbol.iterator]: () => IterableIterator<ConfigWithExtends>;
rules: Linter.RulesRecord;
};
'flat/all': {
[Symbol.iterator]: () => IterableIterator<ConfigWithExtends>;
rules: Linter.RulesRecord;
};
};
}
declare module 'eslint-plugin-security' {
import type { Linter } from 'eslint';
import type { ConfigWithExtends } from 'typescript-eslint';
export const configs: {
recommended: {
[Symbol.iterator]: () => IterableIterator<ConfigWithExtends>;
rules: Linter.RulesRecord;
};
};
}
@mkelley33
Copy link

I couldn't get this to work with NextJS 14.2.7. Do you have any recommendations?

@leerob
Copy link

leerob commented Oct 15, 2024

ESLint 9 is supported as of the Next.js 15 RC 2. v15 stable release coming soon 🙏

https://x.com/nextjs/status/1846276572812124565

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment