ESLint Configuration and Usage
ESLint is a static code analysis tool for identifying problematic patterns in JavaScript and TypeScript code. This document covers how ESLint is configured and used in this project.
Overview
- Version: 8.x
- Primary plugins:
@typescript-eslint/eslint-plugin
: TypeScript-specific linting ruleseslint-plugin-react
: React-specific linting ruleseslint-plugin-react-hooks
: Rules for React Hookseslint-plugin-jsx-a11y
: Accessibility rules for JSXeslint-config-prettier
: Turns off ESLint rules that might conflict with Prettier
Configuration
The project uses the standard Next.js ESLint configuration with additional custom rules. The configuration is managed through package.json and potential future .eslintrc.js
or .eslintrc.json
files.
To customize the ESLint configuration, you can create a new file at the project root:
// .eslintrc.js
module.exports = {
extends: ['next/core-web-vitals', 'prettier'],
plugins: ['@typescript-eslint', 'react', 'react-hooks', 'jsx-a11y'],
rules: {
// Add custom rules here
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
},
};
Default Commands
The following npm scripts are available for linting:
-
Lint check: Checks for linting errors without fixing them
npm run lint
-
Lint with auto-fix: Automatically fixes linting errors where possible
npm run lint -- --fix
Integration with Git Hooks
ESLint is integrated with Git hooks through Husky and lint-staged. When you commit changes, ESLint will automatically run on the staged files.
The configuration in package.json
:
"lint-staged": {
"*.{js,jsx,ts,tsx,mjs}": [
"eslint --fix",
"prettier --write"
]
}
Editor Integration
For the best development experience, integrate ESLint with your code editor:
VS Code
- Install the ESLint extension
- Add the following configuration to your VS Code settings:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
]
}
Ignoring Files
To exclude files from linting, create a .eslintignore
file in the project root. Example:
node_modules
.next
build
dist
out
coverage
Best Practices
- Run lint checks locally: Always run
npm run lint
before pushing changes - Fix issues early: Address linting issues as they arise rather than ignoring them
- Understand the rules: Take time to understand why certain patterns are flagged as errors
- Create custom rules sparingly: Only add custom rules when absolutely necessary