Skip to content

Commit b60adbc

Browse files
committedDec 26, 2017
style: throw instructive error for non-existent rules
1 parent a530981 commit b60adbc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎@commitlint/core/src/lint.js

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ export default async (message, rules = {}, opts = {}) => {
1616
// Parse the commit message
1717
const parsed = await parse(message, undefined, opts.parserOpts);
1818

19+
// Find invalid rules configs
20+
const missing = Object.keys(rules).filter(
21+
name => typeof implementations[name] !== 'function'
22+
);
23+
24+
if (missing.length > 0) {
25+
const names = Object.keys(implementations);
26+
throw new RangeError(
27+
`Found missing rule names: ${missing.join(
28+
', '
29+
)}. Supported rule names are: ${names.join(', ')}`
30+
);
31+
}
32+
1933
// Validate against all rules
2034
const results = entries(rules)
2135
.filter(entry => {
@@ -32,6 +46,7 @@ export default async (message, rules = {}, opts = {}) => {
3246
}
3347

3448
const rule = implementations[name];
49+
3550
const [valid, message] = rule(parsed, when, value);
3651

3752
return {

‎@commitlint/core/src/lint.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ test('positive on stub message and opts', async t => {
5252
);
5353
t.true(actual.valid);
5454
});
55+
56+
test('should throw for invalid rule names', async t => {
57+
const error = await t.throws(
58+
lint('foo', {foo: [2, 'always'], bar: [1, 'never']})
59+
);
60+
61+
t.is(error.message.indexOf('Found invalid rule names: foo, bar'), 0);
62+
});

0 commit comments

Comments
 (0)