You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The purpose of lint-staged is to run linters (or other tasks) on your staged files and prevent a git commit from accidentally including changes that do not adhere to your code conventions. The simplest example of this is that you want to run ESLint or Prettier to make sure you code has been automatically formatted.
lint-staged works best in combination with other tools that do the same:
Your IDE (for example Visual Studio Code) can automatically run linters on save
lint-staged can automatically run linters on git commit
Your CI/CD system (for example GitHub Actions) can run linters for your entire feature branch
How does lint-staged task configuration work? Simply put, you configure a task that runs on staged files filtered by a glob, and lint-staged takes your command passes it the list of staged files.
The simplest configuration can be for example:
{
"*.js": "eslint --fix"
}
Where one glob *.js is configured to run one command eslint --fix. The glob will match all files that end with .js, so basically all staged JavaScript files. The command eslint --fix will invoke the ESLint cli with the the --fix flag so that all auto-fixable problems will be fixed.
With this configuration, staging files my-code.js and README.md, lint-staged will match only the JS file and run the following command:
eslint --fix my-code.js
That's it, really. In the simplest configuration, lint-staged simply appends the list of staged files matching the glob, to the command. The resulting command is invoked using execa to make sure there are no issues with cross-platform compatibility or escaping of filenames.
Lint-staged also supports more advanced configuration, like multiple globs and arrays of tasks per each command. You can also use JavaScript to dynamically create the configuration and do custom operations on the list of staged files matching the glob.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The purpose of lint-staged is to run linters (or other tasks) on your staged files and prevent a
git
commit from accidentally including changes that do not adhere to your code conventions. The simplest example of this is that you want to run ESLint or Prettier to make sure you code has been automatically formatted.lint-staged works best in combination with other tools that do the same:
How does lint-staged task configuration work? Simply put, you configure a task that runs on staged files filtered by a glob, and lint-staged takes your command passes it the list of staged files.
The simplest configuration can be for example:
Where one glob
*.js
is configured to run one commandeslint --fix
. The glob will match all files that end with.js
, so basically all staged JavaScript files. The commandeslint --fix
will invoke the ESLint cli with the the--fix
flag so that all auto-fixable problems will be fixed.With this configuration, staging files
my-code.js
andREADME.md
, lint-staged will match only the JS file and run the following command:That's it, really. In the simplest configuration, lint-staged simply appends the list of staged files matching the glob, to the command. The resulting command is invoked using
execa
to make sure there are no issues with cross-platform compatibility or escaping of filenames.Lint-staged also supports more advanced configuration, like multiple globs and arrays of tasks per each command. You can also use JavaScript to dynamically create the configuration and do custom operations on the list of staged files matching the glob.
Beta Was this translation helpful? Give feedback.
All reactions