Skip to content

Commit f45ddad

Browse files
committed
feat(core): Implement footer-contains rules
1 parent 56e1f7c commit f45ddad

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default (value, regex) => {
2+
if (value === undefined) {
3+
return false;
4+
}
5+
if (!regex instanceof RegExp) {
6+
return false;
7+
}
8+
return regex.test(value);
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import test from 'ava';
2+
import ensure from './ensure-contains';
3+
4+
test('false for no params', t => {
5+
const actual = ensure();
6+
t.is(actual, false);
7+
});
8+
9+
test('true for /^foo/gi against foo', t => {
10+
const actual = ensure('foo', /^foo/gi);
11+
t.is(actual, true);
12+
});
13+
14+
test('false for /^foo/gi against notfoo', t => {
15+
const actual = ensure('notfoo', /^foo/gi);
16+
t.is(actual, false);
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ensureContains from '../library/ensure-contains';
2+
import message from '../library/message';
3+
4+
export default (parsed, when, value) => {
5+
if (!parsed.scope) {
6+
return [true, ''];
7+
}
8+
9+
const negated = when === 'never';
10+
const result = value.length === 0 || ensureContains(parsed.scope, value);
11+
12+
return [
13+
negated ? !result : result,
14+
message([
15+
`footer content must`,
16+
negated ? `not` : null,
17+
`pass the regular expression: ${value.toString()}`
18+
])
19+
];
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import test from 'ava';
2+
import parse from '../library/parse';
3+
import footerContains from './footer-contains';
4+
5+
const messages = {
6+
empty: 'foo(bar): baz',
7+
matched: 'foo(bar): baz\n\nbody\n\nqux',
8+
unmatched: 'foo(bar): baz\n\nbody\n\nquux'
9+
};
10+
11+
const parsed = {
12+
empty: parse(messages.empty),
13+
matched: parse(messages.matched),
14+
unmatched: parse(messages.unmatched)
15+
};
16+
17+
test('footer-contains with no footer should not succeed', async t => {
18+
const [actual] = footerContains(await parsed.empty, 'always', []);
19+
const expected = false;
20+
t.deepEqual(actual, expected);
21+
});
22+
23+
test('footer-contains with matching footer should succeed', async t => {
24+
const [actual] = footerContains(await parsed.matched, 'never', []);
25+
const expected = true;
26+
t.deepEqual(actual, expected);
27+
});
28+
29+
test('footer-contains with non-matching footer should not succeed', async t => {
30+
const [actual] = footerContains(await parsed.unmatched, 'never', []);
31+
const expected = false;
32+
t.deepEqual(actual, expected);
33+
});

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default {
55
'body-max-length': require('./body-max-length'),
66
'body-min-length': require('./body-min-length'),
77
'body-tense': require('./body-tense'),
8+
'footer-contains': require('./footer-contains'),
89
'footer-empty': require('./footer-empty'),
910
'footer-leading-blank': require('./footer-leading-blank'),
1011
'footer-max-length': require('./footer-max-length'),

0 commit comments

Comments
 (0)