lint-staged 16.2.1 → 16.2.3

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.
@@ -6,7 +6,7 @@ import { Option, program } from 'commander'
6
6
 
7
7
  import { createDebug, enableDebug } from '../lib/debug.js'
8
8
  import lintStaged from '../lib/index.js'
9
- import { CONFIG_STDIN_ERROR, RESTORE_STASH_EXAMPLE } from '../lib/messages.js'
9
+ import { CONFIG_STDIN_ERROR, restoreStashExample } from '../lib/messages.js'
10
10
  import { readStdin } from '../lib/readStdin.js'
11
11
  import { getVersion } from '../lib/version.js'
12
12
 
@@ -120,7 +120,7 @@ program
120
120
  ).default(false)
121
121
  )
122
122
 
123
- .addHelpText('afterAll', '\n' + RESTORE_STASH_EXAMPLE)
123
+ .addHelpText('afterAll', '\n' + restoreStashExample())
124
124
 
125
125
  const cliOptions = program.parse(process.argv).opts()
126
126
 
@@ -9,7 +9,7 @@ import { getDiffCommand } from './getDiffCommand.js'
9
9
  import { parseGitZOutput } from './parseGitZOutput.js'
10
10
  import {
11
11
  ApplyEmptyCommitError,
12
- ExitCodeError,
12
+ FailOnChangesError,
13
13
  GetBackupStashError,
14
14
  GitError,
15
15
  HideUnstagedChangesError,
@@ -277,15 +277,6 @@ export class GitWorkflow {
277
277
  task.title = `Backed up original state in git stash (${ctx.backupHash})`
278
278
  debugLog(task.title)
279
279
  }
280
-
281
- if (this.failOnChanges) {
282
- debugLog(
283
- 'Calculating SHA-256 hash of unstaged changes because "--fail-on-changes" was used...'
284
- )
285
- const diff = await this.execGit(['diff', '--patch', '--unified=0'])
286
- this.unstagedDiffSha256 = calculateSha256(diff)
287
- debugLog('SHA-256 hash of unstaged changes is %S', this.unstagedDiffSha256)
288
- }
289
280
  } catch (error) {
290
281
  handleError(error, ctx)
291
282
  }
@@ -304,20 +295,33 @@ export class GitWorkflow {
304
295
  }
305
296
  }
306
297
 
298
+ async runTasks(ctx, task, { listrTasks, concurrent }) {
299
+ if (ctx.shouldFailOnChanges) {
300
+ debugLog(
301
+ 'Calculating SHA-256 hash of unstaged changes because "--fail-on-changes" was used...'
302
+ )
303
+ const diff = await this.execGit(['diff', '--patch', '--unified=0'])
304
+ ctx.unstagedDiffSha256 = calculateSha256(diff)
305
+ debugLog('SHA-256 hash of unstaged changes is %s', ctx.unstagedDiffSha256)
306
+ }
307
+
308
+ return task.newListr(listrTasks, { concurrent })
309
+ }
310
+
307
311
  /**
308
312
  * Applies back task modifications, and unstaged changes hidden in the stash.
309
313
  * In case of a merge-conflict retry with 3-way merge.
310
314
  */
311
315
  async applyModifications(ctx) {
312
- if (this.failOnChanges) {
316
+ if (ctx.shouldFailOnChanges) {
313
317
  debugLog(
314
318
  'Calculating SHA-256 hash of changes after tasks because "--fail-on-changes" was used...'
315
319
  )
316
320
  const diff = await this.execGit(['diff', '--patch', '--unified=0'])
317
321
  const diffSha256 = calculateSha256(diff)
318
- debugLog('SHA-256 hash of changes after tasks is %S', this.diffSha256)
319
- if (this.unstagedDiffSha256 !== diffSha256) {
320
- ctx.errors.add(ExitCodeError)
322
+ debugLog('SHA-256 hash of changes after tasks is %s', diffSha256)
323
+ if (ctx.unstagedDiffSha256 !== diffSha256) {
324
+ ctx.errors.add(FailOnChangesError)
321
325
  throw new Error('Tasks modified files and --fail-on-changes was used!')
322
326
  }
323
327
  }
package/lib/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  NO_CONFIGURATION,
7
7
  PREVENTED_EMPTY_COMMIT,
8
8
  PREVENTED_TASK_MODIFICATIONS,
9
- RESTORE_STASH_EXAMPLE,
9
+ restoreStashExample,
10
10
  UNSTAGED_CHANGES_BACKUP_STASH_LOCATION,
11
11
  } from './messages.js'
12
12
  import { printTaskOutput } from './printTaskOutput.js'
@@ -15,7 +15,7 @@ import { cleanupSkipped } from './state.js'
15
15
  import {
16
16
  ApplyEmptyCommitError,
17
17
  ConfigNotFoundError,
18
- ExitCodeError,
18
+ FailOnChangesError,
19
19
  GetBackupStashError,
20
20
  GitError,
21
21
  RestoreUnstagedChangesError,
@@ -155,8 +155,9 @@ const lintStaged = async (
155
155
  logger.error(NO_CONFIGURATION)
156
156
  } else if (ctx.errors.has(ApplyEmptyCommitError)) {
157
157
  logger.warn(PREVENTED_EMPTY_COMMIT)
158
- } else if (ctx.errors.has(ExitCodeError)) {
159
- logger.warn(PREVENTED_TASK_MODIFICATIONS)
158
+ } else if (ctx.errors.has(FailOnChangesError)) {
159
+ logger.warn(PREVENTED_TASK_MODIFICATIONS + '\n')
160
+ logger.warn(restoreStashExample(ctx.backupHash))
160
161
  } else if (ctx.errors.has(RestoreUnstagedChangesError)) {
161
162
  logger.warn(UNSTAGED_CHANGES_BACKUP_STASH_LOCATION)
162
163
  logger.warn(ctx.unstagedPatch)
@@ -167,7 +168,7 @@ const lintStaged = async (
167
168
  logger.error(GIT_ERROR)
168
169
  if (ctx.shouldBackup) {
169
170
  // No sense to show this if the backup stash itself is missing.
170
- logger.error(RESTORE_STASH_EXAMPLE + '\n')
171
+ logger.error(restoreStashExample(ctx.backupHash) + '\n')
171
172
  }
172
173
  }
173
174
 
package/lib/messages.js CHANGED
@@ -20,11 +20,11 @@ export const incorrectBraces = (before, after) =>
20
20
  `
21
21
  )
22
22
 
23
- export const NO_CONFIGURATION = `${error} No valid configuration found.`
23
+ export const NO_CONFIGURATION = `${error} lint-staged could not find any valid configuration.`
24
24
 
25
- export const NO_STAGED_FILES = `${info} No staged files found.`
25
+ export const NO_STAGED_FILES = `${info} lint-staged could not find any staged files.`
26
26
 
27
- export const NO_TASKS = `${info} No staged files match any configured task.`
27
+ export const NO_TASKS = `${info} lint-staged could not find any staged files matching configured tasks.`
28
28
 
29
29
  export const skippingBackup = (hasInitialCommit, diff) => {
30
30
  const reason =
@@ -66,11 +66,13 @@ export const PREVENTED_EMPTY_COMMIT = `
66
66
  Use the --allow-empty option to continue, or check your task configuration`)}
67
67
  `
68
68
 
69
- export const RESTORE_STASH_EXAMPLE = `Any lost modifications can be restored from a git stash:
69
+ export const restoreStashExample = (
70
+ hash = 'h0a0s0h0'
71
+ ) => `Any lost modifications can be restored from a git stash:
70
72
 
71
73
  > git stash list --format="%h %s"
72
- h0a0s0h0 On main: lint-staged automatic backup
73
- > git apply --index h0a0s0h0`
74
+ ${hash} On main: lint-staged automatic backup
75
+ > git apply --index ${hash}`
74
76
 
75
77
  export const CONFIG_STDIN_ERROR = red(`${error} Failed to read config from stdin.`)
76
78
 
package/lib/runAll.js CHANGED
@@ -110,6 +110,7 @@ export const runAll = async (
110
110
  debugLog('Using working directory `%s`', cwd)
111
111
 
112
112
  const ctx = getInitialState({
113
+ failOnChanges,
113
114
  hidePartiallyStaged,
114
115
  hideUnstaged,
115
116
  quiet,
@@ -336,7 +337,7 @@ export const runAll = async (
336
337
  },
337
338
  {
338
339
  title: `Running tasks for ${diff ? 'changed' : 'staged'} files...`,
339
- task: (ctx, task) => task.newListr(listrTasks, { concurrent }),
340
+ task: (ctx, task) => git.runTasks(ctx, task, { listrTasks, concurrent }),
340
341
  skip: () => listrTasks.every((task) => task.skip()),
341
342
  },
342
343
  {
package/lib/state.js CHANGED
@@ -2,7 +2,7 @@ import EventEmitter from 'events'
2
2
 
3
3
  import { GIT_ERROR, TASK_ERROR } from './messages.js'
4
4
  import {
5
- ExitCodeError,
5
+ FailOnChangesError,
6
6
  GitError,
7
7
  RestoreOriginalStateError,
8
8
  RestoreUnstagedChangesError,
@@ -10,6 +10,7 @@ import {
10
10
  } from './symbols.js'
11
11
 
12
12
  export const getInitialState = ({
13
+ failOnChanges = false,
13
14
  hideUnstaged = false,
14
15
  hidePartiallyStaged = !hideUnstaged,
15
16
  quiet = false,
@@ -18,6 +19,7 @@ export const getInitialState = ({
18
19
  backupHash: null,
19
20
  errors: new Set([]),
20
21
  events: new EventEmitter(),
22
+ shouldFailOnChanges: failOnChanges,
21
23
  hasFilesToHide: null,
22
24
  output: [],
23
25
  quiet,
@@ -25,6 +27,7 @@ export const getInitialState = ({
25
27
  shouldHidePartiallyStaged: hidePartiallyStaged,
26
28
  shouldHideUnstaged: hideUnstaged,
27
29
  shouldRevert: revert,
30
+ unstagedDiffSha256: null,
28
31
  unstagedPatch: null,
29
32
  })
30
33
 
@@ -37,10 +40,12 @@ export const shouldRestoreUnstagedChanges = (ctx) =>
37
40
  export const applyModificationsSkipped = (ctx) => {
38
41
  // Always apply back unstaged modifications when skipping revert or backup
39
42
  if (!ctx.shouldRevert || !ctx.shouldBackup) return false
43
+
40
44
  // Should be skipped in case of git errors
41
45
  if (ctx.errors.has(GitError)) {
42
46
  return GIT_ERROR
43
47
  }
48
+
44
49
  // Should be skipped when tasks fail
45
50
  if (ctx.errors.has(TaskError)) {
46
51
  return TASK_ERROR
@@ -53,6 +58,13 @@ export const restoreUnstagedChangesSkipped = (ctx) => {
53
58
  return GIT_ERROR
54
59
  }
55
60
 
61
+ // When complete reverting to original state is skipped,
62
+ // we can still restore unstaged changes to make it easier
63
+ // to do manually.
64
+ if (!ctx.shouldRevert) {
65
+ false
66
+ }
67
+
56
68
  // Should be skipped when tasks fail
57
69
  if (ctx.errors.has(TaskError)) {
58
70
  return TASK_ERROR
@@ -62,17 +74,13 @@ export const restoreUnstagedChangesSkipped = (ctx) => {
62
74
  export const restoreOriginalStateEnabled = (ctx) =>
63
75
  !!ctx.shouldRevert &&
64
76
  !!ctx.shouldBackup &&
65
- (ctx.errors.has(ExitCodeError) ||
77
+ (ctx.errors.has(FailOnChangesError) ||
66
78
  ctx.errors.has(TaskError) ||
67
79
  ctx.errors.has(RestoreUnstagedChangesError))
68
80
 
69
81
  export const restoreOriginalStateSkipped = (ctx) => {
70
82
  // Should be skipped in case of unknown git errors
71
- if (
72
- ctx.errors.has(GitError) &&
73
- !ctx.errors.has(RestoreUnstagedChangesError) &&
74
- !ctx.errors.has(ExitCodeError)
75
- ) {
83
+ if (ctx.errors.has(GitError) && !ctx.errors.has(RestoreUnstagedChangesError)) {
76
84
  return GIT_ERROR
77
85
  }
78
86
  }
@@ -80,6 +88,11 @@ export const restoreOriginalStateSkipped = (ctx) => {
80
88
  export const cleanupEnabled = (ctx) => ctx.shouldBackup
81
89
 
82
90
  export const cleanupSkipped = (ctx) => {
91
+ // "--fail-on-changes" was used, so we shouldn't drop the backup stash
92
+ if (ctx.errors.has(FailOnChangesError) && !ctx.shouldRevert) {
93
+ return true
94
+ }
95
+
83
96
  // Should be skipped in case of unknown git errors
84
97
  if (restoreOriginalStateSkipped(ctx)) {
85
98
  return GIT_ERROR
package/lib/symbols.js CHANGED
@@ -26,4 +26,4 @@ export const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError')
26
26
 
27
27
  export const TaskError = Symbol('TaskError')
28
28
 
29
- export const ExitCodeError = Symbol('ExitCodeError')
29
+ export const FailOnChangesError = Symbol('FailOnChangesError')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "16.2.1",
3
+ "version": "16.2.3",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": {