lint-staged 10.2.13 → 10.4.2
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 +38 -7
- package/lib/formatConfig.js +7 -0
- package/lib/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,8 +97,11 @@ Options:
|
|
|
97
97
|
Starting with v3.1 you can now use different ways of configuring it:
|
|
98
98
|
|
|
99
99
|
- `lint-staged` object in your `package.json`
|
|
100
|
-
- `.lintstagedrc` file in JSON or YML format
|
|
101
|
-
-
|
|
100
|
+
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
|
|
101
|
+
- `.lintstagedrc.json`
|
|
102
|
+
- `.lintstagedrc.yaml`
|
|
103
|
+
- `.lintstagedrc.yml`
|
|
104
|
+
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in JS format
|
|
102
105
|
- Pass a configuration file using the `--config` or `-c` flag
|
|
103
106
|
|
|
104
107
|
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
|
|
@@ -189,14 +192,40 @@ For example:
|
|
|
189
192
|
|
|
190
193
|
going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
|
|
191
194
|
|
|
192
|
-
## Using JS
|
|
195
|
+
## Using JS configuration file
|
|
193
196
|
|
|
194
|
-
|
|
197
|
+
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
|
+
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 or command strings. These strings are considered complete and should include the filename arguments, if wanted.
|
|
200
|
+
|
|
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, 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
|
+
### Function signature
|
|
204
|
+
|
|
205
|
+
The function can also be async:
|
|
195
206
|
|
|
196
207
|
```ts
|
|
197
|
-
|
|
208
|
+
(filenames: string[]) => string | string[] | Promise<string | string[]>
|
|
198
209
|
```
|
|
199
210
|
|
|
211
|
+
### Example: Export a function to build your own matchers
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
// lint-staged.config.js
|
|
215
|
+
const micromatch = require('micromatch')
|
|
216
|
+
|
|
217
|
+
module.exports = (allStagedFiles) => {
|
|
218
|
+
const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh']);
|
|
219
|
+
if (shFiles.length) {
|
|
220
|
+
return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
|
|
221
|
+
}
|
|
222
|
+
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts']);
|
|
223
|
+
const docFiles = micromatch(allStagedFiles, ['**/*.md']);
|
|
224
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
|
|
200
229
|
### Example: Wrap filenames in single quotes and run once per file
|
|
201
230
|
|
|
202
231
|
```js
|
|
@@ -226,6 +255,7 @@ module.exports = {
|
|
|
226
255
|
```
|
|
227
256
|
|
|
228
257
|
### Example: Use your own globs
|
|
258
|
+
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.
|
|
229
259
|
|
|
230
260
|
```js
|
|
231
261
|
// lint-staged.config.js
|
|
@@ -233,8 +263,9 @@ const micromatch = require('micromatch')
|
|
|
233
263
|
|
|
234
264
|
module.exports = {
|
|
235
265
|
'*': (allFiles) => {
|
|
236
|
-
const
|
|
237
|
-
|
|
266
|
+
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts']);
|
|
267
|
+
const docFiles = micromatch(allFiles, ['**/*.md']);
|
|
268
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
|
|
238
269
|
}
|
|
239
270
|
}
|
|
240
271
|
```
|
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } = require('./
|
|
|
9
9
|
const printTaskOutput = require('./printTaskOutput')
|
|
10
10
|
const runAll = require('./runAll')
|
|
11
11
|
const { ApplyEmptyCommitError, GetBackupStashError, GitError } = require('./symbols')
|
|
12
|
+
const formatConfig = require('./formatConfig')
|
|
12
13
|
const validateConfig = require('./validateConfig')
|
|
13
14
|
|
|
14
15
|
const errConfigNotFound = new Error('Config could not be found')
|
|
@@ -30,7 +31,9 @@ function loadConfig(configPath) {
|
|
|
30
31
|
'.lintstagedrc.yaml',
|
|
31
32
|
'.lintstagedrc.yml',
|
|
32
33
|
'.lintstagedrc.js',
|
|
34
|
+
'.lintstagedrc.cjs',
|
|
33
35
|
'lint-staged.config.js',
|
|
36
|
+
'lint-staged.config.cjs',
|
|
34
37
|
],
|
|
35
38
|
})
|
|
36
39
|
|
|
@@ -88,7 +91,8 @@ module.exports = async function lintStaged(
|
|
|
88
91
|
debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
|
|
89
92
|
// resolved.config is the parsed configuration object
|
|
90
93
|
// resolved.filepath is the path to the config file that was found
|
|
91
|
-
const
|
|
94
|
+
const formattedConfig = formatConfig(resolved.config)
|
|
95
|
+
const config = validateConfig(formattedConfig)
|
|
92
96
|
if (debug) {
|
|
93
97
|
// Log using logger to be able to test through `consolemock`.
|
|
94
98
|
logger.log('Running lint-staged with the following config:')
|