Skip to content

Commit 7767ca2

Browse files
authored
feat(rules): add body-full-stop rule (#2144)
1 parent 00e9241 commit 7767ca2

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import parse from '@commitlint/parse';
2+
import {bodyFullStop} from './body-full-stop';
3+
4+
const messages = {
5+
empty: 'test:\n',
6+
with: `test: subject\n\nbody.`,
7+
without: `test: subject\n\nbody`,
8+
};
9+
10+
const parsed = {
11+
empty: parse(messages.empty),
12+
with: parse(messages.with),
13+
without: parse(messages.without),
14+
};
15+
16+
test('empty against "always" should succeed', async () => {
17+
const [actual] = bodyFullStop(await parsed.empty, 'always', '.');
18+
const expected = true;
19+
expect(actual).toEqual(expected);
20+
});
21+
22+
test('empty against "never ." should succeed', async () => {
23+
const [actual] = bodyFullStop(await parsed.empty, 'never', '.');
24+
const expected = true;
25+
expect(actual).toEqual(expected);
26+
});
27+
28+
test('with against "always ." should succeed', async () => {
29+
const [actual] = bodyFullStop(await parsed.with, 'always', '.');
30+
const expected = true;
31+
expect(actual).toEqual(expected);
32+
});
33+
34+
test('with against "never ." should fail', async () => {
35+
const [actual] = bodyFullStop(await parsed.with, 'never', '.');
36+
const expected = false;
37+
expect(actual).toEqual(expected);
38+
});
39+
40+
test('without against "always ." should fail', async () => {
41+
const [actual] = bodyFullStop(await parsed.without, 'always', '.');
42+
const expected = false;
43+
expect(actual).toEqual(expected);
44+
});
45+
46+
test('without against "never ." should succeed', async () => {
47+
const [actual] = bodyFullStop(await parsed.without, 'never', '.');
48+
const expected = true;
49+
expect(actual).toEqual(expected);
50+
});
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import message from '@commitlint/message';
2+
import {SyncRule} from '@commitlint/types';
3+
4+
export const bodyFullStop: SyncRule<string> = (
5+
parsed,
6+
when = 'always',
7+
value = '.'
8+
) => {
9+
const input = parsed.body;
10+
11+
if (!input) {
12+
return [true];
13+
}
14+
15+
const negated = when === 'never';
16+
const hasStop = input[input.length - 1] === value;
17+
18+
return [
19+
negated ? !hasStop : hasStop,
20+
message(['body', negated ? 'may not' : 'must', 'end with full stop']),
21+
];
22+
};

@commitlint/rules/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {bodyCase} from './body-case';
22
import {bodyEmpty} from './body-empty';
3+
import {bodyFullStop} from './body-full-stop';
34
import {bodyLeadingBlank} from './body-leading-blank';
45
import {bodyMaxLength} from './body-max-length';
56
import {bodyMaxLineLength} from './body-max-line-length';
@@ -34,6 +35,7 @@ import {typeMinLength} from './type-min-length';
3435
export default {
3536
'body-case': bodyCase,
3637
'body-empty': bodyEmpty,
38+
'body-full-stop': bodyFullStop,
3739
'body-leading-blank': bodyLeadingBlank,
3840
'body-max-length': bodyMaxLength,
3941
'body-max-line-length': bodyMaxLineLength,

@commitlint/types/src/rules.ts

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export type EnumRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
9191
export type RulesConfig<V = RuleConfigQuality.User> = {
9292
'body-case': CaseRuleConfig<V>;
9393
'body-empty': RuleConfig<V>;
94+
'body-full-stop': RuleConfig<V>;
9495
'body-leading-blank': RuleConfig<V>;
9596
'body-max-length': LengthRuleConfig<V>;
9697
'body-max-line-length': LengthRuleConfig<V>;

docs/reference-rules.md

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ Rule configurations are either of type `array` residing on a key with the rule's
4242

4343
### Available rules
4444

45+
#### body-full-stop
46+
47+
- **condition**: `body` ends with `value`
48+
- **rule**: `never`
49+
- **value**
50+
51+
```
52+
'.'
53+
```
54+
4555
#### body-leading-blank
4656

4757
- **condition**: `body` begins with blank line

0 commit comments

Comments
 (0)