Skip to content

Popular eslint configurations

Course: ESLint, Prettier and Pre-commit hooks - chapter 6

You can roll your own config, like we did in the last slide

module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}

Or you can use a popular one created by other people, for example

  1. https://github.com/airbnb/javascript

  2. https://github.com/dustinspecker/awesome-eslint#configs

  • Add dependencies
yarn add eslint-config-airbnb eslint-config-airbnb-typescript eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-import @typescript-eslint/eslint-plugin
  • Update .eslintrc.cjs
module.exports = {
root: true,
env: { browser: true, es2020: true },
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json", "./tsconfig.node.json"],
tsconfigRootDir: __dirname,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"airbnb",
"airbnb/hooks",
],
ignorePatterns: ["dist", ".eslintrc.cjs", "vite.config.ts"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
"react/jsx-filename-extension": [
1,
{ extensions: [".js", ".jsx", ".ts", ".tsx"] },
],
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};

💡 If you try doing the same in the new config system (for eg, the node.js project), you’ll face some issues.

New config system - ‣

Ref - https://stackoverflow.com/questions/74925642/how-to-use-eslint-config-airbnb-with-the-new-eslint-config-spec

  • Update .eslintrc.cjs (read README.md)
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
  • Try running the lint command
yarn lint

  • Try fixing them automatically
npm run lint -- --fix
  • Fix the rest of the errors manually
  • Update .eslintrc.cjs to ignore vite.config.js
ignorePatterns: ['dist', '.eslintrc.cjs', 'vite.config.ts'],