Sub topic 3
Editor Integration and Format on Save
This chapter assumes you already have a working eslint.config.js, a .prettierrc, and eslint-config-prettier installed. It also assumes you know basic Git and GitHub Actions.
Editor Integration and Format on Save
Install the ESLint and Prettier extensions for VS Code. Then add this to .vscode/settings.json so Prettier formats each file and ESLint applies its fixes every time you save:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}If you commit this file, everyone on the team gets the same behavior.
Adding Lint and Format Scripts
Your editor checks only the files you open. npm scripts check the whole project:
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"format:check": "prettier . --check"
},
"lint-staged": {
"*.{js,mjs,cjs}": ["eslint --fix", "prettier --write"],
"*.{json,md,css}": "prettier --write"
}
}Checking Staged Files with Husky and lint-staged
lint-staged runs the commands above only on staged files, so commits stay fast. Husky runs lint-staged from a Git pre-commit hook:
npm install --save-dev husky lint-staged
npx husky init
echo "npx lint-staged" > .husky/pre-commitEnforcing Lint and Format Checks in CI
name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run lint
- run: npm run format:checkeslint . exits with a non-zero code when it finds errors (warnings don't count), and --check fails when any file is unformatted. Either failure fails the build.
Linting TypeScript with typescript-eslint
Run npm install --save-dev typescript-eslint, then build your config with its helper:
// eslint.config.js
import js from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
export default tseslint.config(
js.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier,
);Writing a Custom ESLint Rule
A rule is an object with two parts. meta describes the rule, and create(context) returns visitor functions keyed by AST node type, such as CallExpression. Each visitor inspects its node and calls context.report() when the node breaks the rule. To use a rule, register it through a local plugin: plugins: { local: { rules: { "no-console-log": rule } } }. Then turn it on as "local/no-console-log": "error".
Migrating from .eslintrc to Flat Config
ESLint 9 uses flat config by default. Convert a legacy file with:
npx @eslint/migrate-config .eslintrc.jsonThe tool writes an eslint.config.mjs. Review it, delete the old .eslintrc.* file, and move the patterns from .eslintignore into an ignores entry.
Exercise
A teammate commits with --no-verify and pushes unformatted code. Which layer catches it, and which command fails?
Show answer
CI catches it. The npm run format:check step (prettier . --check) exits non-zero, so the build fails.