lint-staged 16.3.3 → 16.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
@@ -227,7 +227,7 @@ _Lint-staged_ can be configured in many ways:
227
227
  whether your project's _package.json_ contains the `"type": "module"` option or not.
228
228
  - Pass a configuration file using the `--config` or `-c` flag
229
229
 
230
- Configuration should be an object where each value is a **command** to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns. JavaScript files can also export advanced configuration as a function. See [Using JS configuration files](#using-js-configuration-files) for more info.
230
+ Configuration should be an object where each value is a **command** to run and its key is a glob pattern to use for this command. This package uses [picomatch](https://github.com/micromatch/picomatch) for glob patterns. JavaScript files can also export advanced configuration as a function. See [Using JS configuration files](#using-js-configuration-files) for more info.
231
231
 
232
232
  You can also place multiple configuration files in different directories inside a project. For a given staged file, the closest configuration file will always be used. See ["How to use `lint-staged` in a multi-package monorepo?"](#how-to-use-lint-staged-in-a-multi-package-monorepo) for more info and an example.
233
233
 
@@ -329,9 +329,9 @@ Or, if necessary, you can limit the concurrency using `--concurrent <number>` or
329
329
 
330
330
  ## Filtering files
331
331
 
332
- Task 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:
332
+ Task commands work on a subset of all staged files, defined by a _glob pattern_. lint-staged uses [picomatch](https://github.com/micromatch/picomatch) for matching files with the following rules:
333
333
 
334
- - If the glob pattern contains no slashes (`/`), micromatch's `matchBase` option will be enabled, so globs match a file's basename regardless of directory:
334
+ - If the glob pattern contains no slashes (`/`), picomatch's `matchBase` option will be enabled, so globs match a file's basename regardless of directory:
335
335
  - `"*.js"` will match all JS files, like `/test.js` and `/foo/bar/test.js`
336
336
  - `"!(*test).js"` will match all JS files, except those ending in `test.js`, so `foo.js` but not `foo.test.js`
337
337
  - `"!(*.css|*.js)"` will match all files except CSS and JS files
@@ -432,15 +432,15 @@ export default {
432
432
 
433
433
  ```js
434
434
  // lint-staged.config.js
435
- import micromatch from 'micromatch'
435
+ import picomatch from 'picomatch'
436
436
 
437
437
  export default (allStagedFiles) => {
438
- const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh'])
438
+ const shFiles = allStagedFiles.filter((file) => picomatch.isMatch(file, ['**/src/**/*.sh']))
439
439
  if (shFiles.length) {
440
440
  return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
441
441
  }
442
- const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts'])
443
- const docFiles = micromatch(allStagedFiles, ['**/*.md'])
442
+ const codeFiles = allStagedFiles.filter((file) => picomatch.isMatch(file, ['**/*.js', '**/*.ts']))
443
+ const docFiles = allStagedFiles.filter((file) => picomatch.isMatch(file, ['**/*.md']))
444
444
  return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
445
445
  }
446
446
  ```
@@ -499,12 +499,12 @@ It's better to use the [function-based configuration (seen above)](https://githu
499
499
 
500
500
  ```js
501
501
  // lint-staged.config.js
502
- import micromatch from 'micromatch'
502
+ import picomatch from 'picomatch'
503
503
 
504
504
  export default {
505
505
  '*': (allFiles) => {
506
- const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts'])
507
- const docFiles = micromatch(allFiles, ['**/*.md'])
506
+ const codeFiles = allFiles.filter((file) => picomatch.isMatch(file, ['**/*.js', '**/*.ts']))
507
+ const docFiles = allFiles.filter((file) => picomatch.isMatch(file, ['**/*.md']))
508
508
  return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`]
509
509
  },
510
510
  }
@@ -517,16 +517,16 @@ export default {
517
517
  <details>
518
518
  <summary>Click to expand</summary>
519
519
 
520
- If for some reason you want to ignore files from the glob match, you can use `micromatch.not()`:
520
+ If for some reason you want to ignore files from the glob match, you can use `picomatch.isMatch()` with negated pattern(s):
521
521
 
522
522
  ```js
523
523
  // lint-staged.config.js
524
- import micromatch from 'micromatch'
524
+ import picomatch from 'picomatch'
525
525
 
526
526
  export default {
527
527
  '*.js': (files) => {
528
528
  // from `files` filter those _NOT_ matching `*test.js`
529
- const match = micromatch.not(files, '*test.js')
529
+ const match = files.filter((file) => picomatch.isMatch(file, '!*test.js'))
530
530
  return `eslint ${match.join(' ')}`
531
531
  },
532
532
  }
@@ -1,8 +1,7 @@
1
1
  import path from 'node:path'
2
2
 
3
- import micromatch from 'micromatch'
4
-
5
3
  import { createDebug } from './debug.js'
4
+ import { matchFiles } from './matchFiles.js'
6
5
  import { normalizePath } from './normalizePath.js'
7
6
 
8
7
  const debugLog = createDebug('lint-staged:generateTasks')
@@ -30,36 +29,15 @@ export const generateTasks = ({ config, cwd = process.cwd(), files, relative = f
30
29
 
31
30
  // Only worry about children of the CWD unless the pattern explicitly
32
31
  // specifies that it concerns a parent directory.
33
- const filteredFiles = relativeFiles.filter((file) => {
32
+ const includedFiles = relativeFiles.filter((file) => {
34
33
  if (isParentDirPattern) return true
35
34
  return !file.filepath.startsWith('..') && !path.isAbsolute(file.filepath)
36
35
  })
37
36
 
38
- const matches = micromatch(
39
- filteredFiles.map((file) => file.filepath),
40
- pattern,
41
- {
42
- cwd,
43
- dot: true,
44
- // If the pattern doesn't look like a path, enable `matchBase` to
45
- // match against filenames in every directory. This makes `*.js`
46
- // match both `test.js` and `subdirectory/test.js`.
47
- matchBase: !pattern.includes('/'),
48
- posixSlashes: true,
49
- strictBrackets: true,
50
- }
51
- )
52
-
53
- const fileList = filteredFiles.flatMap((file) =>
54
- matches.includes(file.filepath)
55
- ? [
56
- {
57
- filepath: normalizePath(relative ? file.filepath : path.resolve(cwd, file.filepath)),
58
- status: file.status,
59
- },
60
- ]
61
- : []
62
- )
37
+ const fileList = matchFiles(includedFiles, pattern, cwd).map((file) => ({
38
+ filepath: normalizePath(relative ? file.filepath : path.resolve(cwd, file.filepath)),
39
+ status: file.status,
40
+ }))
63
41
 
64
42
  const task = { pattern, commands, fileList }
65
43
  debugLog('Generated task: \n%O', task)
@@ -0,0 +1,22 @@
1
+ import picomatch from 'picomatch'
2
+
3
+ /**
4
+ * Match list of files against a pattern.
5
+ *
6
+ * @param {string} pattern
7
+ * @param {import('./getStagedFiles.js').StagedFile[]} files
8
+ */
9
+ export const matchFiles = (files, pattern, cwd = process.cwd()) => {
10
+ const isMatch = picomatch(pattern, {
11
+ cwd,
12
+ dot: true,
13
+ // If the pattern doesn't look like a path, enable `matchBase` to
14
+ // match against filenames in every directory. This makes `*.js`
15
+ // match both `test.js` and `subdirectory/test.js`.
16
+ matchBase: !pattern.includes('/'),
17
+ posixSlashes: true,
18
+ strictBrackets: true,
19
+ })
20
+
21
+ return files.filter((file) => isMatch(file.filepath))
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "16.3.3",
3
+ "version": "16.4.0",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -50,16 +50,16 @@
50
50
  "dependencies": {
51
51
  "commander": "^14.0.3",
52
52
  "listr2": "^9.0.5",
53
- "micromatch": "^4.0.8",
53
+ "picomatch": "^4.0.3",
54
54
  "string-argv": "^0.3.2",
55
- "tinyexec": "^1.0.2",
55
+ "tinyexec": "^1.0.4",
56
56
  "yaml": "^2.8.2"
57
57
  },
58
58
  "devDependencies": {
59
- "@changesets/changelog-github": "0.5.2",
60
- "@changesets/cli": "2.29.8",
61
- "@commitlint/cli": "20.4.2",
62
- "@commitlint/config-conventional": "20.4.2",
59
+ "@changesets/changelog-github": "0.6.0",
60
+ "@changesets/cli": "2.30.0",
61
+ "@commitlint/cli": "20.4.4",
62
+ "@commitlint/config-conventional": "20.4.4",
63
63
  "@eslint/js": "10.0.1",
64
64
  "@vitest/coverage-v8": "4.0.18",
65
65
  "@vitest/eslint-plugin": "1.6.9",