|
1 | | -import { Applicability, Case, Level, Rule } from '@commitlint/load'; |
| 1 | +import type { TargetCaseType, RuleConfigTuple, RuleConfigCondition } from '@commitlint/types'; |
| 2 | +import { RuleConfigSeverity } from '@commitlint/types'; |
2 | 3 | import { wordCase } from './utils'; |
3 | 4 |
|
4 | 5 | type ValidateRulesWithRuleValue = (value: string, ruleValue: number) => boolean; |
5 | | -type ValidateRulesWithCaseValue = (value: string, ruleValue: Case, inclusive?: boolean) => boolean; |
| 6 | +type ValidateRulesWithCaseValue = (value: string, ruleValue: TargetCaseType, inclusive?: boolean) => boolean; |
6 | 7 | type ValidatorRulesWithoutValue = (value: string) => boolean; |
7 | 8 | type Validator = ValidateRulesWithRuleValue | ValidatorRulesWithoutValue | ValidateRulesWithCaseValue; |
8 | 9 |
|
9 | 10 | export function validate( |
10 | 11 | validators: { |
11 | 12 | value: string; |
12 | | - rule: Rule<number | Case | Case[] | undefined> | undefined; |
| 13 | + rule: RuleConfigTuple<number | TargetCaseType | TargetCaseType[] | undefined> | undefined; |
13 | 14 | validator: Validator; |
14 | | - message: (length?: number | Case | Case[], applicable?: Applicability) => string; |
| 15 | + message: (length?: number | TargetCaseType | TargetCaseType[], applicable?: RuleConfigCondition) => string; |
15 | 16 | }[] |
16 | 17 | ): string | true { |
17 | 18 | const errorMessages: string[] = validators |
18 | 19 | .map((v) => { |
19 | | - if (v.rule == undefined) { |
| 20 | + if (v.rule === undefined) { |
20 | 21 | return true; |
21 | 22 | } |
22 | 23 |
|
23 | 24 | const [level, applicable, ruleValue] = v.rule; |
24 | 25 |
|
25 | | - if (level !== Level.Error) { |
| 26 | + if (level !== RuleConfigSeverity.Error) { |
26 | 27 | return true; |
27 | 28 | } |
28 | 29 |
|
29 | 30 | let valid: boolean; |
30 | | - if (Array.isArray(ruleValue) && applicable == 'never') { |
| 31 | + if (Array.isArray(ruleValue) && applicable === 'never') { |
31 | 32 | valid = v.validator(v.value, ruleValue as never, false); |
32 | 33 | } else { |
33 | 34 | valid = v.validator(v.value, ruleValue as never); |
34 | 35 | } |
35 | 36 |
|
36 | | - if (applicable == 'never') { |
| 37 | + if (applicable === 'never') { |
37 | 38 | valid = !valid; |
38 | 39 | } |
39 | 40 |
|
@@ -68,14 +69,14 @@ export function emptyValidator(value: string): boolean { |
68 | 69 | return value.length < 1; |
69 | 70 | } |
70 | 71 |
|
71 | | -export function caseValidator(value: string, rule: Case | Case[], inclusive = true): boolean { |
| 72 | +export function caseValidator(value: string, rule: TargetCaseType | TargetCaseType[], inclusive = true): boolean { |
72 | 73 | if (typeof rule === 'string') { |
73 | 74 | return value == wordCase(value, rule); |
74 | 75 | } |
75 | 76 |
|
76 | 77 | if (inclusive) { |
77 | | - return rule.every((r) => wordCase(value, r) == value); |
| 78 | + return rule.every((r) => wordCase(value, r) === value); |
78 | 79 | } |
79 | 80 |
|
80 | | - return rule.some((r) => wordCase(value, r) == value); |
| 81 | + return rule.some((r) => wordCase(value, r) === value); |
81 | 82 | } |
0 commit comments