Skip to content

Commit

Permalink
feat(rules): create header-full-stop rule
Browse files Browse the repository at this point in the history
This is a simplified version of the subject-full-stop rule. Headers are always present, so we don’t need to check for empty-ness. If headers are not defined, the parser will throw with an error “expecting raw commit”.
  • Loading branch information
byCedric committed Jan 27, 2019
1 parent fa6168a commit 7448b7e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions @commitlint/rules/src/header-full-stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import message from '@commitlint/message';

export default (parsed, when, value) => {
const {header} = parsed;
const negated = when === 'never';
const hasStop = header[header.length - 1] === value;

return [
negated ? !hasStop : hasStop,
message(['header', negated ? 'may not' : 'must', 'end with full stop'])
];
};
37 changes: 37 additions & 0 deletions @commitlint/rules/src/header-full-stop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import parse from '@commitlint/parse';
import check from './header-full-stop';

const messages = {
with: `header.\n`,
without: `header\n`
};

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

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

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

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

test('without against "never ." should succeed', async t => {
const [actual] = check(await parsed.without, 'never', '.');
const expected = true;
t.is(actual, expected);
});
1 change: 1 addition & 0 deletions @commitlint/rules/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
'footer-max-length': require('./footer-max-length'),
'footer-max-line-length': require('./footer-max-line-length'),
'footer-min-length': require('./footer-min-length'),
'header-full-stop': require('./header-full-stop'),
'header-max-length': require('./header-max-length'),
'header-min-length': require('./header-min-length'),
'references-empty': require('./references-empty'),
Expand Down

0 comments on commit 7448b7e

Please sign in to comment.