Skip to content

Commit

Permalink
feat: add deprecation error for advanced configuration
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 e829646 commit 4bef26e
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = async function runAll(
console.warn(
dedent`${symbols.warning} ${chalk.yellow(
`lint-staged generated an argument string of ${argLength} characters, and commands might not run correctly on your platform.
It is recommended to use functions as linters and split your command based on the number of staged files. For more info, please read:
It is recommended to use functions as linters and split your command based on the number of staged files. For more info, please visit:
https://github.com/okonet/lint-staged#using-js-functions-to-customize-linter-commands
`
)}`
Expand Down
26 changes: 25 additions & 1 deletion src/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ const format = require('stringify-object')

const debug = require('debug')('lint-staged:cfg')

const TEST_DEPRECATED_KEYS = new Map([
['concurrent', key => typeof key === 'boolean'],
['chunkSize', key => typeof key === 'number'],
['globOptions', key => typeof key === 'object'],
['linters', key => typeof key === 'object'],
['ignore', key => Array.isArray(key)],
['subTaskConcurrency', key => typeof key === 'number'],
['renderer', key => typeof key === 'string'],
['relative', key => typeof key === 'boolean']
])

const formatError = helpMsg => `● Validation Error:
${helpMsg}
Expand Down Expand Up @@ -42,6 +53,19 @@ module.exports = function validateConfig(config) {
}

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

if (
(!Array.isArray(config[key]) ||
config[key].some(item => typeof item !== 'string' && typeof item !== 'function')) &&
Expand All @@ -52,7 +76,7 @@ module.exports = function validateConfig(config) {
createError(
key,
'Should be a string, a function, or an array of strings and functions',
key
config[key]
)
)
}
Expand Down
133 changes: 132 additions & 1 deletion test/__snapshots__/validateConfig.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,140 @@ exports[`validateConfig should throw and should print validation errors for inva
Should be a string, a function, or an array of strings and functions.
Configured value is: 'foo'
Configured value is: false
Please refer to https://github.com/okonet/lint-staged#configuration for more information..."
`;

exports[`validateConfig should throw and should print validation errors for invalid config 1 1`] = `"Configuration should be an object!"`;

exports[`validateConfig should throw when detecting deprecated advanced configuration 1`] = `
"● Validation Error:
Invalid value for 'chunkSize'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: 10
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'chunkSize'.
Should be a string, a function, or an array of strings and functions.
Configured value is: 10
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'concurrent'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: false
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'concurrent'.
Should be a string, a function, or an array of strings and functions.
Configured value is: false
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'globOptions'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: {matchBase: false}
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'globOptions'.
Should be a string, a function, or an array of strings and functions.
Configured value is: {matchBase: false}
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'ignore'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: ['test.js']
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'linters'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: {'*.js': ['eslint']}
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'linters'.
Should be a string, a function, or an array of strings and functions.
Configured value is: {'*.js': ['eslint']}
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'relative'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: true
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'relative'.
Should be a string, a function, or an array of strings and functions.
Configured value is: true
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'renderer'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: 'silent'
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'subTaskConcurrency'.
Advanced configuration has been deprecated. For more info, please visit: https://github.com/okonet/lint-staged.
Configured value is: 10
Please refer to https://github.com/okonet/lint-staged#configuration for more information...
● Validation Error:
Invalid value for 'subTaskConcurrency'.
Should be a string, a function, or an array of strings and functions.
Configured value is: 10
Please refer to https://github.com/okonet/lint-staged#configuration for more information..."
`;

exports[`validateConfig should throw when detecting deprecated advanced configuration 2`] = `""`;
18 changes: 18 additions & 0 deletions test/validateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ describe('validateConfig', () => {
).not.toThrow()
expect(console.printHistory()).toMatchSnapshot()
})

it('should throw when detecting deprecated advanced configuration', () => {
const advancedConfig = {
chunkSize: 10,
concurrent: false,
globOptions: { matchBase: false },
ignore: ['test.js'],
linters: {
'*.js': ['eslint']
},
relative: true,
renderer: 'silent',
subTaskConcurrency: 10
}

expect(() => validateConfig(advancedConfig)).toThrowErrorMatchingSnapshot()
expect(console.printHistory()).toMatchSnapshot()
})
})

0 comments on commit 4bef26e

Please sign in to comment.