diff --git a/lib/config.js b/lib/config.js index 7b8ac77a..41ff4bf5 100644 --- a/lib/config.js +++ b/lib/config.js @@ -15,8 +15,8 @@ class Config { verbose: args.verbose, // Command specific options lint: { - rules: args.rules, - skip: args.skip, + rules: this.notEmptyArray(args.rules), + skip: this.notEmptyArray(args.skip), }, resolve: { output: args.output, @@ -71,6 +71,17 @@ class Config { }); return cleaned; } + + /** + * Check and returns the given array if it is not empty. Otherwise it returns 'undefined'. + * Useful for configuration options where an empty array has no meaning. + * + * @param array The array instance to check. + * @returns {array|undefined} The given array; or 'undefined' if array is 'undefined' or empty. + */ + notEmptyArray(array) { + return array && array.length > 0 ? array : undefined; + } } module.exports = new Config; diff --git a/lint.js b/lint.js index e49ad1d8..d1424cc0 100644 --- a/lint.js +++ b/lint.js @@ -69,8 +69,8 @@ const command = async (specFile, cmd) => { config.init(cmd); const jsonSchema = config.get('jsonSchema'); const verbose = config.get('quiet') ? 0 : config.get('verbose', 1); - const rulesets = config.get('lint:rules'); - const skip = config.get('lint:skip'); + const rulesets = config.get('lint:rules', []); + const skip = config.get('lint:skip', []); rules.init({ skip diff --git a/test/lib/config.test.js b/test/lib/config.test.js index 1b03cacd..11a6832a 100644 --- a/test/lib/config.test.js +++ b/test/lib/config.test.js @@ -38,6 +38,20 @@ describe('Config', () => { 'https://example.org/my-rules.json', ]); }); + + test('meaningless empty arrays in default config must not override options of config file', () => { + config.init({ config: configFile, rules: [], skip: []}); + + expect(config.get('lint:rules')).toEqual([ + 'strict', + './some/local/rules.json', + 'https://example.org/my-rules.json', + ]); + + expect(config.get('lint:skip')).toEqual([ + 'info-contact', + ]); + }); }); });