lint-staged 10.5.2 → 11.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +238 -52
- package/bin/lint-staged.js +12 -10
- package/lib/index.js +15 -10
- package/lib/runAll.js +2 -1
- package/package.json +19 -24
package/README.md
CHANGED
|
@@ -6,38 +6,39 @@ Run linters against staged git files and don't let :poop: slip into your code ba
|
|
|
6
6
|
|
|
7
7
|
## Why
|
|
8
8
|
|
|
9
|
-
Linting 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.
|
|
9
|
+
Linting 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.
|
|
10
10
|
|
|
11
11
|
This 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.
|
|
12
12
|
|
|
13
|
-
## Related
|
|
13
|
+
## Related blog posts and talks
|
|
14
14
|
|
|
15
15
|
- [Introductory Medium post - Andrey Okonetchnikov, 2016](https://medium.com/@okonetchnikov/make-linting-great-again-f3890e1ad6b8#.8qepn2b5l)
|
|
16
16
|
- [Running Jest Tests Before Each Git Commit - Ben McCormick, 2017](https://benmccormick.org/2017/02/26/running-jest-tests-before-each-git-commit/)
|
|
17
17
|
- [AgentConf presentation - Andrey Okonetchnikov, 2018](https://www.youtube.com/watch?v=-mhY7e-EsC4)
|
|
18
18
|
- [SurviveJS interview - Juho Vepsäläinen and Andrey Okonetchnikov, 2018](https://survivejs.com/blog/lint-staged-interview/)
|
|
19
|
+
- [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)
|
|
19
20
|
|
|
20
21
|
> If you've written one, please submit a PR with the link to it!
|
|
21
22
|
|
|
22
23
|
## Installation and setup
|
|
23
24
|
|
|
24
|
-
The fastest way to start using lint-staged is to run following command in your terminal:
|
|
25
|
+
The fastest way to start using lint-staged is to run the following command in your terminal:
|
|
25
26
|
|
|
26
27
|
```bash
|
|
27
|
-
npx mrm lint-staged
|
|
28
|
+
npx mrm@2 lint-staged
|
|
28
29
|
```
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
This command will install and configure [husky](https://github.com/typicode/husky) and lint-staged depending on the code quality tools from your project's `package.json` dependencies, so please make sure you install (`npm install --save-dev`) and configure all code quality tools like [Prettier](https://prettier.io) and [ESLint](https://eslint.org) prior to that.
|
|
31
32
|
|
|
32
|
-
Don't forget to commit changes to `package.json` to share this setup with your team!
|
|
33
|
+
Don't forget to commit changes to `package.json` and `.husky` to share this setup with your team!
|
|
33
34
|
|
|
34
|
-
Now change a few files, `git add` or `git add --patch` some of them to your commit and try to `git commit` them.
|
|
35
|
+
Now change a few files, `git add` or `git add --patch` some of them to your commit, and try to `git commit` them.
|
|
35
36
|
|
|
36
37
|
See [examples](#examples) and [configuration](#configuration) for more information.
|
|
37
38
|
|
|
38
39
|
## Changelog
|
|
39
40
|
|
|
40
|
-
See [Releases](https://github.com/okonet/lint-staged/releases)
|
|
41
|
+
See [Releases](https://github.com/okonet/lint-staged/releases).
|
|
41
42
|
|
|
42
43
|
### Migration
|
|
43
44
|
|
|
@@ -47,10 +48,10 @@ See [Releases](https://github.com/okonet/lint-staged/releases)
|
|
|
47
48
|
If your task previously contained a `git add` step, please remove this.
|
|
48
49
|
The automatic behaviour ensures there are less race-conditions,
|
|
49
50
|
since trying to run multiple git operations at the same time usually results in an error.
|
|
50
|
-
- From `v10.0.0` onwards
|
|
51
|
-
Since git stashes require at least an initial commit, you shouldn't run
|
|
52
|
-
- From `v10.0.0` onwards
|
|
53
|
-
- From `v10.0.0` onwards
|
|
51
|
+
- From `v10.0.0` onwards, lint-staged uses git stashes to improve speed and provide backups while running.
|
|
52
|
+
Since git stashes require at least an initial commit, you shouldn't run lint-staged in an empty repo.
|
|
53
|
+
- From `v10.0.0` onwards, lint-staged requires Node.js version 10.13.0 or later.
|
|
54
|
+
- 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.
|
|
54
55
|
|
|
55
56
|
## Command line flags
|
|
56
57
|
|
|
@@ -78,7 +79,7 @@ Options:
|
|
|
78
79
|
```
|
|
79
80
|
|
|
80
81
|
- **`--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.
|
|
81
|
-
- **`--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 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 -`.
|
|
82
|
+
- **`--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 -`.
|
|
82
83
|
- **`--debug`**: Run in debug mode. When set, it does the following:
|
|
83
84
|
- 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*`.
|
|
84
85
|
- 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.
|
|
@@ -94,7 +95,7 @@ Options:
|
|
|
94
95
|
|
|
95
96
|
## Configuration
|
|
96
97
|
|
|
97
|
-
Starting with v3.1 you can now use different ways of configuring
|
|
98
|
+
Starting with v3.1 you can now use different ways of configuring lint-staged:
|
|
98
99
|
|
|
99
100
|
- `lint-staged` object in your `package.json`
|
|
100
101
|
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
|
|
@@ -134,7 +135,7 @@ So, considering you did `git add file1.ext file2.ext`, lint-staged will run the
|
|
|
134
135
|
|
|
135
136
|
## Filtering files
|
|
136
137
|
|
|
137
|
-
Linter commands work on a subset of all staged files, defined by a _glob pattern_.
|
|
138
|
+
Linter 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:
|
|
138
139
|
|
|
139
140
|
- If the glob pattern contains no slashes (`/`), micromatch's `matchBase` option will enabled, so globs match a file's basename regardless of directory:
|
|
140
141
|
- **`"*.js"`** will match all JS files, like `/test.js` and `/foo/bar/test.js`
|
|
@@ -143,7 +144,7 @@ Linter commands work on a subset of all staged files, defined by a _glob pattern
|
|
|
143
144
|
- **`"./*.js"`** will match all JS files in the git repo root, so `/test.js` but not `/foo/bar/test.js`
|
|
144
145
|
- **`"foo/**/\*.js"`** will match all JS files inside the`/foo`directory, so`/foo/bar/test.js`but not`/test.js`
|
|
145
146
|
|
|
146
|
-
When matching,
|
|
147
|
+
When matching, lint-staged will do the following
|
|
147
148
|
|
|
148
149
|
- Resolve the git root automatically, no configuration needed.
|
|
149
150
|
- Pick the staged files which are present inside the project directory.
|
|
@@ -152,11 +153,11 @@ When matching, `lint-staged` will do the following
|
|
|
152
153
|
|
|
153
154
|
**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).
|
|
154
155
|
|
|
155
|
-
Also see [How to use `lint-staged` in a multi
|
|
156
|
+
Also see [How to use `lint-staged` in a multi-package monorepo?](#how-to-use-lint-staged-in-a-multi-package-monorepo)
|
|
156
157
|
|
|
157
158
|
### Ignoring files
|
|
158
159
|
|
|
159
|
-
The concept of `lint-staged` is to run configured linter (or other
|
|
160
|
+
The 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.
|
|
160
161
|
|
|
161
162
|
Consider 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.
|
|
162
163
|
|
|
@@ -166,7 +167,7 @@ In advanced scenarios, where it is impossible to configure the linter task itsel
|
|
|
166
167
|
|
|
167
168
|
Supported are any executables installed locally or globally via `npm` as well as any executable from your \$PATH.
|
|
168
169
|
|
|
169
|
-
> Using globally installed scripts is discouraged, since lint-staged may not work for someone who doesn
|
|
170
|
+
> Using globally installed scripts is discouraged, since lint-staged may not work for someone who doesn't have it installed.
|
|
170
171
|
|
|
171
172
|
`lint-staged` uses [execa](https://github.com/sindresorhus/execa#preferlocal) to locate locally installed scripts. So in your `.lintstagedrc` you can write:
|
|
172
173
|
|
|
@@ -194,11 +195,11 @@ going to execute `eslint` and if it exits with `0` code, it will execute `pretti
|
|
|
194
195
|
|
|
195
196
|
## Using JS configuration file
|
|
196
197
|
|
|
197
|
-
Writing the configuration file in JavaScript is the most powerful way to configure
|
|
198
|
+
Writing 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.
|
|
198
199
|
|
|
199
|
-
If 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
|
|
200
|
+
If 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.
|
|
200
201
|
|
|
201
|
-
If 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
|
|
202
|
+
If 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.
|
|
202
203
|
|
|
203
204
|
### Function signature
|
|
204
205
|
|
|
@@ -210,51 +211,74 @@ The function can also be async:
|
|
|
210
211
|
|
|
211
212
|
### Example: Export a function to build your own matchers
|
|
212
213
|
|
|
214
|
+
<details>
|
|
215
|
+
<summary>Click to expand</summary>
|
|
216
|
+
|
|
213
217
|
```js
|
|
214
218
|
// lint-staged.config.js
|
|
215
219
|
const micromatch = require('micromatch')
|
|
216
220
|
|
|
217
221
|
module.exports = (allStagedFiles) => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts']);
|
|
223
|
-
const docFiles = micromatch(allStagedFiles, ['**/*.md']);
|
|
224
|
-
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
|
|
222
|
+
const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh'])
|
|
223
|
+
if (shFiles.length) {
|
|
224
|
+
return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
|
|
225
225
|
}
|
|
226
|
+
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts'])
|
|
227
|
+
const docFiles = micromatch(allStagedFiles, ['**/*.md'])
|
|
228
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
|
|
229
|
+
}
|
|
226
230
|
```
|
|
227
231
|
|
|
232
|
+
</details>
|
|
228
233
|
|
|
229
234
|
### Example: Wrap filenames in single quotes and run once per file
|
|
230
235
|
|
|
236
|
+
<details>
|
|
237
|
+
<summary>Click to expand</summary>
|
|
238
|
+
|
|
231
239
|
```js
|
|
232
240
|
// .lintstagedrc.js
|
|
233
241
|
module.exports = {
|
|
234
|
-
'**/*.js?(x)': (filenames) => filenames.map((filename) => `prettier --write '${filename}'`)
|
|
242
|
+
'**/*.js?(x)': (filenames) => filenames.map((filename) => `prettier --write '${filename}'`),
|
|
235
243
|
}
|
|
236
244
|
```
|
|
237
245
|
|
|
246
|
+
</details>
|
|
247
|
+
|
|
238
248
|
### Example: Run `tsc` on changes to TypeScript files, but do not pass any filename arguments
|
|
239
249
|
|
|
250
|
+
<details>
|
|
251
|
+
<summary>Click to expand</summary>
|
|
252
|
+
|
|
240
253
|
```js
|
|
241
254
|
// lint-staged.config.js
|
|
242
255
|
module.exports = {
|
|
243
|
-
'**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit'
|
|
256
|
+
'**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit',
|
|
244
257
|
}
|
|
245
258
|
```
|
|
246
259
|
|
|
247
|
-
|
|
260
|
+
</details>
|
|
261
|
+
|
|
262
|
+
### Example: Run ESLint on entire repo if more than 10 staged files
|
|
263
|
+
|
|
264
|
+
<details>
|
|
265
|
+
<summary>Click to expand</summary>
|
|
248
266
|
|
|
249
267
|
```js
|
|
250
268
|
// .lintstagedrc.js
|
|
251
269
|
module.exports = {
|
|
252
270
|
'**/*.js?(x)': (filenames) =>
|
|
253
|
-
filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}
|
|
271
|
+
filenames.length > 10 ? 'eslint .' : `eslint ${filenames.join(' ')}`,
|
|
254
272
|
}
|
|
255
273
|
```
|
|
256
274
|
|
|
275
|
+
</details>
|
|
276
|
+
|
|
257
277
|
### Example: Use your own globs
|
|
278
|
+
|
|
279
|
+
<details>
|
|
280
|
+
<summary>Click to expand</summary>
|
|
281
|
+
|
|
258
282
|
It'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.
|
|
259
283
|
|
|
260
284
|
```js
|
|
@@ -263,15 +287,20 @@ const micromatch = require('micromatch')
|
|
|
263
287
|
|
|
264
288
|
module.exports = {
|
|
265
289
|
'*': (allFiles) => {
|
|
266
|
-
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts'])
|
|
267
|
-
const docFiles = micromatch(allFiles, ['**/*.md'])
|
|
268
|
-
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
|
|
269
|
-
}
|
|
290
|
+
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts'])
|
|
291
|
+
const docFiles = micromatch(allFiles, ['**/*.md'])
|
|
292
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
|
|
293
|
+
},
|
|
270
294
|
}
|
|
271
295
|
```
|
|
272
296
|
|
|
297
|
+
</details>
|
|
298
|
+
|
|
273
299
|
### Example: Ignore files from match
|
|
274
300
|
|
|
301
|
+
<details>
|
|
302
|
+
<summary>Click to expand</summary>
|
|
303
|
+
|
|
275
304
|
If for some reason you want to ignore files from the glob match, you can use `micromatch.not()`:
|
|
276
305
|
|
|
277
306
|
```js
|
|
@@ -283,14 +312,19 @@ module.exports = {
|
|
|
283
312
|
// from `files` filter those _NOT_ matching `*test.js`
|
|
284
313
|
const match = micromatch.not(files, '*test.js')
|
|
285
314
|
return `eslint ${match.join(' ')}`
|
|
286
|
-
}
|
|
315
|
+
},
|
|
287
316
|
}
|
|
288
317
|
```
|
|
289
318
|
|
|
290
319
|
Please note that for most cases, globs can achieve the same effect. For the above example, a matching glob would be `!(*test).js`.
|
|
291
320
|
|
|
321
|
+
</details>
|
|
322
|
+
|
|
292
323
|
### Example: Use relative paths for commands
|
|
293
324
|
|
|
325
|
+
<details>
|
|
326
|
+
<summary>Click to expand</summary>
|
|
327
|
+
|
|
294
328
|
```js
|
|
295
329
|
const path = require('path')
|
|
296
330
|
|
|
@@ -299,10 +333,12 @@ module.exports = {
|
|
|
299
333
|
const cwd = process.cwd()
|
|
300
334
|
const relativePaths = absolutePaths.map((file) => path.relative(cwd, file))
|
|
301
335
|
return `ng lint myProjectName --files ${relativePaths.join(' ')}`
|
|
302
|
-
}
|
|
336
|
+
},
|
|
303
337
|
}
|
|
304
338
|
```
|
|
305
339
|
|
|
340
|
+
</details>
|
|
341
|
+
|
|
306
342
|
## Reformatting the code
|
|
307
343
|
|
|
308
344
|
Tools 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.
|
|
@@ -317,7 +353,7 @@ Prior to version 10, tasks had to manually include `git add` as the final step.
|
|
|
317
353
|
|
|
318
354
|
## Examples
|
|
319
355
|
|
|
320
|
-
All examples
|
|
356
|
+
All 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.
|
|
321
357
|
|
|
322
358
|
```json
|
|
323
359
|
{
|
|
@@ -326,27 +362,39 @@ All examples assuming you’ve already set up lint-staged and husky in the `pack
|
|
|
326
362
|
"scripts": {
|
|
327
363
|
"my-custom-script": "linter --arg1 --arg2"
|
|
328
364
|
},
|
|
329
|
-
"husky": {
|
|
330
|
-
"hooks": {
|
|
331
|
-
"pre-commit": "lint-staged"
|
|
332
|
-
}
|
|
333
|
-
},
|
|
334
365
|
"lint-staged": {}
|
|
335
366
|
}
|
|
336
367
|
```
|
|
337
368
|
|
|
338
|
-
|
|
369
|
+
In `.husky/pre-commit`
|
|
370
|
+
|
|
371
|
+
```shell
|
|
372
|
+
#!/bin/sh
|
|
373
|
+
. "$(dirname "$0")/_/husky.sh"
|
|
374
|
+
|
|
375
|
+
npx lint-staged
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
_Note: we don't pass a path as an argument for the runners. This is important since lint-staged will do this for you._
|
|
339
379
|
|
|
340
380
|
### ESLint with default parameters for `*.js` and `*.jsx` running as a pre-commit hook
|
|
341
381
|
|
|
382
|
+
<details>
|
|
383
|
+
<summary>Click to expand</summary>
|
|
384
|
+
|
|
342
385
|
```json
|
|
343
386
|
{
|
|
344
387
|
"*.{js,jsx}": "eslint"
|
|
345
388
|
}
|
|
346
389
|
```
|
|
347
390
|
|
|
391
|
+
</details>
|
|
392
|
+
|
|
348
393
|
### Automatically fix code style with `--fix` and add to commit
|
|
349
394
|
|
|
395
|
+
<details>
|
|
396
|
+
<summary>Click to expand</summary>
|
|
397
|
+
|
|
350
398
|
```json
|
|
351
399
|
{
|
|
352
400
|
"*.js": "eslint --fix"
|
|
@@ -355,8 +403,13 @@ _Note we don’t pass a path as an argument for the runners. This is important s
|
|
|
355
403
|
|
|
356
404
|
This will run `eslint --fix` and automatically add changes to the commit.
|
|
357
405
|
|
|
406
|
+
</details>
|
|
407
|
+
|
|
358
408
|
### Reuse npm script
|
|
359
409
|
|
|
410
|
+
<details>
|
|
411
|
+
<summary>Click to expand</summary>
|
|
412
|
+
|
|
360
413
|
If you wish to reuse a npm script defined in your package.json:
|
|
361
414
|
|
|
362
415
|
```json
|
|
@@ -373,8 +426,13 @@ The following is equivalent:
|
|
|
373
426
|
}
|
|
374
427
|
```
|
|
375
428
|
|
|
429
|
+
</details>
|
|
430
|
+
|
|
376
431
|
### Use environment variables with linting commands
|
|
377
432
|
|
|
433
|
+
<details>
|
|
434
|
+
<summary>Click to expand</summary>
|
|
435
|
+
|
|
378
436
|
Linting 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).
|
|
379
437
|
|
|
380
438
|
For example, here is `jest` running on all `.js` files with the `NODE_ENV` variable being set to `"test"`:
|
|
@@ -385,7 +443,47 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia
|
|
|
385
443
|
}
|
|
386
444
|
```
|
|
387
445
|
|
|
388
|
-
|
|
446
|
+
</details>
|
|
447
|
+
|
|
448
|
+
### Automatically fix code style with `prettier` for any format prettier supports
|
|
449
|
+
|
|
450
|
+
<details>
|
|
451
|
+
<summary>Click to expand</summary>
|
|
452
|
+
|
|
453
|
+
You need to add `micromatch` as a dev dependency.
|
|
454
|
+
|
|
455
|
+
```sh
|
|
456
|
+
$ npm i --save-dev micromatch
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
```js
|
|
460
|
+
// lint-staged.config.js
|
|
461
|
+
const micromatch = require('micromatch')
|
|
462
|
+
const prettier = require('prettier')
|
|
463
|
+
|
|
464
|
+
const prettierSupportedExtensions = prettier
|
|
465
|
+
.getSupportInfo()
|
|
466
|
+
.languages.map(({ extensions }) => extensions)
|
|
467
|
+
.flat()
|
|
468
|
+
const addQuotes = (a) => `"${a}"`
|
|
469
|
+
|
|
470
|
+
module.exports = (allStagedFiles) => {
|
|
471
|
+
const prettierFiles = micromatch(
|
|
472
|
+
allStagedFiles,
|
|
473
|
+
prettierSupportedExtensions.map((extension) => `**/*${extension}`)
|
|
474
|
+
)
|
|
475
|
+
return prettierFiles.length > 0
|
|
476
|
+
? [`prettier --write ${prettierFiles.map(addQuotes).join(' ')}`]
|
|
477
|
+
: []
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
</details>
|
|
482
|
+
|
|
483
|
+
### Automatically fix code style with `prettier` for JavaScript, TypeScript, Markdown, HTML, or CSS
|
|
484
|
+
|
|
485
|
+
<details>
|
|
486
|
+
<summary>Click to expand</summary>
|
|
389
487
|
|
|
390
488
|
```json
|
|
391
489
|
{
|
|
@@ -393,8 +491,13 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia
|
|
|
393
491
|
}
|
|
394
492
|
```
|
|
395
493
|
|
|
494
|
+
</details>
|
|
495
|
+
|
|
396
496
|
### Stylelint for CSS with defaults and for SCSS with SCSS syntax
|
|
397
497
|
|
|
498
|
+
<details>
|
|
499
|
+
<summary>Click to expand</summary>
|
|
500
|
+
|
|
398
501
|
```json
|
|
399
502
|
{
|
|
400
503
|
"*.css": "stylelint",
|
|
@@ -402,16 +505,26 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia
|
|
|
402
505
|
}
|
|
403
506
|
```
|
|
404
507
|
|
|
508
|
+
</details>
|
|
509
|
+
|
|
405
510
|
### Run PostCSS sorting and Stylelint to check
|
|
406
511
|
|
|
512
|
+
<details>
|
|
513
|
+
<summary>Click to expand</summary>
|
|
514
|
+
|
|
407
515
|
```json
|
|
408
516
|
{
|
|
409
517
|
"*.scss": ["postcss --config path/to/your/config --replace", "stylelint"]
|
|
410
518
|
}
|
|
411
519
|
```
|
|
412
520
|
|
|
521
|
+
</details>
|
|
522
|
+
|
|
413
523
|
### Minify the images
|
|
414
524
|
|
|
525
|
+
<details>
|
|
526
|
+
<summary>Click to expand</summary>
|
|
527
|
+
|
|
415
528
|
```json
|
|
416
529
|
{
|
|
417
530
|
"*.{png,jpeg,jpg,gif,svg}": "imagemin-lint-staged"
|
|
@@ -425,20 +538,29 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia
|
|
|
425
538
|
|
|
426
539
|
See 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.
|
|
427
540
|
|
|
541
|
+
</details>
|
|
428
542
|
</details>
|
|
429
543
|
|
|
430
544
|
### Typecheck your staged files with flow
|
|
431
545
|
|
|
546
|
+
<details>
|
|
547
|
+
<summary>Click to expand</summary>
|
|
548
|
+
|
|
432
549
|
```json
|
|
433
550
|
{
|
|
434
551
|
"*.{js,jsx}": "flow focus-check"
|
|
435
552
|
}
|
|
436
553
|
```
|
|
437
554
|
|
|
555
|
+
</details>
|
|
556
|
+
|
|
438
557
|
## Frequently Asked Questions
|
|
439
558
|
|
|
440
559
|
### Can I use `lint-staged` via node?
|
|
441
560
|
|
|
561
|
+
<details>
|
|
562
|
+
<summary>Click to expand</summary>
|
|
563
|
+
|
|
442
564
|
Yes!
|
|
443
565
|
|
|
444
566
|
```js
|
|
@@ -485,14 +607,19 @@ const success = await lintStaged({
|
|
|
485
607
|
relative: false,
|
|
486
608
|
shell: false,
|
|
487
609
|
stash: true,
|
|
488
|
-
verbose: false
|
|
610
|
+
verbose: false,
|
|
489
611
|
})
|
|
490
612
|
```
|
|
491
613
|
|
|
492
614
|
The `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.
|
|
493
615
|
|
|
616
|
+
</details>
|
|
617
|
+
|
|
494
618
|
### Using with JetBrains IDEs _(WebStorm, PyCharm, IntelliJ IDEA, RubyMine, etc.)_
|
|
495
619
|
|
|
620
|
+
<details>
|
|
621
|
+
<summary>Click to expand</summary>
|
|
622
|
+
|
|
496
623
|
_**Update**_: The latest version of JetBrains IDEs now support running hooks as you would expect.
|
|
497
624
|
|
|
498
625
|
When 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.
|
|
@@ -525,7 +652,12 @@ husky v0.x
|
|
|
525
652
|
|
|
526
653
|
_Thanks to [this comment](https://youtrack.jetbrains.com/issue/IDEA-135454#comment=27-2710654) for the fix!_
|
|
527
654
|
|
|
528
|
-
|
|
655
|
+
</details>
|
|
656
|
+
|
|
657
|
+
### How to use `lint-staged` in a multi-package monorepo?
|
|
658
|
+
|
|
659
|
+
<details>
|
|
660
|
+
<summary>Click to expand</summary>
|
|
529
661
|
|
|
530
662
|
Starting with v5.0, `lint-staged` automatically resolves the git root **without any** additional configuration. You configure `lint-staged` as you normally would if your project root and git root were the same directory.
|
|
531
663
|
|
|
@@ -534,8 +666,13 @@ If you wish to use `lint-staged` in a multi package monorepo, it is recommended
|
|
|
534
666
|
|
|
535
667
|
Example repo: [sudo-suhas/lint-staged-multi-pkg](https://github.com/sudo-suhas/lint-staged-multi-pkg).
|
|
536
668
|
|
|
669
|
+
</details>
|
|
670
|
+
|
|
537
671
|
### Can I lint files outside of the current project folder?
|
|
538
672
|
|
|
673
|
+
<details>
|
|
674
|
+
<summary>Click to expand</summary>
|
|
675
|
+
|
|
539
676
|
tl;dr: Yes, but the pattern should start with `../`.
|
|
540
677
|
|
|
541
678
|
By default, `lint-staged` executes linters only on the files present inside the project folder(where `lint-staged` is installed and run from).
|
|
@@ -547,11 +684,23 @@ Note that patterns like `*.js`, `**/*.js` will still only match the project file
|
|
|
547
684
|
|
|
548
685
|
Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo-suhas/lint-staged-django-react-demo).
|
|
549
686
|
|
|
550
|
-
|
|
687
|
+
</details>
|
|
688
|
+
|
|
689
|
+
### How can I ignore files from `.eslintignore`?
|
|
690
|
+
|
|
691
|
+
<details>
|
|
692
|
+
<summary>Click to expand</summary>
|
|
551
693
|
|
|
552
694
|
ESLint 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 ).
|
|
553
695
|
|
|
554
|
-
|
|
696
|
+
</details>
|
|
697
|
+
|
|
698
|
+
#### ESLint < 7
|
|
699
|
+
|
|
700
|
+
<details>
|
|
701
|
+
<summary>Click to expand</summary>
|
|
702
|
+
|
|
703
|
+
Based 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.
|
|
555
704
|
|
|
556
705
|
So you can setup a `.lintstagedrc.js` config file to do this:
|
|
557
706
|
|
|
@@ -562,6 +711,43 @@ const cli = new CLIEngine({})
|
|
|
562
711
|
|
|
563
712
|
module.exports = {
|
|
564
713
|
'*.js': (files) =>
|
|
565
|
-
'eslint --max-warnings=0 ' + files.filter((file) => !cli.isPathIgnored(file)).join(' ')
|
|
714
|
+
'eslint --max-warnings=0 ' + files.filter((file) => !cli.isPathIgnored(file)).join(' '),
|
|
566
715
|
}
|
|
567
716
|
```
|
|
717
|
+
|
|
718
|
+
</details>
|
|
719
|
+
|
|
720
|
+
#### ESLint >= 7
|
|
721
|
+
|
|
722
|
+
<details>
|
|
723
|
+
<summary>Click to expand</summary>
|
|
724
|
+
|
|
725
|
+
In 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.
|
|
726
|
+
|
|
727
|
+
This particular code uses a tiny package, [node-filter-async](https://www.npmjs.com/package/node-filter-async), to filter the files array with an async function. If you prefer to not have an extra dependency, it is quite simple to write a similar function.
|
|
728
|
+
|
|
729
|
+
Since [10.5.3](https://github.com/okonet/lint-staged/releases), any errors due to a bad ESLint config will come through to the console.
|
|
730
|
+
|
|
731
|
+
```js
|
|
732
|
+
const { ESLint } = require('eslint')
|
|
733
|
+
const filterAsync = require('node-filter-async').default
|
|
734
|
+
|
|
735
|
+
const eslintCli = new ESLint()
|
|
736
|
+
|
|
737
|
+
const removeIgnoredFiles = async (files) => {
|
|
738
|
+
const filteredFiles = await filterAsync(files, async (file) => {
|
|
739
|
+
const isIgnored = await eslintCli.isPathIgnored(file)
|
|
740
|
+
return !isIgnored
|
|
741
|
+
})
|
|
742
|
+
return filteredFiles.join(' ')
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
module.exports = {
|
|
746
|
+
'**/*.{ts,tsx,js,jsx}': async (files) => {
|
|
747
|
+
const filesToLint = await removeIgnoredFiles(files)
|
|
748
|
+
return [`eslint --max-warnings=0 ${filesToLint}`]
|
|
749
|
+
},
|
|
750
|
+
}
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
</details>
|
package/bin/lint-staged.js
CHANGED
|
@@ -17,7 +17,7 @@ const pkg = require('../package.json')
|
|
|
17
17
|
require('please-upgrade-node')(
|
|
18
18
|
Object.assign({}, pkg, {
|
|
19
19
|
engines: {
|
|
20
|
-
node: '>=
|
|
20
|
+
node: '>=12.13.0', // First LTS release of 'Erbium'
|
|
21
21
|
},
|
|
22
22
|
})
|
|
23
23
|
)
|
|
@@ -74,17 +74,19 @@ const getMaxArgLength = () => {
|
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
const cmdlineOptions = cmdline.opts()
|
|
78
|
+
|
|
77
79
|
const options = {
|
|
78
|
-
allowEmpty: !!
|
|
79
|
-
concurrent:
|
|
80
|
-
configPath:
|
|
81
|
-
debug: !!
|
|
80
|
+
allowEmpty: !!cmdlineOptions.allowEmpty,
|
|
81
|
+
concurrent: JSON.parse(cmdlineOptions.concurrent),
|
|
82
|
+
configPath: cmdlineOptions.config,
|
|
83
|
+
debug: !!cmdlineOptions.debug,
|
|
82
84
|
maxArgLength: getMaxArgLength() / 2,
|
|
83
|
-
stash: !!
|
|
84
|
-
quiet: !!
|
|
85
|
-
relative: !!
|
|
86
|
-
shell: !!
|
|
87
|
-
verbose: !!
|
|
85
|
+
stash: !!cmdlineOptions.stash, // commander inverts `no-<x>` flags to `!x`
|
|
86
|
+
quiet: !!cmdlineOptions.quiet,
|
|
87
|
+
relative: !!cmdlineOptions.relative,
|
|
88
|
+
shell: !!cmdlineOptions.shell,
|
|
89
|
+
verbose: !!cmdlineOptions.verbose,
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
debug('Options parsed from command-line:', options)
|
package/lib/index.js
CHANGED
|
@@ -128,19 +128,24 @@ module.exports = async function lintStaged(
|
|
|
128
128
|
printTaskOutput(ctx, logger)
|
|
129
129
|
return true
|
|
130
130
|
} catch (runAllError) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
131
|
+
if (runAllError && runAllError.ctx && runAllError.ctx.errors) {
|
|
132
|
+
const { ctx } = runAllError
|
|
133
|
+
if (ctx.errors.has(ApplyEmptyCommitError)) {
|
|
134
|
+
logger.warn(PREVENTED_EMPTY_COMMIT)
|
|
135
|
+
} else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) {
|
|
136
|
+
logger.error(GIT_ERROR)
|
|
137
|
+
if (ctx.shouldBackup) {
|
|
138
|
+
// No sense to show this if the backup stash itself is missing.
|
|
139
|
+
logger.error(RESTORE_STASH_EXAMPLE)
|
|
140
|
+
}
|
|
139
141
|
}
|
|
142
|
+
|
|
143
|
+
printTaskOutput(ctx, logger)
|
|
144
|
+
return false
|
|
140
145
|
}
|
|
141
146
|
|
|
142
|
-
|
|
143
|
-
|
|
147
|
+
// Probably a compilation error in the config js file. Pass it up to the outer error handler for logging.
|
|
148
|
+
throw runAllError
|
|
144
149
|
}
|
|
145
150
|
} catch (lintStagedError) {
|
|
146
151
|
if (lintStagedError === errConfigNotFound) {
|
package/lib/runAll.js
CHANGED
|
@@ -147,7 +147,8 @@ const runAll = async (
|
|
|
147
147
|
matchedFiles.add(file)
|
|
148
148
|
})
|
|
149
149
|
|
|
150
|
-
hasDeprecatedGitAdd =
|
|
150
|
+
hasDeprecatedGitAdd =
|
|
151
|
+
hasDeprecatedGitAdd || subTasks.some((subTask) => subTask.command === 'git add')
|
|
151
152
|
|
|
152
153
|
chunkListrTasks.push({
|
|
153
154
|
title: `Running tasks for ${task.pattern}`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lint-staged",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.1",
|
|
4
4
|
"description": "Lint files staged by git",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "https://github.com/okonet/lint-staged",
|
|
@@ -26,45 +26,40 @@
|
|
|
26
26
|
"test": "jest --coverage",
|
|
27
27
|
"test:watch": "jest --watch"
|
|
28
28
|
},
|
|
29
|
-
"husky": {
|
|
30
|
-
"hooks": {
|
|
31
|
-
"pre-commit": "./bin/lint-staged.js"
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
29
|
"dependencies": {
|
|
35
|
-
"chalk": "^4.1.
|
|
30
|
+
"chalk": "^4.1.1",
|
|
36
31
|
"cli-truncate": "^2.1.0",
|
|
37
|
-
"commander": "^
|
|
32
|
+
"commander": "^7.2.0",
|
|
38
33
|
"cosmiconfig": "^7.0.0",
|
|
39
|
-
"debug": "^4.
|
|
34
|
+
"debug": "^4.3.1",
|
|
40
35
|
"dedent": "^0.7.0",
|
|
41
36
|
"enquirer": "^2.3.6",
|
|
42
|
-
"execa": "^
|
|
43
|
-
"listr2": "^3.
|
|
44
|
-
"log-symbols": "^4.
|
|
45
|
-
"micromatch": "^4.0.
|
|
37
|
+
"execa": "^5.0.0",
|
|
38
|
+
"listr2": "^3.8.2",
|
|
39
|
+
"log-symbols": "^4.1.0",
|
|
40
|
+
"micromatch": "^4.0.4",
|
|
46
41
|
"normalize-path": "^3.0.0",
|
|
47
42
|
"please-upgrade-node": "^3.2.0",
|
|
48
43
|
"string-argv": "0.3.1",
|
|
49
44
|
"stringify-object": "^3.3.0"
|
|
50
45
|
},
|
|
51
46
|
"devDependencies": {
|
|
52
|
-
"@babel/core": "^7.
|
|
53
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.
|
|
54
|
-
"@babel/preset-env": "^7.
|
|
47
|
+
"@babel/core": "^7.14.0",
|
|
48
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
|
|
49
|
+
"@babel/preset-env": "^7.14.1",
|
|
55
50
|
"babel-eslint": "10.1.0",
|
|
56
|
-
"babel-jest": "^26.6.
|
|
51
|
+
"babel-jest": "^26.6.3",
|
|
57
52
|
"consolemock": "^1.1.0",
|
|
58
|
-
"eslint": "^7.
|
|
59
|
-
"eslint-config-prettier": "^
|
|
53
|
+
"eslint": "^7.25.0",
|
|
54
|
+
"eslint-config-prettier": "^8.3.0",
|
|
60
55
|
"eslint-plugin-import": "^2.22.1",
|
|
61
56
|
"eslint-plugin-node": "^11.1.0",
|
|
62
|
-
"eslint-plugin-prettier": "^3.
|
|
63
|
-
"fs-extra": "^
|
|
64
|
-
"husky": "^
|
|
65
|
-
"jest": "^26.6.
|
|
57
|
+
"eslint-plugin-prettier": "^3.4.0",
|
|
58
|
+
"fs-extra": "^10.0.0",
|
|
59
|
+
"husky": "^6.0.0",
|
|
60
|
+
"jest": "^26.6.3",
|
|
66
61
|
"jest-snapshot-serializer-ansi": "^1.0.0",
|
|
67
|
-
"prettier": "^2.1
|
|
62
|
+
"prettier": "^2.2.1"
|
|
68
63
|
},
|
|
69
64
|
"config": {
|
|
70
65
|
"commitizen": {
|