Skip to content

Commit

Permalink
feat(rules): add body-full-stop rule (#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelomrani1 authored Oct 1, 2020
1 parent 00e9241 commit 7767ca2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
50 changes: 50 additions & 0 deletions @commitlint/rules/src/body-full-stop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import parse from '@commitlint/parse';
import {bodyFullStop} from './body-full-stop';

const messages = {
empty: 'test:\n',
with: `test: subject\n\nbody.`,
without: `test: subject\n\nbody`,
};

const parsed = {
empty: parse(messages.empty),
with: parse(messages.with),
without: parse(messages.without),
};

test('empty against "always" should succeed', async () => {
const [actual] = bodyFullStop(await parsed.empty, 'always', '.');
const expected = true;
expect(actual).toEqual(expected);
});

test('empty against "never ." should succeed', async () => {
const [actual] = bodyFullStop(await parsed.empty, 'never', '.');
const expected = true;
expect(actual).toEqual(expected);
});

test('with against "always ." should succeed', async () => {
const [actual] = bodyFullStop(await parsed.with, 'always', '.');
const expected = true;
expect(actual).toEqual(expected);
});

test('with against "never ." should fail', async () => {
const [actual] = bodyFullStop(await parsed.with, 'never', '.');
const expected = false;
expect(actual).toEqual(expected);
});

test('without against "always ." should fail', async () => {
const [actual] = bodyFullStop(await parsed.without, 'always', '.');
const expected = false;
expect(actual).toEqual(expected);
});

test('without against "never ." should succeed', async () => {
const [actual] = bodyFullStop(await parsed.without, 'never', '.');
const expected = true;
expect(actual).toEqual(expected);
});
22 changes: 22 additions & 0 deletions @commitlint/rules/src/body-full-stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import message from '@commitlint/message';
import {SyncRule} from '@commitlint/types';

export const bodyFullStop: SyncRule<string> = (
parsed,
when = 'always',
value = '.'
) => {
const input = parsed.body;

if (!input) {
return [true];
}

const negated = when === 'never';
const hasStop = input[input.length - 1] === value;

return [
negated ? !hasStop : hasStop,
message(['body', negated ? 'may not' : 'must', 'end with full stop']),
];
};
2 changes: 2 additions & 0 deletions @commitlint/rules/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {bodyCase} from './body-case';
import {bodyEmpty} from './body-empty';
import {bodyFullStop} from './body-full-stop';
import {bodyLeadingBlank} from './body-leading-blank';
import {bodyMaxLength} from './body-max-length';
import {bodyMaxLineLength} from './body-max-line-length';
Expand Down Expand Up @@ -34,6 +35,7 @@ import {typeMinLength} from './type-min-length';
export default {
'body-case': bodyCase,
'body-empty': bodyEmpty,
'body-full-stop': bodyFullStop,
'body-leading-blank': bodyLeadingBlank,
'body-max-length': bodyMaxLength,
'body-max-line-length': bodyMaxLineLength,
Expand Down
1 change: 1 addition & 0 deletions @commitlint/types/src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type EnumRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
export type RulesConfig<V = RuleConfigQuality.User> = {
'body-case': CaseRuleConfig<V>;
'body-empty': RuleConfig<V>;
'body-full-stop': RuleConfig<V>;
'body-leading-blank': RuleConfig<V>;
'body-max-length': LengthRuleConfig<V>;
'body-max-line-length': LengthRuleConfig<V>;
Expand Down
10 changes: 10 additions & 0 deletions docs/reference-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ Rule configurations are either of type `array` residing on a key with the rule's

### Available rules

#### body-full-stop

- **condition**: `body` ends with `value`
- **rule**: `never`
- **value**

```
'.'
```

#### body-leading-blank

- **condition**: `body` begins with blank line
Expand Down

0 comments on commit 7767ca2

Please sign in to comment.