Skip to content

Commit

Permalink
feat(rules): allow body-case to accept an array of cases
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AdeAttwood committed Jul 13, 2021
1 parent c3bef38 commit 5383c9e
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions @commitlint/rules/src/body-case.ts
Expand Up @@ -2,22 +2,38 @@ import {case as ensureCase} from '@commitlint/ensure';
import message from '@commitlint/message';
import {TargetCaseType, SyncRule} from '@commitlint/types';

export const bodyCase: SyncRule<TargetCaseType> = (
const negated = (when?: string) => when === 'never';

export const bodyCase: SyncRule<TargetCaseType | TargetCaseType[]> = (
parsed,
when = 'always',
value = undefined
value = []
) => {
const {body} = parsed;

if (!body) {
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}`]),
];
};

0 comments on commit 5383c9e

Please sign in to comment.