forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope-enum.ts
41 lines (35 loc) · 1.04 KB
/
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
33
34
35
36
37
38
39
40
41
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';
import {SyncRule} from '@commitlint/types';
export const scopeEnum: SyncRule<
string[] | {delimiter?: RegExp; values?: string[]}
> = (parsed, when = 'always', config = []) => {
if (!parsed.scope) {
return [true, ''];
}
let delimiters = /\/|\\|,/g;
let value: string[] = [];
if (!Array.isArray(config)) {
if (config.delimiter) {
delimiters = config.delimiter;
}
value = config.values || [];
} else {
value = config;
}
// 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 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(', ')}]`,
]),
];
};