Skip to content

Commit 1dc784e

Browse files
committed
fix: error is thrown if there's no scope-{enum,type} rule at all
`type`, `scope` and `scopeOverrides` in cz-customizable config is/are now silently ignored if `type-num`, `scope-enum` rule doesn't exist in commitlint config. Closes #3
1 parent bb315a8 commit 1dc784e

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

lib/config.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,29 @@ function get(pathOrCzConfig, defaultConfig) {
3838
}
3939

4040
// Converts the `scopes` and `scopeOverrides` into `scope-enum` rule.
41-
if (typeof czConfig.scopes !== 'undefined' || typeof czConfig.scopeOverrides !== 'undefined') {
41+
const hasScopeEnum = Object.prototype.hasOwnProperty.call(config.rules, 'scope-enum');
42+
43+
if (
44+
hasScopeEnum &&
45+
(typeof czConfig.scopes !== 'undefined' || typeof czConfig.scopeOverrides !== 'undefined')
46+
) {
4247
config.rules['scope-enum'][2] = Scopes.get(czConfig);
4348
}
4449

4550
// Removes empty `scope-enum` rule.
46-
if (!config.rules['scope-enum'][2].length) {
51+
if (hasScopeEnum && !config.rules['scope-enum'][2].length) {
4752
delete config.rules['scope-enum'];
4853
}
4954

5055
// Converts the `types` into `type-enum` rule.
51-
if (typeof czConfig.types !== 'undefined') {
56+
const hasTypeEnum = Object.prototype.hasOwnProperty.call(config.rules, 'type-enum');
57+
58+
if (hasTypeEnum && typeof czConfig.types !== 'undefined') {
5259
config.rules['type-enum'][2] = Types.get(czConfig);
5360
}
5461

5562
// Removes empty `type-enum` rule.
56-
if (!config.rules['type-enum'][2].length) {
63+
if (hasTypeEnum && !config.rules['type-enum'][2].length) {
5764
delete config.rules['type-enum'];
5865
}
5966

test/config.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,91 @@ describe('config', function () {
173173
assert.deepStrictEqual(Config.get(configPath, defaultConfig), expected);
174174
}
175175
);
176+
177+
it('should ignore `scopes` and `scopeOverrides` when commitlint config has no `scope-enum` rule',
178+
function () {
179+
const configPath = path.join(__dirname, 'fixtures/.cz-config.js');
180+
181+
const defaultConfig = {
182+
rules: {
183+
'body-leading-blank' : [
184+
2,
185+
'always',
186+
],
187+
'type-enum' : [
188+
1,
189+
'never',
190+
[
191+
'type-3',
192+
'type-4',
193+
],
194+
],
195+
},
196+
};
197+
198+
const expected = {
199+
rules: {
200+
'body-leading-blank' : [
201+
2,
202+
'always',
203+
],
204+
'type-enum' : [
205+
1,
206+
'never',
207+
[
208+
'type-1',
209+
'type-2',
210+
],
211+
],
212+
}
213+
};
214+
215+
assert.deepStrictEqual(Config.get(configPath, defaultConfig), expected);
216+
}
217+
);
218+
219+
it('should ignore `types` when commitlint config has no `type-enum` rule',
220+
function () {
221+
const configPath = path.join(__dirname, 'fixtures/.cz-config.js');
222+
223+
const defaultConfig = {
224+
rules: {
225+
'body-leading-blank' : [
226+
2,
227+
'always',
228+
],
229+
'scope-enum': [
230+
1,
231+
'never',
232+
[
233+
'scope-4',
234+
'scope-5',
235+
'scope-6',
236+
],
237+
],
238+
},
239+
};
240+
241+
const expected = {
242+
rules: {
243+
'body-leading-blank' : [
244+
2,
245+
'always',
246+
],
247+
'scope-enum': [
248+
1,
249+
'never',
250+
[
251+
'scope-1',
252+
'scope-2',
253+
'scope-3',
254+
],
255+
],
256+
}
257+
};
258+
259+
assert.deepStrictEqual(Config.get(configPath, defaultConfig), expected);
260+
}
261+
);
176262
});
177263
});

0 commit comments

Comments
 (0)