Skip to content

Commit

Permalink
fix: parse titles for function linters
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Jäppinen authored and okonet committed Jul 1, 2019
1 parent e862e7e commit e24aaf2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/makeCmdTasks.js
Expand Up @@ -4,10 +4,23 @@ const resolveTaskFn = require('./resolveTaskFn')

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

/**
* Get title for linter task. For a function, evaluate by passing single [file].
* For strings, return as-is
* @param {string|Function} linter
*/
const getTitle = linter => {
if (typeof linter === 'function') {
const resolved = linter(['[file]'])
return Array.isArray(resolved) ? resolved[0] : resolved
}
return linter
}

/**
* Creates and returns an array of listr tasks which map to the given commands.
*
* @param {Array<string>|string} commands
* @param {Array<string|Function>|string|Function} commands
* @param {Boolean} shell
* @param {Array<string>} pathsToLint
* @param {Object} [options]
Expand All @@ -18,7 +31,7 @@ module.exports = async function makeCmdTasks(commands, shell, gitDir, pathsToLin
const lintersArray = Array.isArray(commands) ? commands : [commands]

return lintersArray.map(linter => ({
title: linter,
title: getTitle(linter),
task: resolveTaskFn({
linter,
shell,
Expand Down
30 changes: 30 additions & 0 deletions test/makeCmdTasks.spec.js
Expand Up @@ -52,4 +52,34 @@ describe('makeCmdTasks', () => {
shell: false
})
})

it('should work with function linter returning a string', async () => {
const res = await makeCmdTasks(() => 'test', false, gitDir, ['test.js'])
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test')
})

it('should work with function linter returning array of string', async () => {
const res = await makeCmdTasks(() => ['test', 'test2'], false, gitDir, ['test.js'])
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test')
})

it('should work with function linter accepting arguments', async () => {
const res = await makeCmdTasks(
filenames => filenames.map(file => `test ${file}`),
false,
gitDir,
['test.js']
)
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test [file]')
})

it('should work with array of mixed string and function linters', async () => {
const res = await makeCmdTasks([() => 'test', 'test2'], false, gitDir, ['test.js'])
expect(res.length).toBe(2)
expect(res[0].title).toEqual('test')
expect(res[1].title).toEqual('test2')
})
})

0 comments on commit e24aaf2

Please sign in to comment.