Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 0d027a7

Browse files
committed
Configuration: Do not set default options if preset is set
Ref #2276 Fixes #2275
1 parent 5885239 commit 0d027a7

File tree

3 files changed

+108
-14
lines changed

3 files changed

+108
-14
lines changed

lib/checker.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ utils.inherits(Checker, StringChecker);
2626
*/
2727
Checker.prototype.configure = function(config) {
2828
StringChecker.prototype.configure.call(this, config);
29-
30-
this._fileExtensions = this._configuration.getFileExtensions();
3129
};
3230

3331
/**
@@ -252,11 +250,12 @@ Checker.prototype._processStdin = function(stdinHandler) {
252250
Checker.prototype._hasCorrectExtension = function(testPath) {
253251
var extension = path.extname(testPath).toLowerCase();
254252
var basename = path.basename(testPath).toLowerCase();
253+
var fileExtensions = this._configuration.getFileExtensions();
255254

256255
return !(
257-
this._fileExtensions.indexOf(extension) < 0 &&
258-
this._fileExtensions.indexOf(basename) < 0 &&
259-
this._fileExtensions.indexOf('*') < 0
256+
fileExtensions.indexOf(extension) < 0 &&
257+
fileExtensions.indexOf(basename) < 0 &&
258+
fileExtensions.indexOf('*') < 0
260259
);
261260
};
262261

lib/config/configuration.js

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ function Configuration() {
9595
*/
9696
this._fileExtensions = [];
9797

98+
/**
99+
* List of defined options (not complete).
100+
*
101+
* @protected
102+
* @type {Array}
103+
*/
104+
this._definedOptions = [];
105+
98106
/**
99107
* Default file extensions that would be checked.
100108
*
@@ -199,13 +207,31 @@ function Configuration() {
199207
*/
200208
Configuration.prototype.load = function(config) {
201209

202-
// Apply all the options
210+
// Load all the options
203211
this._processConfig(config);
204212

213+
// Load defaults if they weren't set
214+
this._loadDefaults(config);
215+
205216
// Load and apply all the rules
206217
this._useRules();
207218
};
208219

220+
/**
221+
* Load default values for options which were not defined
222+
*
223+
* @private
224+
*/
225+
Configuration.prototype._loadDefaults = function() {
226+
if (!this._isDefined('excludeFiles')) {
227+
this._loadExcludedFiles(this._defaultExcludedFileMasks);
228+
}
229+
230+
if (!this._isDefined('fileExtensions')) {
231+
this._loadFileExtensions(this._defaultFileExtensions);
232+
}
233+
};
234+
209235
/**
210236
* Returns resulting configuration after preset is applied and options are processed.
211237
*
@@ -437,13 +463,21 @@ Configuration.prototype._processConfig = function(config) {
437463
options.plugins.forEach(function(plugin) {
438464
this._loadPlugin(plugin, options.configPath);
439465
}, this);
466+
467+
if (!this._isDefined('plugins')) {
468+
this._definedOptions.push('plugins');
469+
}
440470
}
441471

442472
if (options.hasOwnProperty('additionalRules')) {
443473
assert(Array.isArray(options.additionalRules), '`additionalRules` option requires array value');
444474
options.additionalRules.forEach(function(rule) {
445475
this._loadAdditionalRule(rule, options.configPath);
446476
}, this);
477+
478+
if (!this._isDefined('additionalRules')) {
479+
this._definedOptions.push('additionalRules');
480+
}
447481
}
448482

449483
if (options.hasOwnProperty('extract')) {
@@ -452,18 +486,10 @@ Configuration.prototype._processConfig = function(config) {
452486

453487
if (options.hasOwnProperty('fileExtensions')) {
454488
this._loadFileExtensions(options.fileExtensions);
455-
456-
// Set default extensions if there is no presets that could define their own
457-
} else if (!options.hasOwnProperty('preset')) {
458-
this._loadFileExtensions(this._defaultFileExtensions);
459489
}
460490

461491
if (options.hasOwnProperty('excludeFiles')) {
462492
this._loadExcludedFiles(options.excludeFiles);
463-
464-
// Set default masks if there is no presets that could define their own
465-
} else if (!options.hasOwnProperty('preset')) {
466-
this._loadExcludedFiles(this._defaultExcludedFileMasks);
467493
}
468494

469495
if (options.hasOwnProperty('fix')) {
@@ -543,6 +569,10 @@ Configuration.prototype._loadErrorFilter = function(errorFilter) {
543569
'`errorFilter` option requires a function or null value'
544570
);
545571
this._errorFilter = errorFilter;
572+
573+
if (!this._isDefined('errorFilter')) {
574+
this._definedOptions.push('errorFilter');
575+
}
546576
};
547577

548578
/**
@@ -557,6 +587,10 @@ Configuration.prototype._loadES3 = function(es3) {
557587
'`es3` option requires boolean or null value'
558588
);
559589
this._es3Enabled = Boolean(es3);
590+
591+
if (!this._isDefined('es3')) {
592+
this._definedOptions.push('es3');
593+
}
560594
};
561595

562596
/**
@@ -587,6 +621,10 @@ Configuration.prototype._loadMaxError = function(options) {
587621
);
588622

589623
this._maxErrors = maxErrors;
624+
625+
if (!this._isDefined('fix')) {
626+
this._definedOptions.push('fix');
627+
}
590628
};
591629

592630
/**
@@ -604,6 +642,10 @@ Configuration.prototype._loadFix = function(fix) {
604642
);
605643

606644
this._fix = fix;
645+
646+
if (!this._isDefined('fix')) {
647+
this._definedOptions.push('fix');
648+
}
607649
};
608650

609651
/**
@@ -631,6 +673,10 @@ Configuration.prototype._loadPreset = function(preset) {
631673
var presetData = this._presets[preset];
632674
assert(Boolean(presetData), 'Preset "' + preset + '" does not exist');
633675

676+
if (!this._isDefined('preset')) {
677+
this._definedOptions.push('preset');
678+
}
679+
634680
// Process config from the preset
635681
this._processConfig(this._presets[preset]);
636682
};
@@ -646,9 +692,25 @@ Configuration.prototype._loadFileExtensions = function(extensions) {
646692
typeof extensions === 'string' || Array.isArray(extensions),
647693
'`fileExtensions` option requires string or array value'
648694
);
695+
649696
this._fileExtensions = this._fileExtensions.concat(extensions).map(function(ext) {
650697
return ext.toLowerCase();
651698
});
699+
700+
if (!this._isDefined('fileExtensions')) {
701+
this._definedOptions.push('fileExtensions');
702+
}
703+
};
704+
705+
/**
706+
* Is option defined?
707+
*
708+
* @param {String} name - name of the option
709+
*
710+
* @return {Boolean}
711+
*/
712+
Configuration.prototype._isDefined = function(name) {
713+
return this._definedOptions.indexOf(name) > -1;
652714
};
653715

654716
/**
@@ -666,6 +728,10 @@ Configuration.prototype._loadExcludedFiles = function(masks) {
666728
dot: true
667729
});
668730
}, this);
731+
732+
if (!this._isDefined('excludeFiles')) {
733+
this._definedOptions.push('excludeFiles');
734+
}
669735
};
670736

671737
/**
@@ -688,6 +754,10 @@ Configuration.prototype._loadExtract = function(masks) {
688754
dot: true
689755
});
690756
}, this);
757+
758+
if (!this._isDefined('extract')) {
759+
this._definedOptions.push('extract');
760+
}
691761
};
692762

693763
/**

test/specs/config/configuration.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,31 @@ describe('config/configuration', function() {
583583
expect(configuration.getExcludedFileMasks()).to.deep.equal(['.git/**', 'node_modules/**']);
584584
});
585585

586+
describe('for settings with default value when preset is used #2275', function() {
587+
it('`excludeFiles`', function() {
588+
configuration.registerPreset('test1', {});
589+
configuration.load({
590+
preset: 'test1',
591+
excludeFiles: ['test']
592+
});
593+
594+
expect(configuration.getExcludedFileMasks().length).to.equal(1);
595+
expect(configuration.getExcludedFileMasks()[0]).to.equal('test');
596+
});
597+
598+
it('`fileExtensions`', function() {
599+
configuration.registerPreset('test1', {});
600+
configuration.load({
601+
preset: 'test1',
602+
fileExtensions: ['.test']
603+
});
604+
605+
expect(configuration.getFileExtensions().length).to.equal(1);
606+
expect(configuration.getFileExtensions()[0]).to.equal('.test');
607+
}
608+
);
609+
});
610+
586611
it('should set `fileExtensions` setting from presets', function() {
587612
configuration.registerPreset('test1', {
588613
fileExtensions: ['first']

0 commit comments

Comments
 (0)