ESLint
About
- ESLint is a static code analyser for Javasvcript code. It lets you find and fix problems in your JavaScript code
- ESLint statically analyzes your code to quickly find problems. It is built into most text editors and you can run ESLint as part of your continuous integration pipeline.
Links
ESLint Plugins
Depending upon the project need or whether you are using React or Angular or Node, you may use different ESLint plugins:
For TypeScript based projects:
- typescript-eslint: Tooling which enables you to use TypeScript with ESLint
For React based projects:
- eslint-plugin-react: Provides React specific linting rules for eslint
- eslint-plugin-react-hooks: This ESLint plugin enforces the Rules of Hooks.
- eslint-plugin-react-refresh: This ESLint plugin validate that your components can safely be updated with Fast Refresh.
- eslint-plugin-jsx-a11y: This plugin runs Static AST checker for accessibility rules on JSX elements.
For projects using prettier as formatting tool:
Prettier
- prettier: Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
ESLint plugins for Prettier
- eslint-config-prettier: Turns off all rules that are unnecessary or might conflict with Prettier.
- eslint-plugin-prettier: Runs Prettier as an ESLint rule and reports differences as individual ESLint issues.
Prerequisites
- Node.js (^18.18.0, ^20.9.0, or >=21.1.0) built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.)
Installation
Installation for Angular projects
@TODO
Installation for Nodejs - Expressjs projects
@TODO
Installation for Nodejs - Nestjs projects
@TODO
Installation for React projects using TypeScript
Navigate to your project directory and install npm packages required for ESLint, ESLint plugins and configurations.
- If using npm
npm install globals \
eslint @eslint/js typescript-eslint \
eslint-plugin-react \
eslint-plugin-react-hooks \
eslint-plugin-react-refresh \
eslint-plugin-jsx-a11y
prettier eslint-config-prettier eslint-plugin-prettier \
--save-dev
- Or if using yarn
yarn add globals \
eslint @eslint/js typescript-eslint \
eslint-plugin-react \
eslint-plugin-react-hooks \
eslint-plugin-react-refresh \
eslint-plugin-jsx-a11y
prettier eslint-config-prettier eslint-plugin-prettier \
--dev
warning
Remember to save these as devDependencies
in your project.
Config file for eslint.config.js
- From v8.21.0, eslint announced a new config system. In the new system, .eslintrc* is no longer used. eslint.config.js would be the default config file name. In eslint v8, the legacy system (.eslintrc*) would still be supported, while in eslint v9, only the new system would be supported.
- And from v8.23.0, eslint CLI starts to look up eslint.config.js. So, if your eslint is >=8.23.0, you're 100% ready to use the new config system.
- You might want to check out the official blog posts,
- Create and use a file named
eslint.config.js
in your python project root folder
Recommended ESLint configuration for Angular projects
@TODO
Recommended ESLint configuration for Nodejs - Expressjs projects
@TODO
Recommended ESLint configuration for Nodejs - Nestjs projects
@TODO
Recommended ESLint configuration for React projects using TypeScript
import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import pluginReact from 'eslint-plugin-react';
import pluginReactHooks from 'eslint-plugin-react-hooks';
import pluginReactRefresh from 'eslint-plugin-react-refresh';
import pluginJsxA11y from 'eslint-plugin-jsx-a11y';
import pluginEslintPrettieRecommendedConfig from 'eslint-plugin-prettier/recommended';
/** @type {import('eslint').Linter.Config[]} */
export default [
{
files: ['**/*.{js,mjs,cjs,ts,jsx,tsx}'],
},
{
languageOptions: {
globals: {
...globals.browser,
},
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
pluginReact.configs.flat.recommended,
pluginJsxA11y.flatConfigs.recommended,
pluginEslintPrettieRecommendedConfig,
{
plugins: {
'react-hooks': pluginReactHooks,
'react-refresh': pluginReactRefresh,
},
settings: {
react: {
version: 'detect',
},
},
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'react-refresh/only-export-components': 'warn',
// Prettier and Indentation Rules
'prettier/prettier': [
'error',
{
arrowParens: 'always',
endOfLine: 'lf',
semi: true,
singleQuote: true,
tabWidth: 4,
trailingComma: 'es5',
useTabs: true,
},
],
indent: ['error', 'tab', { SwitchCase: 1 }],
},
},
{
// Optional: ignore specific files or patterns
ignores: ['**/node_modules/**', 'dist/**', 'build/**'],
},
];
Recommended ESLint configuration for React Native projects
@TODO