forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope-enum.ts
32 lines (28 loc) · 838 Bytes
/
scope-enum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';
import {SyncRule} from '@commitlint/types';
export const scopeEnum: SyncRule<string[]> = (
parsed,
when = 'always',
value = []
) => {
if (!parsed.scope) {
return [true, ''];
}
// Scopes may contain slash or comma delimiters to separate them and mark them as individual segments.
// This means that each of these segments should be tested separately with `ensure`.
const delimiters = /\/|\\|, ?/g;
const scopeSegments = parsed.scope.split(delimiters);
const negated = when === 'never';
const result =
value.length === 0 ||
scopeSegments.every((scope) => ensure.enum(scope, value));
return [
negated ? !result : result,
message([
`scope must`,
negated ? `not` : null,
`be one of [${value.join(', ')}]`,
]),
];
};