You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-30Lines changed: 32 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -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 blogs posts and talks
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
-
It will install and configure [husky](https://github.com/typicode/husky) and lint-staged depending on code quality tools from `package.json` dependencies so please make sure you install (`npm install --save-dev`) and configure all code quality tools like [Prettier](https://prettier.io), [ESlint](https://eslint.org) prior that.
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
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_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 empty commit, please use the `--allow-empty` option.
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 it:
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_. `lint-staged´ uses [micromatch](https://github.com/micromatch/micromatch) for matching files with the following rules:
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, `lint-staged` will do the following
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 multipackage monorepo?](#how-to-use-lint-staged-in-a-multi-package-monorepo)
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) 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
+
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’t have it installed.
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 _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
+
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, and return a command string, or an array orcommand strings. These strings are considered complete and should include the filename arguments, if wanted.
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, 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
+
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 eslint on entire repo if more than 10 staged files
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 assuming you’ve already set up lint-staged in the `package.json` and [husky](https://github.com/typicode/husky).
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
{
@@ -375,7 +375,7 @@ In `.husky/pre-commit`
375
375
npx lint-staged
376
376
```
377
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._
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._
379
379
380
380
### ESLint with default parameters for `*.js` and `*.jsx` running as a pre-commit hook
### Automatically fix code style with `prettier` for javascript, typescript, markdown, HTML, or CSS
483
+
### Automatically fix code style with `prettier` for JavaScript, TypeScript, Markdown, HTML, or CSS
482
484
483
485
<details>
484
486
<summary>Click to expand</summary>
@@ -652,7 +654,7 @@ _Thanks to [this comment](https://youtrack.jetbrains.com/issue/IDEA-135454#comme
652
654
653
655
</details>
654
656
655
-
### How to use `lint-staged` in a multipackage monorepo?
657
+
### How to use `lint-staged` in a multi-package monorepo?
656
658
657
659
<details>
658
660
<summary>Click to expand</summary>
@@ -684,7 +686,7 @@ Example repo: [sudo-suhas/lint-staged-django-react-demo](https://github.com/sudo
684
686
685
687
</details>
686
688
687
-
### How can i ignore files from `.eslintignore`?
689
+
### How can I ignore files from `.eslintignore`?
688
690
689
691
<details>
690
692
<summary>Click to expand</summary>
@@ -715,16 +717,16 @@ module.exports = {
715
717
716
718
</details>
717
719
718
-
#### ESlint >= 7
720
+
#### ESLint >= 7
719
721
720
722
<details>
721
723
<summary>Click to expand</summary>
722
724
723
-
In versions of ESlint> 7, [isPathIgnored](https://eslint.org/docs/developer-guide/nodejs-api#-eslintispathignoredfilepath) is an async functionand now returns a promise. The code below can be used to reinstate the above functionality.
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.
724
726
725
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.
726
728
727
-
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.
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.
0 commit comments