Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/issue-1398: added support for custom configuration #1401

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
42 changes: 35 additions & 7 deletions lib/makeCmdTasks.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has started to become a bit complex... I wonder if we could split it somehow, maybe different handling for function and regular config.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -1,7 +1,8 @@
import debug from 'debug'

import { configurationError } from './messages.js'
import { resolveTaskFn } from './resolveTaskFn.js'
import { makeErr, resolveTaskFn } from './resolveTaskFn.js'
import { getInitialState } from './state.js'

const debugLog = debug('lint-staged:makeCmdTasks')

Expand Down Expand Up @@ -31,20 +32,47 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbos
const resolvedArray = Array.isArray(resolved) ? resolved : [resolved] // Wrap non-array command as array

for (const command of resolvedArray) {
// If the function linter didn't return string | string[] it won't work
// If the function linter didn't return string | string[] | object it won't work
// Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
if (isFn && typeof command !== 'string') {
if ((isFn && typeof command !== 'string' && typeof command !== 'object') || !command) {
throw new Error(
configurationError(
'[Function]',
'Function task should return a string or an array of strings',
'Function task should return a string or an array of strings or an object',
resolved
)
)
} else if (isFn && typeof command === 'object') {
if (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we flip the condition and do an early return?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

typeof command.title === 'string' &&
typeof command.command === 'string' &&
typeof command.task === 'function'
) {
const task = async (ctx = getInitialState()) => {
try {
await command.task()
} catch (e) {
throw makeErr(command.title, e, ctx)
}
}
cmdTasks.push({
title: command.title,
command: command.command,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both the command and title?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only need title, so removed the command field.

task,
})
} else {
throw new Error(
configurationError(
'[Function]',
'Function task should return object with title, command and task where title or command should be string and task should be function',
resolved
)
)
}
} else {
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
cmdTasks.push({ title: command, command, task })
}

const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
cmdTasks.push({ title: command, command, task })
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/resolveTaskFn.js
Expand Up @@ -108,7 +108,7 @@ const interruptExecutionOnError = (ctx, execaChildProcess) => {
* @param {Object} ctx
* @returns {Error}
*/
const makeErr = (command, result, ctx) => {
export const makeErr = (command, result, ctx) => {
ctx.errors.add(TaskError)

// https://nodejs.org/api/events.html#error-events
Expand Down
4 changes: 2 additions & 2 deletions test/unit/makeCmdTasks.spec.js
Expand Up @@ -112,14 +112,14 @@ describe('makeCmdTasks', () => {
expect(res[0].title).toEqual('test')
})

it("should throw when function task doesn't return string | string[]", async () => {
it("should throw when function task doesn't return string | string[] | object", async () => {
await expect(makeCmdTasks({ commands: () => null, gitDir, files: ['test.js'] })).rejects
.toThrowErrorMatchingInlineSnapshot(`
"✖ Validation Error:

Invalid value for '[Function]': null

Function task should return a string or an array of strings"
Function task should return a string or an array of strings or an object"
`)
})

Expand Down