Skip to content

Commit 5383c9e

Browse files
committed
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
1 parent c3bef38 commit 5383c9e

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

Diff for: @commitlint/rules/src/body-case.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@ import {case as ensureCase} from '@commitlint/ensure';
22
import message from '@commitlint/message';
33
import {TargetCaseType, SyncRule} from '@commitlint/types';
44

5-
export const bodyCase: SyncRule<TargetCaseType> = (
5+
const negated = (when?: string) => when === 'never';
6+
7+
export const bodyCase: SyncRule<TargetCaseType | TargetCaseType[]> = (
68
parsed,
79
when = 'always',
8-
value = undefined
10+
value = []
911
) => {
1012
const {body} = parsed;
1113

1214
if (!body) {
1315
return [true];
1416
}
1517

16-
const negated = when === 'never';
18+
const checks = (Array.isArray(value) ? value : [value]).map((check) => {
19+
if (typeof check === 'string') {
20+
return {
21+
when: 'always',
22+
case: check,
23+
};
24+
}
25+
return check;
26+
});
27+
28+
const result = checks.some((check) => {
29+
const r = ensureCase(body, check.case);
30+
return negated(check.when) ? !r : r;
31+
});
32+
33+
const list = checks.map((c) => c.case).join(', ');
1734

18-
const result = ensureCase(body, value);
1935
return [
20-
negated ? !result : result,
21-
message([`body must`, negated ? `not` : null, `be ${value}`]),
36+
negated(when) ? !result : result,
37+
message([`body must`, negated(when) ? `not` : null, `be ${list}`]),
2238
];
2339
};

0 commit comments

Comments
 (0)