Skip to content

Commit 4fc8d5d

Browse files
committed
feat: add references-empty rule
1 parent 1270b42 commit 4fc8d5d

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

@commitlint/core/src/rules/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
'header-max-length': require('./header-max-length'),
1414
'header-min-length': require('./header-min-length'),
1515
lang: require('./lang'),
16+
'references-empty': require('./references-empty'),
1617
'scope-case': require('./scope-case'),
1718
'scope-empty': require('./scope-empty'),
1819
'scope-enum': require('./scope-enum'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import message from '../library/message';
2+
3+
export default (parsed, when = 'never') => {
4+
const negated = when === 'always';
5+
const notEmpty = parsed.references.length > 0;
6+
return [
7+
negated ? !notEmpty : notEmpty,
8+
message(['references', negated ? 'must' : 'may not', 'be empty'])
9+
];
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import test from 'ava';
2+
import parse from '../library/parse';
3+
import referencesEmpty from './references-empty';
4+
5+
const messages = {
6+
plain: 'foo: bar',
7+
comment: 'foo: baz\n#1 Comment',
8+
reference: '#comment\nfoo: baz \nCloses #1',
9+
references: '#comment\nfoo: bar \nCloses #1, #2, #3'
10+
};
11+
12+
const parsed = {
13+
plain: parse(messages.plain),
14+
comment: parse(messages.comment),
15+
reference: parse(messages.reference),
16+
references: parse(messages.references)
17+
};
18+
19+
test('defaults to never and fails for plain', async t => {
20+
const [actual] = referencesEmpty(await parsed.plain);
21+
const expected = false;
22+
t.is(actual, expected);
23+
});
24+
25+
test('defaults to never and succeeds for reference', async t => {
26+
const [actual] = referencesEmpty(await parsed.reference);
27+
const expected = true;
28+
t.is(actual, expected);
29+
});
30+
31+
test('fails for comment with never', async t => {
32+
const [actual] = referencesEmpty(await parsed.comment, 'never');
33+
const expected = false;
34+
t.is(actual, expected);
35+
});
36+
37+
test('succeeds for comment with always', async t => {
38+
const [actual] = referencesEmpty(await parsed.comment, 'always');
39+
const expected = true;
40+
t.is(actual, expected);
41+
});
42+
43+
test('succeeds for reference with never', async t => {
44+
const [actual] = referencesEmpty(await parsed.reference, 'never');
45+
const expected = true;
46+
t.is(actual, expected);
47+
});
48+
49+
test('fails for reference with always', async t => {
50+
const [actual] = referencesEmpty(await parsed.reference, 'always');
51+
const expected = false;
52+
t.is(actual, expected);
53+
});
54+
55+
test('succeeds for references with never', async t => {
56+
const [actual] = referencesEmpty(await parsed.references, 'never');
57+
const expected = true;
58+
t.is(actual, expected);
59+
});
60+
61+
test('fails for references with always', async t => {
62+
const [actual] = referencesEmpty(await parsed.references, 'always');
63+
const expected = false;
64+
t.is(actual, expected);
65+
});

docs/reference-rules.md

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Rule configurations are either of type `array` residing on a key with the rule's
9090
0
9191
```
9292

93+
#### references-empty
94+
* **condition**: `references` has at least one entry
95+
* **rule**: `never`
96+
9397
#### scope-enum
9498
* **condition**: `scope` is found in value
9599
* **rule**: `always`

0 commit comments

Comments
 (0)