UNPKG

33.7 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://blog.johnnyreilly.com/2020/12/prettier-your-csharp-with-dotnet-format-and-lint-staged.html)
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).
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.
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/README.md#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/README.md#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## Frequently Asked Questions
615
616### Can I use `lint-staged` via node?
617
618<details>
619 <summary>Click to expand</summary>
620
621Yes!
622
623```js
624import lintStaged from 'lint-staged'
625
626try {
627 const success = await lintStaged()
628 console.log(success ? 'Linting was successful!' : 'Linting failed!')
629} catch (e) {
630 // Failed to load configuration
631 console.error(e)
632}
633```
634
635Parameters to `lintStaged` are equivalent to their CLI counterparts:
636
637```js
638const success = await lintStaged({
639 allowEmpty: false,
640 concurrent: true,
641 configPath: './path/to/configuration/file',
642 cwd: process.cwd(),
643 debug: false,
644 maxArgLength: null,
645 quiet: false,
646 relative: false,
647 shell: false,
648 stash: true,
649 verbose: false,
650})
651```
652
653You can also pass config directly with `config` option:
654
655```js
656const success = await lintStaged({
657 allowEmpty: false,
658 concurrent: true,
659 config: { '*.js': 'eslint --fix' },
660 cwd: process.cwd(),
661 debug: false,
662 maxArgLength: null,
663 quiet: false,
664 relative: false,
665 shell: false,
666 stash: true,
667 verbose: false,
668})
669```
670
671The `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.
672
673</details>
674
675### Using with JetBrains IDEs _(WebStorm, PyCharm, IntelliJ IDEA, RubyMine, etc.)_
676
677<details>
678 <summary>Click to expand</summary>
679
680_**Update**_: The latest version of JetBrains IDEs now support running hooks as you would expect.
681
682When 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.
683
684Until the issue is resolved in the IDE, you can use the following config to work around it:
685
686husky v1.x
687
688```json
689{
690 "husky": {
691 "hooks": {
692 "pre-commit": "lint-staged",
693 "post-commit": "git update-index --again"
694 }
695 }
696}
697```
698
699husky v0.x
700
701```json
702{
703 "scripts": {
704 "precommit": "lint-staged",
705 "postcommit": "git update-index --again"
706 }
707}
708```
709
710_Thanks to [this comment](https://youtrack.jetbrains.com/issue/IDEA-135454#comment=27-2710654) for the fix!_
711
712</details>
713
714### How to use `lint-staged` in a multi-package monorepo?
715
716<details>
717 <summary>Click to expand</summary>
718
719Install _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.
720
721For 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/`.
722
723**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:
724
725```js
726// ./.lintstagedrc.json
727{ "*.md": "prettier --write" }
728```
729
730```js
731// ./packages/frontend/.lintstagedrc.json
732{ "*.js": "eslint --fix" }
733```
734
735When 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:
736
737```js
738import baseConfig from '../.lintstagedrc.js'
739
740export default {
741 ...baseConfig,
742 '*.js': 'eslint --fix',
743}
744```
745
746</details>
747
748### Can I lint files outside of the current project folder?
749
750<details>
751 <summary>Click to expand</summary>
752
753tl;dr: Yes, but the pattern should start with `../`.
754
755By default, `lint-staged` executes linters only on the files present inside the project folder(where `lint-staged` is installed and run from).
756So this question is relevant _only_ when the project folder is a child folder inside the git repo.
757In 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.
758
759`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.
760Note that patterns like `*.js`, `**/*.js` will still only match the project files and not any of the files in parent or sibling directories.
761
762Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo-suhas/lint-staged-django-react-demo).
763
764</details>
765
766### Can I run `lint-staged` in CI, or when there are no staged files?
767
768<details>
769 <summary>Click to expand</summary>
770
771Lint-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
772all 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.
773
774Try out the `git diff` command until you are satisfied with the result, for example:
775
776```
777git diff --diff-filter=ACMR --name-only master...my-branch
778```
779
780This will print a list of _added_, _changed_, _modified_, and _renamed_ files between `master` and `my-branch`.
781
782You can then run lint-staged against the same files with:
783
784```
785npx lint-staged --diff="master...my-branch"
786```
787
788</details>
789
790### Can I use `lint-staged` with `ng lint`
791
792<details>
793 <summary>Click to expand</summary>
794
795You 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.
796
797See issue [!951](https://github.com/okonet/lint-staged/issues/951) for more details and possible workarounds.
798
799</details>
800
801### How can I ignore files from `.eslintignore`?
802
803<details>
804 <summary>Click to expand</summary>
805
806ESLint 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 ).
807
808#### ESLint < 7
809
810<details>
811 <summary>Click to expand</summary>
812
813Based 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.
814
815So you can setup a `.lintstagedrc.js` config file to do this:
816
817```js
818import { CLIEngine } from 'eslint'
819
820export default {
821 '*.js': (files) => {
822 const cli = new CLIEngine({})
823 return 'eslint --max-warnings=0 ' + files.filter((file) => !cli.isPathIgnored(file)).join(' ')
824 },
825}
826```
827
828</details>
829
830#### ESLint >= 7
831
832<details>
833 <summary>Click to expand</summary>
834
835In 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.
836
837Since [10.5.3](https://github.com/okonet/lint-staged/releases), any errors due to a bad ESLint config will come through to the console.
838
839```js
840import { ESLint } from 'eslint'
841
842const removeIgnoredFiles = async (files) => {
843 const eslint = new ESLint()
844 const isIgnored = await Promise.all(
845 files.map((file) => {
846 return eslint.isPathIgnored(file)
847 })
848 )
849 const filteredFiles = files.filter((_, i) => !isIgnored[i])
850 return filteredFiles.join(' ')
851}
852
853export default {
854 '**/*.{ts,tsx,js,jsx}': async (files) => {
855 const filesToLint = await removeIgnoredFiles(files)
856 return [`eslint --max-warnings=0 ${filesToLint}`]
857 },
858}
859```
860
861</details>
862
863</details>