Skip to content

Commit a2c2593

Browse files
committed
fix(scope-maker): exclude skip choice when required
1 parent e9c8edc commit a2c2593

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/prompts/scope-maker.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Rules } from '@commitlint/load';
1+
import { Rules, Level } from '@commitlint/load';
22
import { ChoiceOptions } from 'inquirer';
33
import { whenFactory } from '../when';
44
import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators';
@@ -36,13 +36,39 @@ export function validatorFactory(rules: Rules) {
3636
};
3737
}
3838

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+
}
4547
}
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[]));
4672
}
4773

4874
return choices;

0 commit comments

Comments
 (0)