Skip to content

Commit

Permalink
fix: validateConfig validates function task return values
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Aug 27, 2019
1 parent 2243a83 commit d8fad78
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
43 changes: 29 additions & 14 deletions src/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,55 @@ module.exports = function validateConfig(config) {
if (!config || typeof config !== 'object') {
errors.push('Configuration should be an object!')
} else {
const globs = Object.keys(config)
const entries = Object.entries(config)

if (globs.length === 0) {
if (entries.length === 0) {
errors.push('Configuration should not be empty!')
}

globs.forEach(key => {
if (TEST_DEPRECATED_KEYS.has(key)) {
const testFn = TEST_DEPRECATED_KEYS.get(key)
if (testFn(config[key])) {
entries.forEach(([pattern, task]) => {
if (TEST_DEPRECATED_KEYS.has(pattern)) {
const testFn = TEST_DEPRECATED_KEYS.get(pattern)
if (testFn(task)) {
errors.push(
createError(
key,
pattern,
'Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged',
config[key]
task
)
)
}
}

if (
(!Array.isArray(config[key]) ||
config[key].some(item => typeof item !== 'string' && typeof item !== 'function')) &&
typeof config[key] !== 'string' &&
typeof config[key] !== 'function'
(!Array.isArray(task) ||
task.some(item => typeof item !== 'string' && typeof item !== 'function')) &&
typeof task !== 'string' &&
typeof task !== 'function'
) {
errors.push(
createError(
key,
pattern,
'Should be a string, a function, or an array of strings and functions',
config[key]
task
)
)
}

entries.forEach(([, task]) => {
if (typeof task !== 'function') return
const resolved = task(['[filename]'])
if (typeof resolved === 'string') return
if (!Array.isArray(resolved) || resolved.some(subtask => typeof subtask !== 'string')) {
errors.push(
createError(
task,
'Function task should return a string or an array of strings',
resolved
)
)
}
})
})
}

Expand Down
16 changes: 15 additions & 1 deletion test/__snapshots__/validateConfig.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

exports[`validateConfig should not throw when config contains deprecated key but with valid task 1`] = `""`;

exports[`validateConfig should throw and should print validation errors for invalid config 1`] = `
"● Validation Error:
Expand Down Expand Up @@ -148,3 +150,15 @@ Please refer to https://github.com/okonet/lint-staged#configuration for more inf
`;

exports[`validateConfig should throw when detecting deprecated advanced configuration 2`] = `""`;

exports[`validateConfig should throw when function task returns incorrect values 1`] = `
"● Validation Error:
Invalid value for 'filenames => filenames.map(file => [\`eslint --fix \${file}\`, \`git add \${file}\`])'.
Function task should return a string or an array of strings.
Configured value is: [['eslint --fix [filename]', 'git add [filename]']]
Please refer to https://github.com/okonet/lint-staged#configuration for more information..."
`;
17 changes: 16 additions & 1 deletion test/validateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('validateConfig', () => {
expect(console.printHistory()).toMatchSnapshot()
})

it('should not throw and should print nothing for function linter', () => {
it('should not throw and should print nothing for function task', () => {
expect(() =>
validateConfig({
'*.js': filenames => {
Expand All @@ -49,6 +49,13 @@ describe('validateConfig', () => {
expect(console.printHistory()).toMatchSnapshot()
})

it('should throw when function task returns incorrect values', () => {
const invalidConfig = {
'*.js': filenames => filenames.map(file => [`eslint --fix ${file}`, `git add ${file}`])
}
expect(() => validateConfig(invalidConfig)).toThrowErrorMatchingSnapshot()
})

it('should throw when detecting deprecated advanced configuration', () => {
const advancedConfig = {
chunkSize: 10,
Expand All @@ -66,4 +73,12 @@ describe('validateConfig', () => {
expect(() => validateConfig(advancedConfig)).toThrowErrorMatchingSnapshot()
expect(console.printHistory()).toMatchSnapshot()
})

it('should not throw when config contains deprecated key but with valid task', () => {
const stillValidConfig = {
concurrent: 'my command'
}
expect(() => validateConfig(stillValidConfig)).not.toThrow()
expect(console.printHistory()).toMatchSnapshot()
})
})

0 comments on commit d8fad78

Please sign in to comment.