UNPKG

35.4 kBMarkdownView Raw
1# 🚫💩 lint-staged ![GitHub Actions](https://github.com/okonet/lint-staged/workflows/CI/badge.svg) [![npm version](https://badge.fury.io/js/lint-staged.svg)](https://badge.fury.io/js/lint-staged) [![Codecov](https://codecov.io/gh/okonet/lint-staged/branch/master/graph/badge.svg)](https://codecov.io/gh/okonet/lint-staged)
2
3Run linters against staged git files and don't let :poop: slip into your code base!
4
5```bash
6npm install --save-dev lint-staged # requires further setup
7```
8
9```
10$ git commit
11
12✔ Preparing lint-staged...
13❯ Running tasks for staged files...
14 ❯ packages/frontend/.lintstagedrc.json — 1 file
15 ↓ *.js — no files [SKIPPED]
16 ❯ *.{json,md} — 1 file
17 ⠹ prettier --write
18 ↓ packages/backend/.lintstagedrc.json — 2 files
19 ❯ *.js — 2 files
20 ⠼ eslint --fix
21 ↓ *.{json,md} — no files [SKIPPED]
22◼ Applying modifications from tasks...
23◼ Cleaning up temporary files...
24```
25
26<details>
27<summary>See asciinema video</summary>
28
29[![asciicast](https://asciinema.org/a/199934.svg)](https://asciinema.org/a/199934)
30
31</details>
32
33## Why
34
35Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow, and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
36
37This project contains a script that will run arbitrary shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
38
39## Related blog posts and talks
40
41- [Introductory Medium post - Andrey Okonetchnikov, 2016](https://medium.com/@okonetchnikov/make-linting-great-again-f3890e1ad6b8#.8qepn2b5l)
42- [Running Jest Tests Before Each Git Commit - Ben McCormick, 2017](https://benmccormick.org/2017/02/26/running-jest-tests-before-each-git-commit/)
43- [AgentConf presentation - Andrey Okonetchnikov, 2018](https://www.youtube.com/watch?v=-mhY7e-EsC4)
44- [SurviveJS interview - Juho Vepsäläinen and Andrey Okonetchnikov, 2018](https://survivejs.com/blog/lint-staged-interview/)
45- [Prettier your CSharp with `dotnet-format` and `lint-staged`](https://johnnyreilly.com/2020/12/22/prettier-your-csharp-with-dotnet-format-and-lint-staged)
46
47> If you've written one, please submit a PR with the link to it!
48
49## Installation and setup
50
51To install _lint-staged_ in the recommended way, you need to:
52
531. Install _lint-staged_ itself:
54 - `npm install --save-dev lint-staged`
551. Set up the `pre-commit` git hook to run _lint-staged_
56 - [Husky](https://github.com/typicode/husky) is a popular choice for configuring git hooks
57 - Read more about git hooks [here](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
581. Install some linters, like [ESLint](https://eslint.org) or [Prettier](https://prettier.io)
591. Configure _lint-staged_ to run linters and other tasks:
60 - for example: `{ "*.js": "eslint" }` to run ESLint for all staged JS files
61 - See [Configuration](#Configuration) for more info
62
63Don't forget to commit changes to `package.json` and `.husky` to share this setup with your team!
64
65Now change a few files, `git add` or `git add --patch` some of them to your commit, and try to `git commit` them.
66
67See [examples](#examples) and [configuration](#configuration) for more information.
68
69## Changelog
70
71See [Releases](https://github.com/okonet/lint-staged/releases).
72
73### Migration
74
75#### v13
76
77- Since `v13.0.0` _lint-staged_ no longer supports Node.js 12. Please upgrade your Node.js version to at least `14.13.1`, or `16.0.0` onward.
78
79#### v12
80
81- Since `v12.0.0` _lint-staged_ is a pure ESM module, so make sure your Node.js version is at least `12.20.0`, `14.13.1`, or `16.0.0`. Read more about ESM modules from the official [Node.js Documentation site here](https://nodejs.org/api/esm.html#introduction).
82
83#### v10
84
85- From `v10.0.0` onwards any new modifications to originally staged files will be automatically added to the commit.
86 If your task previously contained a `git add` step, please remove this.
87 The automatic behaviour ensures there are less race-conditions,
88 since trying to run multiple git operations at the same time usually results in an error.
89- From `v10.0.0` onwards, lint-staged uses git stashes to improve speed and provide backups while running.
90 Since git stashes require at least an initial commit, you shouldn't run lint-staged in an empty repo.
91- From `v10.0.0` onwards, lint-staged requires Node.js version 10.13.0 or later.
92- From `v10.0.0` onwards, lint-staged will abort the commit if linter tasks undo all staged changes. To allow creating an empty commit, please use the `--allow-empty` option.
93
94## Command line flags
95
96```
97❯ npx lint-staged --help
98Usage: lint-staged [options]
99
100Options:
101 -V, --version output the version number
102 --allow-empty allow empty commits when tasks revert all staged changes (default: false)
103 -p, --concurrent <number|boolean> the number of tasks to run concurrently, or false for serial (default: true)
104 -c, --config [path] path to configuration file, or - to read from stdin
105 --cwd [path] run all tasks in specific directory, instead of the current
106 -d, --debug print additional debug information (default: false)
107 --diff [string] override the default "--staged" flag of "git diff" to get list of files. Implies
108 "--no-stash".
109 --diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of files
110 --max-arg-length [number] maximum length of the command-line argument string (default: 0)
111 --no-stash disable the backup stash, and do not revert in case of errors
112 -q, --quiet disable lint-staged’s own console output (default: false)
113 -r, --relative pass relative filepaths to tasks (default: false)
114 -x, --shell [path] skip parsing of tasks for better shell support (default: false)
115 -v, --verbose show task output even when tasks succeed; by default only failed output is shown
116 (default: false)
117 -h, --help display help for command
118```
119
120- **`--allow-empty`**: By default, when linter tasks undo all staged changes, lint-staged will exit with an error and abort the commit. Use this flag to allow creating empty git commits.
121- **`--concurrent [number|boolean]`**: Controls the [concurrency of tasks](#task-concurrency) being run by lint-staged. **NOTE**: This does NOT affect the concurrency of subtasks (they will always be run sequentially). Possible values are:
122 - `false`: Run all tasks serially
123 - `true` (default) : _Infinite_ concurrency. Runs as many tasks in parallel as possible.
124 - `{number}`: Run the specified number of tasks in parallel, where `1` is equivalent to `false`.
125- **`--config [path]`**: Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and will print an error if the specified file cannot be found. If '-' is provided as the filename then the config will be read from stdin, allowing piping in the config like `cat my-config.json | npx lint-staged --config -`.
126- **`--cwd [path]`**: By default tasks run in the current working directory. Use the `--cwd some/directory` to override this. The path can be absolute or relative to the current working directory.
127- **`--debug`**: Run in debug mode. When set, it does the following:
128 - uses [debug](https://github.com/visionmedia/debug) internally to log additional information about staged files, commands being executed, location of binaries, etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
129 - uses [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`; this causes serial, uncoloured output to the terminal, instead of the default (beautified, dynamic) output.
130- **`--diff`**: By default linters are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`.
131- **`--diff-filter`**: By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203).
132- **`--max-arg-length`**: long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
133- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash`
134- **`--quiet`**: Supress all CLI output, except from tasks.
135- **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
136- **`--shell`**: By default linter commands will be parsed for speed and security. This has the side-effect that regular shell scripts might not work as expected. You can skip parsing of commands with this option. To use a specific shell, use a path like `--shell "/bin/bash"`.
137- **`--verbose`**: Show task output even when tasks succeed. By default only failed output is shown.
138
139## Configuration
140
141_Lint-staged_ can be configured in many ways:
142
143- `lint-staged` object in your `package.json`
144- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
145 - `.lintstagedrc.json`
146 - `.lintstagedrc.yaml`
147 - `.lintstagedrc.yml`
148- `.lintstagedrc.mjs` or `lint-staged.config.mjs` file in ESM format
149 - the default export value should be a configuration: `export default { ... }`
150- `.lintstagedrc.cjs` or `lint-staged.config.cjs` file in CommonJS format
151 - the exports value should be a configuration: `module.exports = { ... }`
152- `lint-staged.config.js` or `.lintstagedrc.js` in either ESM or CommonJS format, depending on
153 whether your project's _package.json_ contains the `"type": "module"` option or not.
154- Pass a configuration file using the `--config` or `-c` flag
155
156Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns. JavaScript files can also export advanced configuration as a function. See [Using JS configuration files](#using-js-configuration-files) for more info.
157
158You can also place multiple configuration files in different directories inside a project. For a given staged file, the closest configuration file will always be used. See ["How to use `lint-staged` in a multi-package monorepo?"](#how-to-use-lint-staged-in-a-multi-package-monorepo) for more info and an example.
159
160#### `package.json` example:
161
162```json
163{
164 "lint-staged": {
165 "*": "your-cmd"
166 }
167}
168```
169
170#### `.lintstagedrc` example
171
172```json
173{
174 "*": "your-cmd"
175}
176```
177
178This config will execute `your-cmd` with the list of currently staged files passed as arguments.
179
180So, considering you did `git add file1.ext file2.ext`, lint-staged will run the following command:
181
182`your-cmd file1.ext file2.ext`
183
184### Task concurrency
185
186By default _lint-staged_ will run configured tasks concurrently. This means that for every glob, all the commands will be started at the same time. With the following config, both `eslint` and `prettier` will run at the same time:
187
188```json
189{
190 "*.ts": "eslint",
191 "*.md": "prettier --list-different"
192}
193```
194
195This is typically not a problem since the globs do not overlap, and the commands do not make changes to the files, but only report possible errors (aborting the git commit). If you want to run multiple commands for the same set of files, you can use the array syntax to make sure commands are run in order. In the following example, `prettier` will run for both globs, and in addition `eslint` will run for `*.ts` files _after_ it. Both sets of commands (for each glob) are still started at the same time (but do not overlap).
196
197```json
198{
199 "*.ts": ["prettier --list-different", "eslint"],
200 "*.md": "prettier --list-different"
201}
202```
203
204Pay extra attention when the configured globs overlap, and tasks make edits to files. For example, in this configuration `prettier` and `eslint` might try to make changes to the same `*.ts` file at the same time, causing a _race condition_:
205
206```json
207{
208 "*": "prettier --write",
209 "*.ts": "eslint --fix"
210}
211```
212
213If necessary, you can limit the concurrency using `--concurrent <number>` or disable it entirely with `--concurrent false`.
214
215## Filtering files
216
217Linter commands work on a subset of all staged files, defined by a _glob pattern_. lint-staged uses [micromatch](https://github.com/micromatch/micromatch) for matching files with the following rules:
218
219- If the glob pattern contains no slashes (`/`), micromatch's `matchBase` option will enabled, so globs match a file's basename regardless of directory:
220 - **`"*.js"`** will match all JS files, like `/test.js` and `/foo/bar/test.js`
221 - **`"!(*test).js"`**. will match all JS files, except those ending in `test.js`, so `foo.js` but not `foo.test.js`
222- If the glob pattern does contain a slash (`/`), it will match for paths as well:
223 - **`"./*.js"`** will match all JS files in the git repo root, so `/test.js` but not `/foo/bar/test.js`
224 - **`"foo/**/*.js"`** will match all JS files inside the `/foo` directory, so `/foo/bar/test.js` but not `/test.js`
225
226When matching, lint-staged will do the following
227
228- Resolve the git root automatically, no configuration needed.
229- Pick the staged files which are present inside the project directory.
230- Filter them using the specified glob patterns.
231- Pass absolute paths to the linters as arguments.
232
233**NOTE:** `lint-staged` will pass _absolute_ paths to the linters to avoid any confusion in case they're executed in a different working directory (i.e. when your `.git` directory isn't the same as your `package.json` directory).
234
235Also see [How to use `lint-staged` in a multi-package monorepo?](#how-to-use-lint-staged-in-a-multi-package-monorepo)
236
237### Ignoring files
238
239The concept of `lint-staged` is to run configured linter tasks (or other tasks) on files that are staged in git. `lint-staged` will always pass a list of all staged files to the task, and ignoring any files should be configured in the task itself.
240
241Consider a project that uses [`prettier`](https://prettier.io/) to keep code format consistent across all files. The project also stores minified 3rd-party vendor libraries in the `vendor/` directory. To keep `prettier` from throwing errors on these files, the vendor directory should be added to prettier's ignore configuration, the `.prettierignore` file. Running `npx prettier .` will ignore the entire vendor directory, throwing no errors. When `lint-staged` is added to the project and configured to run prettier, all modified and staged files in the vendor directory will be ignored by prettier, even though it receives them as input.
242
243In advanced scenarios, where it is impossible to configure the linter task itself to ignore files, but some staged files should still be ignored by `lint-staged`, it is possible to filter filepaths before passing them to tasks by using the function syntax. See [Example: Ignore files from match](#example-ignore-files-from-match).
244
245## What commands are supported?
246
247Supported are any executables installed locally or globally via `npm` as well as any executable from your \$PATH.
248
249> Using globally installed scripts is discouraged, since lint-staged may not work for someone who doesn't have it installed.
250
251`lint-staged` uses [execa](https://github.com/sindresorhus/execa#preferlocal) to locate locally installed scripts. So in your `.lintstagedrc` you can write:
252
253```json
254{
255 "*.js": "eslint --fix"
256}
257```
258
259Pass arguments to your commands separated by space as you would do in the shell. See [examples](#examples) below.
260
261## Running multiple commands in a sequence
262
263You can run multiple commands in a sequence on every glob. To do so, pass an array of commands instead of a single one. This is useful for running autoformatting tools like `eslint --fix` or `stylefmt` but can be used for any arbitrary sequences.
264
265For example:
266
267```json
268{
269 "*.js": ["eslint", "prettier --write"]
270}
271```
272
273going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
274
275## Using JS configuration files
276
277Writing the configuration file in JavaScript is the most powerful way to configure lint-staged (`lint-staged.config.js`, [similar](https://github.com/okonet/lint-staged#configuration), or passed via `--config`). From the configuration file, you can export either a single function or an object.
278
279If the `exports` value is a function, it will receive an array of all staged filenames. You can then build your own matchers for the files and return a command string or an array of command strings. These strings are considered complete and should include the filename arguments, if wanted.
280
281If the `exports` value is an object, its keys should be glob matches (like in the normal non-js config format). The values can either be like in the normal config or individual functions like described above. Instead of receiving all matched files, the functions in the exported object will only receive the staged files matching the corresponding glob key.
282
283### Function signature
284
285The function can also be async:
286
287```ts
288(filenames: string[]) => string | string[] | Promise<string | string[]>
289```
290
291### Example: Export a function to build your own matchers
292
293<details>
294 <summary>Click to expand</summary>
295
296```js
297// lint-staged.config.js
298import micromatch from 'micromatch'
299
300export default (allStagedFiles) => {
301 const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh'])
302 if (shFiles.length) {
303 return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
304 }
305 const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts'])
306 const docFiles = micromatch(allStagedFiles, ['**/*.md'])
307 return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
308}
309```
310
311</details>
312
313### Example: Wrap filenames in single quotes and run once per file
314
315<details>
316 <summary>Click to expand</summary>
317
318```js
319// .lintstagedrc.js
320export default {
321 '**/*.js?(x)': (filenames) => filenames.map((filename) => `prettier --write '${filename}'`),
322}
323```
324
325</details>
326
327### Example: Run `tsc` on changes to TypeScript files, but do not pass any filename arguments
328
329<details>
330 <summary>Click to expand</summary>
331
332```js
333// lint-staged.config.js
334export default {
335 '**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit',
336}
337```
338
339</details>
340
341### Example: Run ESLint on entire repo if more than 10 staged files
342
343<details>
344 <summary>Click to expand</summary>
345
346```js
347// .lintstagedrc.js
348export default {
349 '**/*.js?(x)': (filenames) =>
350 filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`,
351}
352```
353
354</details>
355
356### Example: Use your own globs
357
358<details>
359 <summary>Click to expand</summary>
360
361It's better to use the [function-based configuration (seen above)](https://github.com/okonet/lint-staged#example-export-a-function-to-build-your-own-matchers), if your use case is this.
362
363```js
364// lint-staged.config.js
365import micromatch from 'micromatch'
366
367export default {
368 '*': (allFiles) => {
369 const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts'])
370 const docFiles = micromatch(allFiles, ['**/*.md'])
371 return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
372 },
373}
374```
375
376</details>
377
378### Example: Ignore files from match
379
380<details>
381 <summary>Click to expand</summary>
382
383If for some reason you want to ignore files from the glob match, you can use `micromatch.not()`:
384
385```js
386// lint-staged.config.js
387import micromatch from 'micromatch'
388
389export default {
390 '*.js': (files) => {
391 // from `files` filter those _NOT_ matching `*test.js`
392 const match = micromatch.not(files, '*test.js')
393 return `eslint ${match.join(' ')}`
394 },
395}
396```
397
398Please note that for most cases, globs can achieve the same effect. For the above example, a matching glob would be `!(*test).js`.
399
400</details>
401
402### Example: Use relative paths for commands
403
404<details>
405 <summary>Click to expand</summary>
406
407```js
408import path from 'path'
409
410export default {
411 '*.ts': (absolutePaths) => {
412 const cwd = process.cwd()
413 const relativePaths = absolutePaths.map((file) => path.relative(cwd, file))
414 return `ng lint myProjectName --files ${relativePaths.join(' ')}`
415 },
416}
417```
418
419</details>
420
421## Reformatting the code
422
423Tools like [Prettier](https://prettier.io), ESLint/TSLint, or stylelint can reformat your code according to an appropriate config by running `prettier --write`/`eslint --fix`/`tslint --fix`/`stylelint --fix`. Lint-staged will automatically add any modifications to the commit as long as there are no errors.
424
425```json
426{
427 "*.js": "prettier --write"
428}
429```
430
431Prior to version 10, tasks had to manually include `git add` as the final step. This behavior has been integrated into lint-staged itself in order to prevent race conditions with multiple tasks editing the same files. If lint-staged detects `git add` in task configurations, it will show a warning in the console. Please remove `git add` from your configuration after upgrading.
432
433## Examples
434
435All examples assume you've already set up lint-staged in the `package.json` file and [husky](https://github.com/typicode/husky) in its own config file.
436
437```json
438{
439 "name": "My project",
440 "version": "0.1.0",
441 "scripts": {
442 "my-custom-script": "linter --arg1 --arg2"
443 },
444 "lint-staged": {}
445}
446```
447
448In `.husky/pre-commit`
449
450```shell
451#!/usr/bin/env sh
452. "$(dirname "$0")/_/husky.sh"
453
454npx lint-staged
455```
456
457_Note: we don't pass a path as an argument for the runners. This is important since lint-staged will do this for you._
458
459### ESLint with default parameters for `*.js` and `*.jsx` running as a pre-commit hook
460
461<details>
462 <summary>Click to expand</summary>
463
464```json
465{
466 "*.{js,jsx}": "eslint"
467}
468```
469
470</details>
471
472### Automatically fix code style with `--fix` and add to commit
473
474<details>
475 <summary>Click to expand</summary>
476
477```json
478{
479 "*.js": "eslint --fix"
480}
481```
482
483This will run `eslint --fix` and automatically add changes to the commit.
484
485</details>
486
487### Reuse npm script
488
489<details>
490 <summary>Click to expand</summary>
491
492If you wish to reuse a npm script defined in your package.json:
493
494```json
495{
496 "*.js": "npm run my-custom-script --"
497}
498```
499
500The following is equivalent:
501
502```json
503{
504 "*.js": "linter --arg1 --arg2"
505}
506```
507
508</details>
509
510### Use environment variables with linting commands
511
512<details>
513 <summary>Click to expand</summary>
514
515Linting commands _do not_ support the shell convention of expanding environment variables. To enable the convention yourself, use a tool like [`cross-env`](https://github.com/kentcdodds/cross-env).
516
517For example, here is `jest` running on all `.js` files with the `NODE_ENV` variable being set to `"test"`:
518
519```json
520{
521 "*.js": ["cross-env NODE_ENV=test jest --bail --findRelatedTests"]
522}
523```
524
525</details>
526
527### Automatically fix code style with `prettier` for any format Prettier supports
528
529<details>
530 <summary>Click to expand</summary>
531
532```json
533{
534 "*": "prettier --ignore-unknown --write"
535}
536```
537
538</details>
539
540### Automatically fix code style with `prettier` for JavaScript, TypeScript, Markdown, HTML, or CSS
541
542<details>
543 <summary>Click to expand</summary>
544
545```json
546{
547 "*.{js,jsx,ts,tsx,md,html,css}": "prettier --write"
548}
549```
550
551</details>
552
553### Stylelint for CSS with defaults and for SCSS with SCSS syntax
554
555<details>
556 <summary>Click to expand</summary>
557
558```json
559{
560 "*.css": "stylelint",
561 "*.scss": "stylelint --syntax=scss"
562}
563```
564
565</details>
566
567### Run PostCSS sorting and Stylelint to check
568
569<details>
570 <summary>Click to expand</summary>
571
572```json
573{
574 "*.scss": ["postcss --config path/to/your/config --replace", "stylelint"]
575}
576```
577
578</details>
579
580### Minify the images
581
582<details>
583 <summary>Click to expand</summary>
584
585```json
586{
587 "*.{png,jpeg,jpg,gif,svg}": "imagemin-lint-staged"
588}
589```
590
591<details>
592 <summary>More about <code>imagemin-lint-staged</code></summary>
593
594[imagemin-lint-staged](https://github.com/tomchentw/imagemin-lint-staged) is a CLI tool designed for lint-staged usage with sensible defaults.
595
596See more on [this blog post](https://medium.com/@tomchentw/imagemin-lint-staged-in-place-minify-the-images-before-adding-to-the-git-repo-5acda0b4c57e) for benefits of this approach.
597
598</details>
599</details>
600
601### Typecheck your staged files with flow
602
603<details>
604 <summary>Click to expand</summary>
605
606```json
607{
608 "*.{js,jsx}": "flow focus-check"
609}
610```
611
612</details>
613
614### Integrate with Next.js
615
616<details>
617 <summary>Click to expand</summary>
618
619```js
620// .lintstagedrc.js
621// See https://nextjs.org/docs/basic-features/eslint#lint-staged for details
622
623const path = require('path')
624
625const buildEslintCommand = (filenames) =>
626 `next lint --fix --file ${filenames
627 .map((f) => path.relative(process.cwd(), f))
628 .join(' --file ')}`
629
630module.exports = {
631 '*.{js,jsx,ts,tsx}': [buildEslintCommand],
632}
633```
634
635</details>
636
637## Frequently Asked Questions
638
639### The output of commit hook looks weird (no colors, duplicate lines, …)
640
641<details>
642 <summary>Click to expand</summary>
643
644Git 2.36.0 introduced a change to hooks where they were no longer run in the original TTY.
645This was fixed in 2.37.0:
646
647https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.37.0.txt
648
649> - In Git 2.36 we revamped the way how hooks are invoked. One change
650> that is end-user visible is that the output of a hook is no longer
651> directly connected to the standard output of "git" that spawns the
652> hook, which was noticed post release. This is getting corrected.
653> (merge [a082345372](https://github.com/git/git/commit/a082345372) ab/hooks-regression-fix later to maint).
654
655</details>
656
657### Can I use `lint-staged` via node?
658
659<details>
660 <summary>Click to expand</summary>
661
662Yes!
663
664```js
665import lintStaged from 'lint-staged'
666
667try {
668 const success = await lintStaged()
669 console.log(success ? 'Linting was successful!' : 'Linting failed!')
670} catch (e) {
671 // Failed to load configuration
672 console.error(e)
673}
674```
675
676Parameters to `lintStaged` are equivalent to their CLI counterparts:
677
678```js
679const success = await lintStaged({
680 allowEmpty: false,
681 concurrent: true,
682 configPath: './path/to/configuration/file',
683 cwd: process.cwd(),
684 debug: false,
685 maxArgLength: null,
686 quiet: false,
687 relative: false,
688 shell: false,
689 stash: true,
690 verbose: false,
691})
692```
693
694You can also pass config directly with `config` option:
695
696```js
697const success = await lintStaged({
698 allowEmpty: false,
699 concurrent: true,
700 config: { '*.js': 'eslint --fix' },
701 cwd: process.cwd(),
702 debug: false,
703 maxArgLength: null,
704 quiet: false,
705 relative: false,
706 shell: false,
707 stash: true,
708 verbose: false,
709})
710```
711
712The `maxArgLength` option configures chunking of tasks into multiple parts that are run one after the other. This is to avoid issues on Windows platforms where the maximum length of the command line argument string is limited to 8192 characters. Lint-staged might generate a very long argument string when there are many staged files. This option is set automatically from the cli, but not via the Node.js API by default.
713
714</details>
715
716### Using with JetBrains IDEs _(WebStorm, PyCharm, IntelliJ IDEA, RubyMine, etc.)_
717
718<details>
719 <summary>Click to expand</summary>
720
721_**Update**_: The latest version of JetBrains IDEs now support running hooks as you would expect.
722
723When using the IDE's GUI to commit changes with the `precommit` hook, you might see inconsistencies in the IDE and command line. This is [known issue](https://youtrack.jetbrains.com/issue/IDEA-135454) at JetBrains so if you want this fixed, please vote for it on YouTrack.
724
725Until the issue is resolved in the IDE, you can use the following config to work around it:
726
727husky v1.x
728
729```json
730{
731 "husky": {
732 "hooks": {
733 "pre-commit": "lint-staged",
734 "post-commit": "git update-index --again"
735 }
736 }
737}
738```
739
740husky v0.x
741
742```json
743{
744 "scripts": {
745 "precommit": "lint-staged",
746 "postcommit": "git update-index --again"
747 }
748}
749```
750
751_Thanks to [this comment](https://youtrack.jetbrains.com/issue/IDEA-135454#comment=27-2710654) for the fix!_
752
753</details>
754
755### How to use `lint-staged` in a multi-package monorepo?
756
757<details>
758 <summary>Click to expand</summary>
759
760Install _lint-staged_ on the monorepo root level, and add separate configuration files in each package. When running, _lint-staged_ will always use the configuration closest to a staged file, so having separate configuration files makes sure linters do not "leak" into other packages.
761
762For example, in a monorepo with `packages/frontend/.lintstagedrc.json` and `packages/backend/.lintstagedrc.json`, a staged file inside `packages/frontend/` will only match that configuration, and not the one in `packages/backend/`.
763
764**Note**: _lint-staged_ discovers the closest configuration to each staged file, even if that configuration doesn't include any matching globs. Given these example configurations:
765
766```js
767// ./.lintstagedrc.json
768{ "*.md": "prettier --write" }
769```
770
771```js
772// ./packages/frontend/.lintstagedrc.json
773{ "*.js": "eslint --fix" }
774```
775
776When committing `./packages/frontend/README.md`, it **will not run** _prettier_, because the configuration in the `frontend/` directory is closer to the file and doesn't include it. You should treat all _lint-staged_ configuration files as isolated and separated from each other. You can always use JS files to "extend" configurations, for example:
777
778```js
779import baseConfig from '../.lintstagedrc.js'
780
781export default {
782 ...baseConfig,
783 '*.js': 'eslint --fix',
784}
785```
786
787To support backwards-compatibility, monorepo features require multiple _lint-staged_ configuration files present in the git repo. If you still want to run _lint-staged_ in only one of the packages in a monorepo, you can either add an "empty" _lint-staged_ configuration to the root of the repo (so that there's two configs in total), or alternatively run _lint-staged_ with the `--cwd` option pointing to your package directory (for example, `lint-staged --cwd packages/frontend`).
788
789</details>
790
791### Can I lint files outside of the current project folder?
792
793<details>
794 <summary>Click to expand</summary>
795
796tl;dr: Yes, but the pattern should start with `../`.
797
798By default, `lint-staged` executes linters only on the files present inside the project folder(where `lint-staged` is installed and run from).
799So this question is relevant _only_ when the project folder is a child folder inside the git repo.
800In certain project setups, it might be desirable to bypass this restriction. See [#425](https://github.com/okonet/lint-staged/issues/425), [#487](https://github.com/okonet/lint-staged/issues/487) for more context.
801
802`lint-staged` provides an escape hatch for the same(`>= v7.3.0`). For patterns that start with `../`, all the staged files are allowed to match against the pattern.
803Note that patterns like `*.js`, `**/*.js` will still only match the project files and not any of the files in parent or sibling directories.
804
805Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo-suhas/lint-staged-django-react-demo).
806
807</details>
808
809### Can I run `lint-staged` in CI, or when there are no staged files?
810
811<details>
812 <summary>Click to expand</summary>
813
814Lint-staged will by default run against files staged in git, and should be run during the git pre-commit hook, for example. It's also possible to override this default behaviour and run against files in a specific diff, for example
815all changed files between two different branches. If you want to run _lint-staged_ in the CI, maybe you can set it up to compare the branch in a _Pull Request_/_Merge Request_ to the target branch.
816
817Try out the `git diff` command until you are satisfied with the result, for example:
818
819```
820git diff --diff-filter=ACMR --name-only master...my-branch
821```
822
823This will print a list of _added_, _changed_, _modified_, and _renamed_ files between `master` and `my-branch`.
824
825You can then run lint-staged against the same files with:
826
827```
828npx lint-staged --diff="master...my-branch"
829```
830
831</details>
832
833### Can I use `lint-staged` with `ng lint`
834
835<details>
836 <summary>Click to expand</summary>
837
838You should not use `ng lint` through _lint-staged_, because it's designed to lint an entire project. Instead, you can add `ng lint` to your git pre-commit hook the same way as you would run lint-staged.
839
840See issue [!951](https://github.com/okonet/lint-staged/issues/951) for more details and possible workarounds.
841
842</details>
843
844### How can I ignore files from `.eslintignore`?
845
846<details>
847 <summary>Click to expand</summary>
848
849ESLint throws out `warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override` warnings that breaks the linting process ( if you used `--max-warnings=0` which is recommended ).
850
851#### ESLint < 7
852
853<details>
854 <summary>Click to expand</summary>
855
856Based on the discussion from [this issue](https://github.com/eslint/eslint/issues/9977), it was decided that using [the outlined script ](https://github.com/eslint/eslint/issues/9977#issuecomment-406420893)is the best route to fix this.
857
858So you can setup a `.lintstagedrc.js` config file to do this:
859
860```js
861import { CLIEngine } from 'eslint'
862
863export default {
864 '*.js': (files) => {
865 const cli = new CLIEngine({})
866 return 'eslint --max-warnings=0 ' + files.filter((file) => !cli.isPathIgnored(file)).join(' ')
867 },
868}
869```
870
871</details>
872
873#### ESLint >= 7
874
875<details>
876 <summary>Click to expand</summary>
877
878In versions of ESLint > 7, [isPathIgnored](https://eslint.org/docs/developer-guide/nodejs-api#-eslintispathignoredfilepath) is an async function and now returns a promise. The code below can be used to reinstate the above functionality.
879
880Since [10.5.3](https://github.com/okonet/lint-staged/releases), any errors due to a bad ESLint config will come through to the console.
881
882```js
883import { ESLint } from 'eslint'
884
885const removeIgnoredFiles = async (files) => {
886 const eslint = new ESLint()
887 const isIgnored = await Promise.all(
888 files.map((file) => {
889 return eslint.isPathIgnored(file)
890 })
891 )
892 const filteredFiles = files.filter((_, i) => !isIgnored[i])
893 return filteredFiles.join(' ')
894}
895
896export default {
897 '**/*.{ts,tsx,js,jsx}': async (files) => {
898 const filesToLint = await removeIgnoredFiles(files)
899 return [`eslint --max-warnings=0 ${filesToLint}`]
900 },
901}
902```
903
904</details>
905
906</details>