|
1 | | -import { Rules } from '@commitlint/load'; |
| 1 | +import { Rules, Level } from '@commitlint/load'; |
2 | 2 | import { ChoiceOptions } from 'inquirer'; |
3 | 3 | import { whenFactory } from '../when'; |
4 | 4 | import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators'; |
@@ -36,13 +36,39 @@ export function validatorFactory(rules: Rules) { |
36 | 36 | }; |
37 | 37 | } |
38 | 38 |
|
39 | | -export function choicesFactory(rules: Rules) { |
40 | | - let choices: ChoiceOptions[] | undefined; |
41 | | - if (rules['scope-enum']) { |
42 | | - const [, , scopeEnum] = rules['scope-enum']; |
43 | | - if (scopeEnum && scopeEnum.length > 0) { |
44 | | - choices = [...scopeEnum.map(scope => ({ name: scope, value: scope })), { name: ':skip', value: '' }]; |
| 39 | +function parseEmptyScopeRule(rule: Rules['scope-empty']): [boolean, ChoiceOptions | undefined] { |
| 40 | + const skipChoice: ChoiceOptions = { name: ':skip', value: '' }; |
| 41 | + if (rule !== undefined) { |
| 42 | + const [level, applicability] = rule; |
| 43 | + if (level === Level.Error) { |
| 44 | + if (applicability === 'always') { |
| 45 | + return [true, skipChoice]; |
| 46 | + } |
45 | 47 | } |
| 48 | + return [false, undefined]; |
| 49 | + } |
| 50 | + return [true, skipChoice]; |
| 51 | +} |
| 52 | + |
| 53 | +function parseScopeEnumRule(rule: Rules['scope-enum']): [boolean, ChoiceOptions[] | undefined] { |
| 54 | + if (rule !== undefined) { |
| 55 | + const [, , scopeEnum] = rule; |
| 56 | + return [true, scopeEnum.map(scope => ({ name: scope, value: scope }))]; |
| 57 | + } |
| 58 | + return [false, undefined]; |
| 59 | +} |
| 60 | + |
| 61 | +export function choicesFactory(rules: Rules): ChoiceOptions[] | undefined { |
| 62 | + const choices: ChoiceOptions[] = []; |
| 63 | + |
| 64 | + const [containsSkipChoice, skipChoice] = parseEmptyScopeRule(rules['scope-empty']); |
| 65 | + if (containsSkipChoice) { |
| 66 | + choices.push(skipChoice as ChoiceOptions); |
| 67 | + } |
| 68 | + |
| 69 | + const [containsScopeEnumChoices, scopeEnumChoices] = parseScopeEnumRule(rules['scope-enum']); |
| 70 | + if (containsScopeEnumChoices) { |
| 71 | + choices.unshift(...(scopeEnumChoices as ChoiceOptions[])); |
46 | 72 | } |
47 | 73 |
|
48 | 74 | return choices; |
|
0 commit comments