diff --git a/@commitlint/rules/src/header-full-stop.js b/@commitlint/rules/src/header-full-stop.js new file mode 100644 index 00000000000..f30ce6d12cb --- /dev/null +++ b/@commitlint/rules/src/header-full-stop.js @@ -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']) + ]; +}; diff --git a/@commitlint/rules/src/header-full-stop.test.js b/@commitlint/rules/src/header-full-stop.test.js new file mode 100644 index 00000000000..5b41da82e3d --- /dev/null +++ b/@commitlint/rules/src/header-full-stop.test.js @@ -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); +}); diff --git a/@commitlint/rules/src/index.js b/@commitlint/rules/src/index.js index 4cf35614424..5779959b0b3 100644 --- a/@commitlint/rules/src/index.js +++ b/@commitlint/rules/src/index.js @@ -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'),