lint-staged 10.3.0 → 10.4.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 CHANGED
@@ -189,14 +189,40 @@ For example:
189
189
 
190
190
  going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
191
191
 
192
- ## Using JS functions to customize tasks
192
+ ## Using JS configuration file
193
193
 
194
- When supplying configuration in JS format it is possible to define the task as a function, which will receive an array of staged filenames/paths and should return the complete command as a string. It is also possible to return an array of complete command strings, for example when the task supports only a single file input. The function can be either sync or async.
194
+ 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.
195
+
196
+ 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.
197
+
198
+ 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.
199
+
200
+ ### Function signature
201
+
202
+ The function can also be async:
195
203
 
196
204
  ```ts
197
- type TaskFn = (filenames: string[]) => string | string[] | Promise<string | string[]>
205
+ (filenames: string[]) => string | string[] | Promise<string | string[]>
198
206
  ```
199
207
 
208
+ ### Example: Export a function to build your own matchers
209
+
210
+ ```js
211
+ // lint-staged.config.js
212
+ const micromatch = require('micromatch')
213
+
214
+ module.exports = (allStagedFiles) => {
215
+ const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh']);
216
+ if (shFiles.length) {
217
+ return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
218
+ }
219
+ const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts']);
220
+ const docFiles = micromatch(allStagedFiles, ['**/*.md']);
221
+ return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
222
+ }
223
+ ```
224
+
225
+
200
226
  ### Example: Wrap filenames in single quotes and run once per file
201
227
 
202
228
  ```js
@@ -226,6 +252,7 @@ module.exports = {
226
252
  ```
227
253
 
228
254
  ### Example: Use your own globs
255
+ 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
256
 
230
257
  ```js
231
258
  // lint-staged.config.js
@@ -233,8 +260,9 @@ const micromatch = require('micromatch')
233
260
 
234
261
  module.exports = {
235
262
  '*': (allFiles) => {
236
- const match = micromatch(allFiles, ['*.js', '*.ts'])
237
- return `eslint ${match.join(' ')}`
263
+ const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts']);
264
+ const docFiles = micromatch(allFiles, ['**/*.md']);
265
+ return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
238
266
  }
239
267
  }
240
268
  ```
@@ -0,0 +1,7 @@
1
+ module.exports = function formatConfig(config) {
2
+ if (typeof config === 'function') {
3
+ return { '*': config }
4
+ }
5
+
6
+ return config
7
+ }
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')
@@ -88,7 +89,8 @@ module.exports = async function lintStaged(
88
89
  debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
89
90
  // resolved.config is the parsed configuration object
90
91
  // resolved.filepath is the path to the config file that was found
91
- const config = validateConfig(resolved.config)
92
+ const formattedConfig = formatConfig(resolved.config)
93
+ const config = validateConfig(formattedConfig)
92
94
  if (debug) {
93
95
  // Log using logger to be able to test through `consolemock`.
94
96
  logger.log('Running lint-staged with the following config:')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "10.3.0",
3
+ "version": "10.4.0",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/okonet/lint-staged",