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

Commit

Permalink
Configuration: Do not set default options if preset is set
Browse files Browse the repository at this point in the history
Ref #2276
Fixes #2275
  • Loading branch information
markelog committed Jun 21, 2016
1 parent 5885239 commit 0d027a7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 14 deletions.
9 changes: 4 additions & 5 deletions lib/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ utils.inherits(Checker, StringChecker);
*/
Checker.prototype.configure = function(config) {
StringChecker.prototype.configure.call(this, config);

this._fileExtensions = this._configuration.getFileExtensions();
};

/**
Expand Down Expand Up @@ -252,11 +250,12 @@ Checker.prototype._processStdin = function(stdinHandler) {
Checker.prototype._hasCorrectExtension = function(testPath) {
var extension = path.extname(testPath).toLowerCase();
var basename = path.basename(testPath).toLowerCase();
var fileExtensions = this._configuration.getFileExtensions();

return !(
this._fileExtensions.indexOf(extension) < 0 &&
this._fileExtensions.indexOf(basename) < 0 &&
this._fileExtensions.indexOf('*') < 0
fileExtensions.indexOf(extension) < 0 &&
fileExtensions.indexOf(basename) < 0 &&
fileExtensions.indexOf('*') < 0
);
};

Expand Down
88 changes: 79 additions & 9 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ function Configuration() {
*/
this._fileExtensions = [];

/**
* List of defined options (not complete).
*
* @protected
* @type {Array}
*/
this._definedOptions = [];

/**
* Default file extensions that would be checked.
*
Expand Down Expand Up @@ -199,13 +207,31 @@ function Configuration() {
*/
Configuration.prototype.load = function(config) {

// Apply all the options
// Load all the options
this._processConfig(config);

// Load defaults if they weren't set
this._loadDefaults(config);

// Load and apply all the rules
this._useRules();
};

/**
* Load default values for options which were not defined
*
* @private
*/
Configuration.prototype._loadDefaults = function() {
if (!this._isDefined('excludeFiles')) {
this._loadExcludedFiles(this._defaultExcludedFileMasks);
}

if (!this._isDefined('fileExtensions')) {
this._loadFileExtensions(this._defaultFileExtensions);
}
};

/**
* Returns resulting configuration after preset is applied and options are processed.
*
Expand Down Expand Up @@ -437,13 +463,21 @@ Configuration.prototype._processConfig = function(config) {
options.plugins.forEach(function(plugin) {
this._loadPlugin(plugin, options.configPath);
}, this);

if (!this._isDefined('plugins')) {
this._definedOptions.push('plugins');
}
}

if (options.hasOwnProperty('additionalRules')) {
assert(Array.isArray(options.additionalRules), '`additionalRules` option requires array value');
options.additionalRules.forEach(function(rule) {
this._loadAdditionalRule(rule, options.configPath);
}, this);

if (!this._isDefined('additionalRules')) {
this._definedOptions.push('additionalRules');
}
}

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

if (options.hasOwnProperty('fileExtensions')) {
this._loadFileExtensions(options.fileExtensions);

// Set default extensions if there is no presets that could define their own
} else if (!options.hasOwnProperty('preset')) {
this._loadFileExtensions(this._defaultFileExtensions);
}

if (options.hasOwnProperty('excludeFiles')) {
this._loadExcludedFiles(options.excludeFiles);

// Set default masks if there is no presets that could define their own
} else if (!options.hasOwnProperty('preset')) {
this._loadExcludedFiles(this._defaultExcludedFileMasks);
}

if (options.hasOwnProperty('fix')) {
Expand Down Expand Up @@ -543,6 +569,10 @@ Configuration.prototype._loadErrorFilter = function(errorFilter) {
'`errorFilter` option requires a function or null value'
);
this._errorFilter = errorFilter;

if (!this._isDefined('errorFilter')) {
this._definedOptions.push('errorFilter');
}
};

/**
Expand All @@ -557,6 +587,10 @@ Configuration.prototype._loadES3 = function(es3) {
'`es3` option requires boolean or null value'
);
this._es3Enabled = Boolean(es3);

if (!this._isDefined('es3')) {
this._definedOptions.push('es3');
}
};

/**
Expand Down Expand Up @@ -587,6 +621,10 @@ Configuration.prototype._loadMaxError = function(options) {
);

this._maxErrors = maxErrors;

if (!this._isDefined('fix')) {
this._definedOptions.push('fix');
}
};

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

this._fix = fix;

if (!this._isDefined('fix')) {
this._definedOptions.push('fix');
}
};

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

if (!this._isDefined('preset')) {
this._definedOptions.push('preset');
}

// Process config from the preset
this._processConfig(this._presets[preset]);
};
Expand All @@ -646,9 +692,25 @@ Configuration.prototype._loadFileExtensions = function(extensions) {
typeof extensions === 'string' || Array.isArray(extensions),
'`fileExtensions` option requires string or array value'
);

this._fileExtensions = this._fileExtensions.concat(extensions).map(function(ext) {
return ext.toLowerCase();
});

if (!this._isDefined('fileExtensions')) {
this._definedOptions.push('fileExtensions');
}
};

/**
* Is option defined?
*
* @param {String} name - name of the option
*
* @return {Boolean}
*/
Configuration.prototype._isDefined = function(name) {
return this._definedOptions.indexOf(name) > -1;
};

/**
Expand All @@ -666,6 +728,10 @@ Configuration.prototype._loadExcludedFiles = function(masks) {
dot: true
});
}, this);

if (!this._isDefined('excludeFiles')) {
this._definedOptions.push('excludeFiles');
}
};

/**
Expand All @@ -688,6 +754,10 @@ Configuration.prototype._loadExtract = function(masks) {
dot: true
});
}, this);

if (!this._isDefined('extract')) {
this._definedOptions.push('extract');
}
};

/**
Expand Down
25 changes: 25 additions & 0 deletions test/specs/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,31 @@ describe('config/configuration', function() {
expect(configuration.getExcludedFileMasks()).to.deep.equal(['.git/**', 'node_modules/**']);
});

describe('for settings with default value when preset is used #2275', function() {
it('`excludeFiles`', function() {
configuration.registerPreset('test1', {});
configuration.load({
preset: 'test1',
excludeFiles: ['test']
});

expect(configuration.getExcludedFileMasks().length).to.equal(1);
expect(configuration.getExcludedFileMasks()[0]).to.equal('test');
});

it('`fileExtensions`', function() {
configuration.registerPreset('test1', {});
configuration.load({
preset: 'test1',
fileExtensions: ['.test']
});

expect(configuration.getFileExtensions().length).to.equal(1);
expect(configuration.getFileExtensions()[0]).to.equal('.test');
}
);
});

it('should set `fileExtensions` setting from presets', function() {
configuration.registerPreset('test1', {
fileExtensions: ['first']
Expand Down

0 comments on commit 0d027a7

Please sign in to comment.