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

Commit 2481d63

Browse files
committed
Configuration: allow default preset override
1 parent 5047150 commit 2481d63

File tree

6 files changed

+62
-14
lines changed

6 files changed

+62
-14
lines changed

lib/config/configuration.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var defaults = {
77
maxErrors: 50
88
};
99

10+
var _ = require('lodash');
11+
1012
var BUILTIN_OPTIONS = {
1113
plugins: true,
1214
preset: true,
@@ -882,6 +884,8 @@ Configuration.prototype.getRegisteredRules = function() {
882884
* @param {Object} presetConfig
883885
*/
884886
Configuration.prototype.registerPreset = function(presetName, presetConfig) {
887+
assert(_.isPlainObject(presetConfig), 'Preset should be an object');
888+
885889
this._presets[presetName] = presetConfig;
886890
};
887891

lib/config/node-configuration.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,21 @@ NodeConfiguration.prototype._loadPlugin = function(plugin, config) {
146146
* @protected
147147
*/
148148
NodeConfiguration.prototype._loadPreset = function(preset, config) {
149-
var registeredPresets = this.getRegisteredPresets();
149+
var name = path.basename(preset).split('.')[0];
150150

151-
if (preset in registeredPresets) {
152-
Configuration.prototype._loadPreset.call(this, preset);
151+
try {
152+
this.registerPreset(name, this.loadExternal(preset, 'preset', config));
153+
} catch (e) {
154+
var registeredPresets = this.getRegisteredPresets();
153155

154-
} else {
155-
var name = path.basename(preset).split('.')[0];
156-
157-
// Suppress it, since missing preset error will be handled by the caller
158-
try {
159-
this.registerPreset(name, this.loadExternal(preset, 'preset', config));
160-
} catch (e) {}
161-
162-
Configuration.prototype._loadPreset.call(this, name);
156+
if (preset in registeredPresets) {
157+
Configuration.prototype._loadPreset.call(this, preset);
158+
return;
159+
}
163160
}
161+
162+
// If preset is an external module, error will be thrown by the caller
163+
Configuration.prototype._loadPreset.call(this, name);
164164
};
165165

166166
/**

test/data/configs/modules/node_modules/jscs-preset-wikimedia/index.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/data/configs/modules/node_modules/jscs-preset-wikimedia/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/specs/config/configuration.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ describe('config/configuration', function() {
236236
});
237237
});
238238

239+
describe('registerPreset', function() {
240+
it('should throw if preset is not an object', function() {
241+
expect(
242+
configuration.registerPreset.bind(configuration, 'test', undefined)
243+
).to.throw('Preset should be an object')
244+
});
245+
});
246+
239247
describe('getFix', function() {
240248
it('should enable "fix" option', function() {
241249
configuration.load({ fix: true });
@@ -486,6 +494,16 @@ describe('config/configuration', function() {
486494
expect(configuration.getProcessedConfig().preset).to.equal('2');
487495
});
488496

497+
it('should override default preset', function() {
498+
configuration.registerDefaultRules();
499+
configuration.registerDefaultPresets();
500+
configuration.registerPreset('wikimedia', {});
501+
502+
configuration.load({preset: 'wikimedia'});
503+
504+
expect(configuration.getConfiguredRules().length).to.equal(0);
505+
});
506+
489507
it('should override `maxErrors` setting', function() {
490508
configuration.override({maxErrors: 2});
491509
configuration.load({maxErrors: 1});

test/specs/config/node-configuration.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ describe('modules/config/node-configuration', function() {
3737
});
3838

3939
it('should load absolute path', function() {
40-
expect(configuration.loadExternal(
40+
expect(
41+
configuration.loadExternal(
4142
path.resolve('./test/data/plugin/plugin'),
4243
'plugin'
43-
)).to.be.a('function');
44+
)
45+
).to.be.a('function');
4446
});
4547

4648
it('should load without "jscs" prefix node module', function() {
@@ -71,6 +73,24 @@ describe('modules/config/node-configuration', function() {
7173
stub.restore();
7274
});
7375

76+
it('should override default preset with module preset', function() {
77+
var way = path.resolve('./test/data/configs/modules');
78+
var stub = sinon.stub(process, 'cwd');
79+
80+
stub.returns(way);
81+
82+
configuration = new NodeConfiguration();
83+
84+
configuration.registerDefaultRules();
85+
configuration.registerDefaultPresets();
86+
configuration.load({
87+
preset: 'wikimedia'
88+
});
89+
stub.restore();
90+
91+
expect(configuration.getConfiguredRules().length).to.equal(0);
92+
});
93+
7494
it('should load preset with "jscs-config" prefix without cwd', function() {
7595
var config = path.resolve('./test/data/configs/modules/config.json');
7696

0 commit comments

Comments
 (0)