lint-staged 10.5.4 → 11.0.0
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 +42 -36
- package/bin/lint-staged.js +12 -10
- package/package.json +19 -24
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@ 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/)
|
|
@@ -22,23 +22,23 @@ This project contains a script that will run arbitrary shell tasks with a list o
|
|
|
22
22
|
|
|
23
23
|
## Installation and setup
|
|
24
24
|
|
|
25
|
-
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:
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
npx mrm lint-staged
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
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.
|
|
32
32
|
|
|
33
|
-
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!
|
|
34
34
|
|
|
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
|
+
Now change a few files, `git add` or `git add --patch` some of them to your commit, and try to `git commit` them.
|
|
36
36
|
|
|
37
37
|
See [examples](#examples) and [configuration](#configuration) for more information.
|
|
38
38
|
|
|
39
39
|
## Changelog
|
|
40
40
|
|
|
41
|
-
See [Releases](https://github.com/okonet/lint-staged/releases)
|
|
41
|
+
See [Releases](https://github.com/okonet/lint-staged/releases).
|
|
42
42
|
|
|
43
43
|
### Migration
|
|
44
44
|
|
|
@@ -48,10 +48,10 @@ See [Releases](https://github.com/okonet/lint-staged/releases)
|
|
|
48
48
|
If your task previously contained a `git add` step, please remove this.
|
|
49
49
|
The automatic behaviour ensures there are less race-conditions,
|
|
50
50
|
since trying to run multiple git operations at the same time usually results in an error.
|
|
51
|
-
- From `v10.0.0` onwards
|
|
52
|
-
Since git stashes require at least an initial commit, you shouldn't run
|
|
53
|
-
- From `v10.0.0` onwards
|
|
54
|
-
- 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.
|
|
55
55
|
|
|
56
56
|
## Command line flags
|
|
57
57
|
|
|
@@ -79,7 +79,7 @@ Options:
|
|
|
79
79
|
```
|
|
80
80
|
|
|
81
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.
|
|
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 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 -`.
|
|
83
83
|
- **`--debug`**: Run in debug mode. When set, it does the following:
|
|
84
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*`.
|
|
85
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.
|
|
@@ -95,7 +95,7 @@ Options:
|
|
|
95
95
|
|
|
96
96
|
## Configuration
|
|
97
97
|
|
|
98
|
-
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:
|
|
99
99
|
|
|
100
100
|
- `lint-staged` object in your `package.json`
|
|
101
101
|
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
|
|
@@ -135,7 +135,7 @@ So, considering you did `git add file1.ext file2.ext`, lint-staged will run the
|
|
|
135
135
|
|
|
136
136
|
## Filtering files
|
|
137
137
|
|
|
138
|
-
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:
|
|
139
139
|
|
|
140
140
|
- If the glob pattern contains no slashes (`/`), micromatch's `matchBase` option will enabled, so globs match a file's basename regardless of directory:
|
|
141
141
|
- **`"*.js"`** will match all JS files, like `/test.js` and `/foo/bar/test.js`
|
|
@@ -144,7 +144,7 @@ Linter commands work on a subset of all staged files, defined by a _glob pattern
|
|
|
144
144
|
- **`"./*.js"`** will match all JS files in the git repo root, so `/test.js` but not `/foo/bar/test.js`
|
|
145
145
|
- **`"foo/**/\*.js"`** will match all JS files inside the`/foo`directory, so`/foo/bar/test.js`but not`/test.js`
|
|
146
146
|
|
|
147
|
-
When matching,
|
|
147
|
+
When matching, lint-staged will do the following
|
|
148
148
|
|
|
149
149
|
- Resolve the git root automatically, no configuration needed.
|
|
150
150
|
- Pick the staged files which are present inside the project directory.
|
|
@@ -153,11 +153,11 @@ When matching, `lint-staged` will do the following
|
|
|
153
153
|
|
|
154
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).
|
|
155
155
|
|
|
156
|
-
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)
|
|
157
157
|
|
|
158
158
|
### Ignoring files
|
|
159
159
|
|
|
160
|
-
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.
|
|
161
161
|
|
|
162
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.
|
|
163
163
|
|
|
@@ -167,7 +167,7 @@ In advanced scenarios, where it is impossible to configure the linter task itsel
|
|
|
167
167
|
|
|
168
168
|
Supported are any executables installed locally or globally via `npm` as well as any executable from your \$PATH.
|
|
169
169
|
|
|
170
|
-
> 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.
|
|
171
171
|
|
|
172
172
|
`lint-staged` uses [execa](https://github.com/sindresorhus/execa#preferlocal) to locate locally installed scripts. So in your `.lintstagedrc` you can write:
|
|
173
173
|
|
|
@@ -195,11 +195,11 @@ going to execute `eslint` and if it exits with `0` code, it will execute `pretti
|
|
|
195
195
|
|
|
196
196
|
## Using JS configuration file
|
|
197
197
|
|
|
198
|
-
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.
|
|
199
199
|
|
|
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
|
|
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.
|
|
201
201
|
|
|
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
|
|
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.
|
|
203
203
|
|
|
204
204
|
### Function signature
|
|
205
205
|
|
|
@@ -259,7 +259,7 @@ module.exports = {
|
|
|
259
259
|
|
|
260
260
|
</details>
|
|
261
261
|
|
|
262
|
-
### Example: Run
|
|
262
|
+
### Example: Run ESLint on entire repo if more than 10 staged files
|
|
263
263
|
|
|
264
264
|
<details>
|
|
265
265
|
<summary>Click to expand</summary>
|
|
@@ -353,7 +353,7 @@ Prior to version 10, tasks had to manually include `git add` as the final step.
|
|
|
353
353
|
|
|
354
354
|
## Examples
|
|
355
355
|
|
|
356
|
-
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.
|
|
357
357
|
|
|
358
358
|
```json
|
|
359
359
|
{
|
|
@@ -362,16 +362,20 @@ All examples assuming you’ve already set up lint-staged and husky in the `pack
|
|
|
362
362
|
"scripts": {
|
|
363
363
|
"my-custom-script": "linter --arg1 --arg2"
|
|
364
364
|
},
|
|
365
|
-
"husky": {
|
|
366
|
-
"hooks": {
|
|
367
|
-
"pre-commit": "lint-staged"
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
365
|
"lint-staged": {}
|
|
371
366
|
}
|
|
372
367
|
```
|
|
373
368
|
|
|
374
|
-
|
|
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._
|
|
375
379
|
|
|
376
380
|
### ESLint with default parameters for `*.js` and `*.jsx` running as a pre-commit hook
|
|
377
381
|
|
|
@@ -468,13 +472,15 @@ module.exports = (allStagedFiles) => {
|
|
|
468
472
|
allStagedFiles,
|
|
469
473
|
prettierSupportedExtensions.map((extension) => `**/*${extension}`)
|
|
470
474
|
)
|
|
471
|
-
return
|
|
475
|
+
return prettierFiles.length > 0
|
|
476
|
+
? [`prettier --write ${prettierFiles.map(addQuotes).join(' ')}`]
|
|
477
|
+
: []
|
|
472
478
|
}
|
|
473
479
|
```
|
|
474
480
|
|
|
475
481
|
</details>
|
|
476
482
|
|
|
477
|
-
### Automatically fix code style with `prettier` for
|
|
483
|
+
### Automatically fix code style with `prettier` for JavaScript, TypeScript, Markdown, HTML, or CSS
|
|
478
484
|
|
|
479
485
|
<details>
|
|
480
486
|
<summary>Click to expand</summary>
|
|
@@ -648,7 +654,7 @@ _Thanks to [this comment](https://youtrack.jetbrains.com/issue/IDEA-135454#comme
|
|
|
648
654
|
|
|
649
655
|
</details>
|
|
650
656
|
|
|
651
|
-
### How to use `lint-staged` in a multi
|
|
657
|
+
### How to use `lint-staged` in a multi-package monorepo?
|
|
652
658
|
|
|
653
659
|
<details>
|
|
654
660
|
<summary>Click to expand</summary>
|
|
@@ -680,7 +686,7 @@ Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo
|
|
|
680
686
|
|
|
681
687
|
</details>
|
|
682
688
|
|
|
683
|
-
### How can
|
|
689
|
+
### How can I ignore files from `.eslintignore`?
|
|
684
690
|
|
|
685
691
|
<details>
|
|
686
692
|
<summary>Click to expand</summary>
|
|
@@ -711,16 +717,16 @@ module.exports = {
|
|
|
711
717
|
|
|
712
718
|
</details>
|
|
713
719
|
|
|
714
|
-
####
|
|
720
|
+
#### ESLint >= 7
|
|
715
721
|
|
|
716
722
|
<details>
|
|
717
723
|
<summary>Click to expand</summary>
|
|
718
724
|
|
|
719
|
-
In versions of
|
|
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.
|
|
720
726
|
|
|
721
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.
|
|
722
728
|
|
|
723
|
-
Since [10.5.3](https://github.com/okonet/lint-staged/releases), any errors due to a bad
|
|
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.
|
|
724
730
|
|
|
725
731
|
```js
|
|
726
732
|
const { ESLint } = require('eslint')
|
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: JSON.parse(
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lint-staged",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
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": {
|