Skip to content

Commit

Permalink
refactor: support function linters in getConfig
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 0bdf501 commit 9e4346f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/getConfig.js
Expand Up @@ -195,11 +195,17 @@ function validateConfig(config) {
if (isObject(config.linters)) {
Object.keys(config.linters).forEach(key => {
if (
(!isArray(config.linters[key]) || config.linters[key].some(item => !isString(item))) &&
!isString(config.linters[key])
(!isArray(config.linters[key]) ||
config.linters[key].some(item => !isString(item) && !isFunction(item))) &&
!isString(config.linters[key]) &&
!isFunction(config.linters[key])
) {
errors.push(
createError(`linters[${key}]`, 'Should be a string or an array of strings', key)
createError(
`linters[${key}]`,
'Should be a string, a function, or an array of strings and functions',
key
)
)
}
})
Expand Down
22 changes: 21 additions & 1 deletion test/__snapshots__/getConfig.spec.js.snap
Expand Up @@ -54,10 +54,30 @@ Object {
}
`;

exports[`getConfig should set linters 2`] = `
Object {
"chunkSize": 9007199254740991,
"concurrent": true,
"globOptions": Object {
"dot": true,
"matchBase": true,
},
"ignore": Array [],
"linters": Object {
"*.js": [Function],
},
"relative": false,
"renderer": "update",
"subTaskConcurrency": 1,
}
`;

exports[`validateConfig should not throw and should print nothing for advanced valid config 1`] = `""`;

exports[`validateConfig should not throw and should print nothing for custom renderer 1`] = `""`;

exports[`validateConfig should not throw and should print nothing for function linter 1`] = `""`;

exports[`validateConfig should not throw and should print nothing for simple valid config 1`] = `""`;

exports[`validateConfig should not throw and should print validation warnings for mixed config 1`] = `
Expand Down Expand Up @@ -112,7 +132,7 @@ exports[`validateConfig should throw and should print validation errors for inva
Invalid value for 'linters[*.js]'.
Should be a string or an array of strings.
Should be a string, a function, or an array of strings and functions.
Configured value is: '*.js'
Expand Down
27 changes: 27 additions & 0 deletions test/getConfig.spec.js
Expand Up @@ -96,6 +96,17 @@ describe('getConfig', () => {
}
})
).toMatchSnapshot()

expect(
getConfig({
linters: {
'*.js': filenames => {
const files = filenames.join(' ')
return `eslint --fix ${files} && git add ${files}`
}
}
})
).toMatchSnapshot()
})

it('should deeply merge configs', () => {
Expand Down Expand Up @@ -229,4 +240,20 @@ describe('validateConfig', () => {
expect(() => validateConfig(getConfig(validAdvancedConfig))).not.toThrow()
expect(console.printHistory()).toMatchSnapshot()
})

it('should not throw and should print nothing for function linter', () => {
expect(() =>
validateConfig(
getConfig({
linters: {
'*.js': filenames => {
const files = filenames.join(' ')
return `eslint --fix ${files} && git add ${files}`
}
}
})
)
).not.toThrow()
expect(console.printHistory()).toMatchSnapshot()
})
})

0 comments on commit 9e4346f

Please sign in to comment.