Lint-staged is a JavaScript module designed to enhance code quality by running linters on staged Git files. With lint-staged, you can ensure that your codebase remains clean and maintainable by preventing unwanted or non-standard code from making it into your repository.
To start using lint-staged, first install the package using the following command in your terminal:
npm install --save-dev lint-staged
Once installed, you need to setup a pre-commit hook to run lint-staged. A popular choice for this setup is Husky. Install some linters, like ESLint or Prettier, then configure lint-staged to run those linters and other tasks on specific file types.
Here's an example setup in your package.json
:
{
"lint-staged": {
"*.js": "eslint",
"*.{json,md}": "prettier --write"
}
}
After this setup, when you try to commit your changes, lint-staged will run the assigned tasks on the staged files. If all tasks pass, the changes are successfully committed. If any task fails, the commit is aborted.
The best place to find the lint-staged documentation is on the project's GitHub repository at https://github.com/okonet/lint-staged
. The readme file on the repository's main page contains detailed instructions and examples for different use cases. This document is the go-to reference for understanding the initial setup, configuration options and supported commands, usage examples, and resolving common issues.