Skip to content

feat(rules): allow body-case to accept an array of cases #2671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions @commitlint/rules/src/body-case.ts
Original file line number Diff line number Diff line change
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}`]),
];
};