lint-staged 10.0.7 → 10.0.8

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.
@@ -264,6 +264,7 @@ class GitWorkflow {
264
264
  // Restore meta information about ongoing git merge
265
265
  await this.restoreMergeStatus()
266
266
  } catch (error) {
267
+ ctx.gitRestoreOriginalStateError = true
267
268
  handleError(error, ctx)
268
269
  }
269
270
  }
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const resolveTaskFn = require('./resolveTaskFn')
4
+ const { createError } = require('./validateConfig')
4
5
 
5
6
  const debug = require('debug')('lint-staged:make-cmd-tasks')
6
7
 
@@ -15,32 +16,34 @@ const debug = require('debug')('lint-staged:make-cmd-tasks')
15
16
  */
16
17
  module.exports = async function makeCmdTasks({ commands, files, gitDir, shell }) {
17
18
  debug('Creating listr tasks for commands %o', commands)
18
- const commandsArray = Array.isArray(commands) ? commands : [commands]
19
+ const commandArray = Array.isArray(commands) ? commands : [commands]
19
20
  const cmdTasks = []
20
21
 
21
- for (const cmd of commandsArray) {
22
+ for (const cmd of commandArray) {
22
23
  // command function may return array of commands that already include `stagedFiles`
23
24
  const isFn = typeof cmd === 'function'
24
25
  const resolved = isFn ? await cmd(files) : cmd
25
26
 
26
27
  const resolvedArray = Array.isArray(resolved) ? resolved : [resolved] // Wrap non-array command as array
27
28
 
28
- // Function command should not be used as the task title as-is
29
- // because the resolved string it might be very long
30
- // Create a matching command array with [file] in place of file names
31
- let mockCmdTasks
32
- if (isFn) {
33
- const mockFileList = Array(files.length).fill('[file]')
34
- const resolved = await cmd(mockFileList)
35
- mockCmdTasks = Array.isArray(resolved) ? resolved : [resolved]
36
- }
37
-
38
- for (const [i, command] of resolvedArray.entries()) {
29
+ for (const command of resolvedArray) {
39
30
  let title = isFn ? '[Function]' : command
40
- if (isFn && mockCmdTasks[i]) {
41
- // If command is a function, use the matching mock command as title,
42
- // but since might include multiple [file] arguments, shorten to one
43
- title = mockCmdTasks[i].replace(/\[file\].*\[file\]/, '[file]')
31
+
32
+ if (isFn) {
33
+ // If the function linter didn't return string | string[] it won't work
34
+ // Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
35
+ if (typeof command !== 'string') {
36
+ throw new Error(
37
+ createError(
38
+ title,
39
+ 'Function task should return a string or an array of strings',
40
+ resolved
41
+ )
42
+ )
43
+ }
44
+
45
+ const [startOfFn] = command.split(' ')
46
+ title += ` ${startOfFn} ...` // Append function name, like `[Function] eslint ...`
44
47
  }
45
48
 
46
49
  cmdTasks.push({
package/lib/runAll.js CHANGED
@@ -23,6 +23,40 @@ const getRenderer = ({ debug, quiet }) => {
23
23
  return 'update'
24
24
  }
25
25
 
26
+ const MESSAGES = {
27
+ TASK_ERROR: 'Skipped because of errors from tasks.',
28
+ GIT_ERROR: 'Skipped because of previous git error.'
29
+ }
30
+
31
+ const shouldSkipApplyModifications = ctx => {
32
+ // Should be skipped in case of git errors
33
+ if (ctx.gitError) {
34
+ return MESSAGES.GIT_ERROR
35
+ }
36
+ // Should be skipped when tasks fail
37
+ if (ctx.taskError) {
38
+ return MESSAGES.TASK_ERROR
39
+ }
40
+ }
41
+
42
+ const shouldSkipRevert = ctx => {
43
+ // Should be skipped in case of unknown git errors
44
+ if (ctx.gitError && !ctx.gitApplyEmptyCommit && !ctx.gitApplyModificationsError) {
45
+ return MESSAGES.GIT_ERROR
46
+ }
47
+ }
48
+
49
+ const shouldSkipCleanup = ctx => {
50
+ // Should be skipped in case of unknown git errors
51
+ if (ctx.gitError && !ctx.gitApplyEmptyCommit && !ctx.gitApplyModificationsError) {
52
+ return MESSAGES.GIT_ERROR
53
+ }
54
+ // Should be skipped when reverting to original state fails
55
+ if (ctx.gitRestoreOriginalStateError) {
56
+ return MESSAGES.GIT_ERROR
57
+ }
58
+ }
59
+
26
60
  /**
27
61
  * Executes all tasks and either resolves or rejects the promise
28
62
  *
@@ -39,7 +73,7 @@ const getRenderer = ({ debug, quiet }) => {
39
73
  * @param {Logger} logger
40
74
  * @returns {Promise}
41
75
  */
42
- module.exports = async function runAll(
76
+ const runAll = async (
43
77
  {
44
78
  allowEmpty = false,
45
79
  config,
@@ -52,7 +86,7 @@ module.exports = async function runAll(
52
86
  concurrent = true
53
87
  },
54
88
  logger = console
55
- ) {
89
+ ) => {
56
90
  debugLog('Running all linter scripts')
57
91
 
58
92
  const { gitDir, gitConfigDir } = await resolveGitRepo(cwd)
@@ -127,7 +161,7 @@ module.exports = async function runAll(
127
161
  task: () => new Listr(chunkListrTasks, { ...listrOptions, concurrent }),
128
162
  skip: (ctx = {}) => {
129
163
  // Skip if the first step (backup) failed
130
- if (ctx.gitError) return 'Skipped because of previous git error.'
164
+ if (ctx.gitError) return MESSAGES.GIT_ERROR
131
165
  // Skip chunk when no every task is skipped (due to no matches)
132
166
  if (chunkListrTasks.every(task => task.skip())) return 'No tasks to run.'
133
167
  return false
@@ -151,15 +185,6 @@ module.exports = async function runAll(
151
185
 
152
186
  const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, stagedFileChunks })
153
187
 
154
- // Running git reset or dropping the backup stash should be skipped
155
- // when there are git errors NOT related to applying unstaged modifications.
156
- // In the latter case, the original state is restored.
157
- const cleanupNotSafe = ctx =>
158
- ctx.gitError &&
159
- !ctx.gitApplyEmptyCommit &&
160
- !ctx.gitApplyModificationsError &&
161
- 'Skipped because of previous git error.'
162
-
163
188
  const runner = new Listr(
164
189
  [
165
190
  {
@@ -169,22 +194,19 @@ module.exports = async function runAll(
169
194
  ...listrTasks,
170
195
  {
171
196
  title: 'Applying modifications...',
172
- skip: ctx => {
173
- if (ctx.gitError) return 'Skipped because of previous git error.'
174
- if (ctx.taskError) return 'Skipped because of errors from tasks.'
175
- },
176
- task: ctx => git.applyModifications(ctx)
197
+ task: ctx => git.applyModifications(ctx),
198
+ skip: shouldSkipApplyModifications
177
199
  },
178
200
  {
179
201
  title: 'Reverting to original state...',
202
+ task: ctx => git.restoreOriginalState(ctx),
180
203
  enabled: ctx => ctx.taskError || ctx.gitApplyEmptyCommit || ctx.gitApplyModificationsError,
181
- skip: cleanupNotSafe,
182
- task: ctx => git.restoreOriginalState(ctx)
204
+ skip: shouldSkipRevert
183
205
  },
184
206
  {
185
207
  title: 'Cleaning up...',
186
- skip: cleanupNotSafe,
187
- task: ctx => git.dropBackup(ctx)
208
+ task: ctx => git.dropBackup(ctx),
209
+ skip: shouldSkipCleanup
188
210
  }
189
211
  ],
190
212
  listrOptions
@@ -219,3 +241,11 @@ module.exports = async function runAll(
219
241
  throw error
220
242
  }
221
243
  }
244
+
245
+ module.exports = runAll
246
+
247
+ module.exports.shouldSkip = {
248
+ shouldSkipApplyModifications,
249
+ shouldSkipRevert,
250
+ shouldSkipCleanup
251
+ }
@@ -80,21 +80,6 @@ module.exports = function validateConfig(config) {
80
80
  )
81
81
  )
82
82
  }
83
-
84
- entries.forEach(([, task]) => {
85
- if (typeof task !== 'function') return
86
- const resolved = task(['[filename]'])
87
- if (typeof resolved === 'string') return
88
- if (!Array.isArray(resolved) || resolved.some(subtask => typeof subtask !== 'string')) {
89
- errors.push(
90
- createError(
91
- task,
92
- 'Function task should return a string or an array of strings',
93
- resolved
94
- )
95
- )
96
- }
97
- })
98
83
  })
99
84
  }
100
85
 
@@ -104,3 +89,5 @@ module.exports = function validateConfig(config) {
104
89
 
105
90
  return config
106
91
  }
92
+
93
+ module.exports.createError = createError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "10.0.7",
3
+ "version": "10.0.8",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/okonet/lint-staged",