lint-staged 16.0.0 → 16.1.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
@@ -34,13 +34,28 @@ $ git commit
34
34
 
35
35
  </details>
36
36
 
37
+ ## Table of Contents
38
+
39
+ - [Why](#why)
40
+ - [Installation and setup](#installation-and-setup)
41
+ - [Changelog](#changelog)
42
+ - [Command line flags](#command-line-flags)
43
+ - [Configuration](#configuration)
44
+ - [Filtering files](#filtering-files)
45
+ - [What commands are supported?](#what-commands-are-supported)
46
+ - [Running multiple commands in a sequence](#running-multiple-commands-in-a-sequence)
47
+ - [Using JS configuration files](#using-js-configuration-files)
48
+ - [Reformatting the code](#reformatting-the-code)
49
+ - [Examples](#examples)
50
+ - [Frequently Asked Questions](#frequently-asked-questions)
51
+
37
52
  ## Why
38
53
 
39
54
  Code quality tasks like formatters and linters make 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 task on a whole project can be slow, and opinionated tasks such as linting can sometimes produce irrelevant results. Ultimately you only want to check files that will be committed.
40
55
 
41
56
  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.
42
57
 
43
- ## Related blog posts and talks
58
+ ### Related blog posts and talks
44
59
 
45
60
  - [Introductory Medium post - Andrey Okonetchnikov, 2016](https://medium.com/@okonetchnikov/make-linting-great-again-f3890e1ad6b8#.8qepn2b5l)
46
61
  - [Running Jest Tests Before Each Git Commit - Ben McCormick, 2017](https://benmccormick.org/2017/02/26/running-jest-tests-before-each-git-commit/)
@@ -70,7 +85,7 @@ Now change a few files, `git add` or `git add --patch` some of them to your comm
70
85
 
71
86
  See [examples](#examples) and [configuration](#configuration) for more information.
72
87
 
73
- > [!CAUTION]
88
+ > [!CAUTION]
74
89
  > _Lint-staged_ runs `git` operations affecting the files in your repository. By default _lint-staged_ creates a `git stash` as a backup of the original state before running any configured tasks to help prevent data loss.
75
90
 
76
91
  ## Changelog
@@ -94,13 +109,12 @@ Options:
94
109
  -c, --config [path] path to configuration file, or - to read from stdin
95
110
  --cwd [path] run all tasks in specific directory, instead of the current
96
111
  -d, --debug print additional debug information (default: false)
97
- --diff [string] override the default "--staged" flag of "git diff" to get list of files.
98
- Implies "--no-stash".
99
- --diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of
100
- files
112
+ --diff [string] override the default "--staged" flag of "git diff" to get list of files. Implies
113
+ "--no-stash".
114
+ --diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of files
101
115
  --max-arg-length [number] maximum length of the command-line argument string (default: 0)
102
- --no-stash disable the backup stash, and do not revert in case of errors. Implies
103
- "--no-hide-partially-staged".
116
+ --no-revert do not revert to original state in case of errors.
117
+ --no-stash disable the backup stash. Implies "--no-revert".
104
118
  --no-hide-partially-staged disable hiding unstaged changes from partially staged files
105
119
  -q, --quiet disable lint-staged’s own console output (default: false)
106
120
  -r, --relative pass relative filepaths to tasks (default: false)
@@ -129,10 +143,11 @@ Any lost modifications can be restored from a git stash:
129
143
  - **`--diff`**: By default tasks are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`.
130
144
  - **`--diff-filter`**: By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203).
131
145
  - **`--max-arg-length`**: long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
132
- - **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash`. This option also implies `--no-hide-partially-staged`.
146
+ - **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. This option also implies `--no-hide-partially-staged`.
133
147
  - **`--no-hide-partially-staged`**: By default, unstaged changes from partially staged files will be hidden. This option will disable this behavior and include all unstaged changes in partially staged files. Can be re-enabled with `--hide-partially-staged`
134
148
  - **`--quiet`**: Supress all CLI output, except from tasks.
135
149
  - **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
150
+ - **`--no-revert`**: By default all task modifications will be reverted in case of an error. This option will disable the behavior, and apply task modifications to the index before aborting the commit.
136
151
  - **`--verbose`**: Show task output even when tasks succeed. By default only failed output is shown.
137
152
 
138
153
  ## Configuration
@@ -998,7 +1013,7 @@ ESLint v8.51.0 introduced [`--no-warn-ignored` CLI flag](https://eslint.org/docs
998
1013
 
999
1014
  When running `lint-staged` via Husky hooks, TypeScript may ignore `tsconfig.json`, leading to errors like:
1000
1015
 
1001
- > **TS17004:** Cannot use JSX unless the '--jsx' flag is provided.
1016
+ > **TS17004:** Cannot use JSX unless the '--jsx' flag is provided.
1002
1017
  > **TS1056:** Accessors are only available when targeting ECMAScript 5 and higher.
1003
1018
 
1004
1019
  See issue [#825](https://github.com/okonet/lint-staged/issues/825) for more details.
@@ -61,30 +61,26 @@ program.option(
61
61
  program.option('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
62
62
 
63
63
  /**
64
- * We don't want to show the `--stash` flag because it's on by default, and only show the
65
- * negatable flag `--no-stash` in stead. There seems to be a bug in Commander.js where
64
+ * We don't want to show the `--revert` flag because it's on by default, and only show the
65
+ * negatable flag `--no-rever` instead. There seems to be a bug in Commander.js where
66
66
  * configuring only the latter won't actually set the default value.
67
67
  */
68
68
  program
69
69
  .addOption(
70
- new Option('--stash', 'enable the backup stash, and revert in case of errors')
71
- .default(true)
72
- .hideHelp()
70
+ new Option('--revert', 'revert to original state in case of errors').default(true).hideHelp()
73
71
  )
74
72
  .addOption(
75
- new Option(
76
- '--no-stash',
77
- 'disable the backup stash, and do not revert in case of errors. Implies "--no-hide-partially-staged".'
78
- )
73
+ new Option('--no-revert', 'do not revert to original state in case of errors.').default(false)
74
+ )
75
+
76
+ program
77
+ .addOption(new Option('--stash', 'enable the backup stash').default(true).hideHelp())
78
+ .addOption(
79
+ new Option('--no-stash', 'disable the backup stash. Implies "--no-revert".')
79
80
  .default(false)
80
- .implies({ hidePartiallyStaged: false })
81
+ .implies({ revert: false, hidePartiallyStaged: false })
81
82
  )
82
83
 
83
- /**
84
- * We don't want to show the `--hide-partially-staged` flag because it's on by default, and only show the
85
- * negatable flag `--no-hide-partially-staged` in stead. There seems to be a bug in Commander.js where
86
- * configuring only the latter won't actually set the default value.
87
- */
88
84
  program
89
85
  .addOption(
90
86
  new Option('--hide-partially-staged', 'hide unstaged changes from partially staged files')
@@ -127,6 +123,7 @@ const options = {
127
123
  maxArgLength: cliOptions.maxArgLength || undefined,
128
124
  quiet: !!cliOptions.quiet,
129
125
  relative: !!cliOptions.relative,
126
+ revert: !!cliOptions.revert, // commander inverts `no-<x>` flags to `!x`
130
127
  stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
131
128
  hidePartiallyStaged: !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
132
129
  verbose: !!cliOptions.verbose,
@@ -38,10 +38,10 @@ export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } =
38
38
  const [, dstMode, , , ,] = info.split(' ')
39
39
 
40
40
  /**
41
- * Filter out submodule root directory. "160000" is the object mode for submodules.
42
- * @see https://github.com/git/git/blob/485f5f863615e670fd97ae40af744e14072cfe18/object.h#L114-L120
41
+ * Filter out submodules and symlinks
42
+ * @see https://github.com/git/git/blob/cb96e1697ad6e54d11fc920c95f82977f8e438f8/Documentation/git-fast-import.adoc?plain=1#L634-L646
43
43
  */
44
- if (dstMode === '160000') {
44
+ if (dstMode === '160000' || dstMode === '120000') {
45
45
  return []
46
46
  }
47
47
 
package/lib/index.d.ts CHANGED
@@ -66,6 +66,11 @@ export type Options = {
66
66
  * @default false
67
67
  */
68
68
  relative?: boolean
69
+ /**
70
+ * Revert to original state in case of errors
71
+ * @default true
72
+ */
73
+ revert?: boolean
69
74
  /**
70
75
  * Enable the backup stash, and revert in case of errors.
71
76
  * @warn Disabling this also implies `hidePartiallyStaged: false`.
package/lib/index.js CHANGED
@@ -57,6 +57,7 @@ const getMaxArgLength = () => {
57
57
  * @param {number} [options.maxArgLength] - Maximum argument string length
58
58
  * @param {boolean} [options.quiet] - Disable lint-staged’s own console output
59
59
  * @param {boolean} [options.relative] - Pass relative filepaths to tasks
60
+ * @param {boolean} [options.revert] - revert to original state in case of errors
60
61
  * @param {boolean} [options.stash] - Enable the backup stash, and revert in case of errors
61
62
  * @param {boolean} [options.verbose] - Show task output even when tasks succeed; by default only failed output is shown
62
63
  * @param {Logger} [logger]
@@ -78,6 +79,8 @@ const lintStaged = async (
78
79
  relative = false,
79
80
  // Stashing should be disabled by default when the `diff` option is used
80
81
  stash = diff === undefined,
82
+ // Cannot revert to original state without stash
83
+ revert = stash,
81
84
  hidePartiallyStaged = stash,
82
85
  verbose = false,
83
86
  } = {},
@@ -110,6 +113,7 @@ const lintStaged = async (
110
113
  maxArgLength,
111
114
  quiet,
112
115
  relative,
116
+ revert,
113
117
  stash,
114
118
  hidePartiallyStaged,
115
119
  verbose,
package/lib/runAll.js CHANGED
@@ -59,6 +59,7 @@ const createError = (ctx) => Object.assign(new Error('lint-staged failed'), { ct
59
59
  * @param {number} [options.maxArgLength] - Maximum argument string length
60
60
  * @param {boolean} [options.quiet] - Disable lint-staged’s own console output
61
61
  * @param {boolean} [options.relative] - Pass relative filepaths to tasks
62
+ * @param {boolean} [options.revert] - revert to original state in case of errors
62
63
  * @param {boolean} [options.stash] - Enable the backup stash, and revert in case of errors
63
64
  * @param {boolean} [options.verbose] - Show task output even when tasks succeed; by default only failed output is shown
64
65
  * @param {Logger} logger
@@ -79,6 +80,8 @@ export const runAll = async (
79
80
  relative = false,
80
81
  // Stashing should be disabled by default when the `diff` option is used
81
82
  stash = diff === undefined,
83
+ // Cannot revert to original state without stash
84
+ revert = stash,
82
85
  hidePartiallyStaged = stash,
83
86
  verbose = false,
84
87
  },
@@ -91,7 +94,7 @@ export const runAll = async (
91
94
  cwd = hasExplicitCwd ? path.resolve(cwd) : process.cwd()
92
95
  debugLog('Using working directory `%s`', cwd)
93
96
 
94
- const ctx = getInitialState({ quiet })
97
+ const ctx = getInitialState({ quiet, revert })
95
98
 
96
99
  const { topLevelDir, gitConfigDir } = await resolveGitRepo(cwd)
97
100
  if (!topLevelDir) {
package/lib/state.js CHANGED
@@ -8,9 +8,10 @@ import {
8
8
  TaskError,
9
9
  } from './symbols.js'
10
10
 
11
- export const getInitialState = ({ quiet = false } = {}) => ({
11
+ export const getInitialState = ({ quiet = false, revert = true } = {}) => ({
12
12
  hasPartiallyStagedFiles: null,
13
13
  shouldBackup: null,
14
+ shouldRevert: revert,
14
15
  backupHash: null,
15
16
  shouldHidePartiallyStaged: true,
16
17
  errors: new Set([]),
@@ -23,8 +24,8 @@ export const shouldHidePartiallyStagedFiles = (ctx) =>
23
24
  ctx.hasPartiallyStagedFiles && ctx.shouldHidePartiallyStaged
24
25
 
25
26
  export const applyModificationsSkipped = (ctx) => {
26
- // Always apply back unstaged modifications when skipping backup
27
- if (!ctx.shouldBackup) return false
27
+ // Always apply back unstaged modifications when skipping revert or backup
28
+ if (!ctx.shouldRevert || !ctx.shouldBackup) return false
28
29
  // Should be skipped in case of git errors
29
30
  if (ctx.errors.has(GitError)) {
30
31
  return GIT_ERROR
@@ -48,7 +49,9 @@ export const restoreUnstagedChangesSkipped = (ctx) => {
48
49
  }
49
50
 
50
51
  export const restoreOriginalStateEnabled = (ctx) =>
51
- ctx.shouldBackup && (ctx.errors.has(TaskError) || ctx.errors.has(RestoreUnstagedChangesError))
52
+ !!ctx.shouldRevert &&
53
+ !!ctx.shouldBackup &&
54
+ (ctx.errors.has(TaskError) || ctx.errors.has(RestoreUnstagedChangesError))
52
55
 
53
56
  export const restoreOriginalStateSkipped = (ctx) => {
54
57
  // Should be skipped in case of unknown git errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "16.0.0",
3
+ "version": "16.1.0",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "url": "https://opencollective.com/lint-staged"
22
22
  },
23
23
  "engines": {
24
- "node": ">=20.18"
24
+ "node": ">=20.17"
25
25
  },
26
26
  "type": "module",
27
27
  "bin": {
@@ -48,25 +48,25 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "chalk": "^5.4.1",
51
- "commander": "^13.1.0",
52
- "debug": "^4.4.0",
51
+ "commander": "^14.0.0",
52
+ "debug": "^4.4.1",
53
53
  "lilconfig": "^3.1.3",
54
54
  "listr2": "^8.3.3",
55
55
  "micromatch": "^4.0.8",
56
- "nano-spawn": "^1.0.0",
56
+ "nano-spawn": "^1.0.2",
57
57
  "pidtree": "^0.6.0",
58
58
  "string-argv": "^0.3.2",
59
- "yaml": "^2.7.1"
59
+ "yaml": "^2.8.0"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@changesets/changelog-github": "0.5.1",
63
- "@changesets/cli": "2.29.3",
63
+ "@changesets/cli": "2.29.4",
64
64
  "@commitlint/cli": "19.8.1",
65
65
  "@commitlint/config-conventional": "19.8.1",
66
- "@eslint/js": "9.26.0",
66
+ "@eslint/js": "9.27.0",
67
67
  "consolemock": "1.1.0",
68
68
  "cross-env": "7.0.3",
69
- "eslint": "9.26.0",
69
+ "eslint": "9.27.0",
70
70
  "eslint-config-prettier": "10.1.5",
71
71
  "eslint-plugin-jest": "28.11.0",
72
72
  "eslint-plugin-n": "17.18.0",
@@ -77,7 +77,7 @@
77
77
  "jest-snapshot-serializer-ansi": "2.2.1",
78
78
  "mock-stdin": "1.0.0",
79
79
  "prettier": "3.5.3",
80
- "semver": "7.7.1",
80
+ "semver": "7.7.2",
81
81
  "typescript": "5.8.3"
82
82
  },
83
83
  "keywords": [