From 4fc8d5d9d21209c8e6eef08341896e69c8272659 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Mon, 6 Nov 2017 16:11:50 +0100 Subject: [PATCH] feat: add references-empty rule --- @commitlint/core/src/rules/index.js | 1 + .../core/src/rules/references-empty.js | 10 +++ .../core/src/rules/references-empty.test.js | 65 +++++++++++++++++++ docs/reference-rules.md | 4 ++ 4 files changed, 80 insertions(+) create mode 100644 @commitlint/core/src/rules/references-empty.js create mode 100644 @commitlint/core/src/rules/references-empty.test.js diff --git a/@commitlint/core/src/rules/index.js b/@commitlint/core/src/rules/index.js index 4d62e894b8..13eaa9a995 100644 --- a/@commitlint/core/src/rules/index.js +++ b/@commitlint/core/src/rules/index.js @@ -13,6 +13,7 @@ export default { 'header-max-length': require('./header-max-length'), 'header-min-length': require('./header-min-length'), lang: require('./lang'), + 'references-empty': require('./references-empty'), 'scope-case': require('./scope-case'), 'scope-empty': require('./scope-empty'), 'scope-enum': require('./scope-enum'), diff --git a/@commitlint/core/src/rules/references-empty.js b/@commitlint/core/src/rules/references-empty.js new file mode 100644 index 0000000000..0daa89f1f1 --- /dev/null +++ b/@commitlint/core/src/rules/references-empty.js @@ -0,0 +1,10 @@ +import message from '../library/message'; + +export default (parsed, when = 'never') => { + const negated = when === 'always'; + const notEmpty = parsed.references.length > 0; + return [ + negated ? !notEmpty : notEmpty, + message(['references', negated ? 'must' : 'may not', 'be empty']) + ]; +}; diff --git a/@commitlint/core/src/rules/references-empty.test.js b/@commitlint/core/src/rules/references-empty.test.js new file mode 100644 index 0000000000..b3245b6cab --- /dev/null +++ b/@commitlint/core/src/rules/references-empty.test.js @@ -0,0 +1,65 @@ +import test from 'ava'; +import parse from '../library/parse'; +import referencesEmpty from './references-empty'; + +const messages = { + plain: 'foo: bar', + comment: 'foo: baz\n#1 Comment', + reference: '#comment\nfoo: baz \nCloses #1', + references: '#comment\nfoo: bar \nCloses #1, #2, #3' +}; + +const parsed = { + plain: parse(messages.plain), + comment: parse(messages.comment), + reference: parse(messages.reference), + references: parse(messages.references) +}; + +test('defaults to never and fails for plain', async t => { + const [actual] = referencesEmpty(await parsed.plain); + const expected = false; + t.is(actual, expected); +}); + +test('defaults to never and succeeds for reference', async t => { + const [actual] = referencesEmpty(await parsed.reference); + const expected = true; + t.is(actual, expected); +}); + +test('fails for comment with never', async t => { + const [actual] = referencesEmpty(await parsed.comment, 'never'); + const expected = false; + t.is(actual, expected); +}); + +test('succeeds for comment with always', async t => { + const [actual] = referencesEmpty(await parsed.comment, 'always'); + const expected = true; + t.is(actual, expected); +}); + +test('succeeds for reference with never', async t => { + const [actual] = referencesEmpty(await parsed.reference, 'never'); + const expected = true; + t.is(actual, expected); +}); + +test('fails for reference with always', async t => { + const [actual] = referencesEmpty(await parsed.reference, 'always'); + const expected = false; + t.is(actual, expected); +}); + +test('succeeds for references with never', async t => { + const [actual] = referencesEmpty(await parsed.references, 'never'); + const expected = true; + t.is(actual, expected); +}); + +test('fails for references with always', async t => { + const [actual] = referencesEmpty(await parsed.references, 'always'); + const expected = false; + t.is(actual, expected); +}); diff --git a/docs/reference-rules.md b/docs/reference-rules.md index 8058544605..f2ccb865c1 100644 --- a/docs/reference-rules.md +++ b/docs/reference-rules.md @@ -90,6 +90,10 @@ Rule configurations are either of type `array` residing on a key with the rule's 0 ``` +#### references-empty +* **condition**: `references` has at least one entry +* **rule**: `never` + #### scope-enum * **condition**: `scope` is found in value * **rule**: `always`