Skip to content

Commit e24aaf2

Browse files
Iiro Jäppinenokonet
Iiro Jäppinen
authored andcommittedJul 1, 2019
fix: parse titles for function linters
1 parent e862e7e commit e24aaf2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
 

‎src/makeCmdTasks.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ const resolveTaskFn = require('./resolveTaskFn')
44

55
const debug = require('debug')('lint-staged:make-cmd-tasks')
66

7+
/**
8+
* Get title for linter task. For a function, evaluate by passing single [file].
9+
* For strings, return as-is
10+
* @param {string|Function} linter
11+
*/
12+
const getTitle = linter => {
13+
if (typeof linter === 'function') {
14+
const resolved = linter(['[file]'])
15+
return Array.isArray(resolved) ? resolved[0] : resolved
16+
}
17+
return linter
18+
}
19+
720
/**
821
* Creates and returns an array of listr tasks which map to the given commands.
922
*
10-
* @param {Array<string>|string} commands
23+
* @param {Array<string|Function>|string|Function} commands
1124
* @param {Boolean} shell
1225
* @param {Array<string>} pathsToLint
1326
* @param {Object} [options]
@@ -18,7 +31,7 @@ module.exports = async function makeCmdTasks(commands, shell, gitDir, pathsToLin
1831
const lintersArray = Array.isArray(commands) ? commands : [commands]
1932

2033
return lintersArray.map(linter => ({
21-
title: linter,
34+
title: getTitle(linter),
2235
task: resolveTaskFn({
2336
linter,
2437
shell,

‎test/makeCmdTasks.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,34 @@ describe('makeCmdTasks', () => {
5252
shell: false
5353
})
5454
})
55+
56+
it('should work with function linter returning a string', async () => {
57+
const res = await makeCmdTasks(() => 'test', false, gitDir, ['test.js'])
58+
expect(res.length).toBe(1)
59+
expect(res[0].title).toEqual('test')
60+
})
61+
62+
it('should work with function linter returning array of string', async () => {
63+
const res = await makeCmdTasks(() => ['test', 'test2'], false, gitDir, ['test.js'])
64+
expect(res.length).toBe(1)
65+
expect(res[0].title).toEqual('test')
66+
})
67+
68+
it('should work with function linter accepting arguments', async () => {
69+
const res = await makeCmdTasks(
70+
filenames => filenames.map(file => `test ${file}`),
71+
false,
72+
gitDir,
73+
['test.js']
74+
)
75+
expect(res.length).toBe(1)
76+
expect(res[0].title).toEqual('test [file]')
77+
})
78+
79+
it('should work with array of mixed string and function linters', async () => {
80+
const res = await makeCmdTasks([() => 'test', 'test2'], false, gitDir, ['test.js'])
81+
expect(res.length).toBe(2)
82+
expect(res[0].title).toEqual('test')
83+
expect(res[1].title).toEqual('test2')
84+
})
5585
})

0 commit comments

Comments
 (0)
Please sign in to comment.