From 5383c9edcd9a351ea1c33ed49f47afed9b1cde6b Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Fri, 9 Jul 2021 21:50:38 +0100 Subject: [PATCH] feat(rules): allow body-case to accept an array of cases To keep the API consistent with all of the case rules the `body-case` rule now accepts an array of cases like all of the other case types Ref: #2631 --- @commitlint/rules/src/body-case.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/@commitlint/rules/src/body-case.ts b/@commitlint/rules/src/body-case.ts index 2676e2e48a..6c0291c22d 100644 --- a/@commitlint/rules/src/body-case.ts +++ b/@commitlint/rules/src/body-case.ts @@ -2,10 +2,12 @@ import {case as ensureCase} from '@commitlint/ensure'; import message from '@commitlint/message'; import {TargetCaseType, SyncRule} from '@commitlint/types'; -export const bodyCase: SyncRule = ( +const negated = (when?: string) => when === 'never'; + +export const bodyCase: SyncRule = ( parsed, when = 'always', - value = undefined + value = [] ) => { const {body} = parsed; @@ -13,11 +15,25 @@ export const bodyCase: SyncRule = ( return [true]; } - const negated = when === 'never'; + const checks = (Array.isArray(value) ? value : [value]).map((check) => { + if (typeof check === 'string') { + return { + when: 'always', + case: check, + }; + } + return check; + }); + + const result = checks.some((check) => { + const r = ensureCase(body, check.case); + return negated(check.when) ? !r : r; + }); + + const list = checks.map((c) => c.case).join(', '); - const result = ensureCase(body, value); return [ - negated ? !result : result, - message([`body must`, negated ? `not` : null, `be ${value}`]), + negated(when) ? !result : result, + message([`body must`, negated(when) ? `not` : null, `be ${list}`]), ]; };