Skip to content

Commit c07687e

Browse files
committed
fix: rule should pass if commit component raw is null (fix #18)
1 parent 99c88a7 commit c07687e

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

lib/src/ensure.dart

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ bool ensureCase(dynamic raw, Case target) {
66
if (raw is Iterable) {
77
return raw.isEmpty || raw.every((element) => ensureCase(element, target));
88
}
9-
if (raw is! String) {
10-
return false;
11-
}
12-
switch (target) {
13-
case Case.lower:
14-
return raw.toLowerCase() == raw;
15-
case Case.upper:
16-
return raw.toUpperCase() == raw;
17-
case Case.camel:
18-
return raw.toCamelCase() == raw;
19-
case Case.kebab:
20-
return raw.toKebabCase() == raw;
21-
case Case.pascal:
22-
return raw.toPascalCase() == raw;
23-
case Case.sentence:
24-
return raw.toSentenceCase() == raw;
25-
case Case.snake:
26-
return raw.toSnakeCase() == raw;
27-
case Case.capital:
28-
return raw.toCapitalCase() == raw;
9+
if (raw is String) {
10+
switch (target) {
11+
case Case.lower:
12+
return raw.toLowerCase() == raw;
13+
case Case.upper:
14+
return raw.toUpperCase() == raw;
15+
case Case.camel:
16+
return raw.toCamelCase() == raw;
17+
case Case.kebab:
18+
return raw.toKebabCase() == raw;
19+
case Case.pascal:
20+
return raw.toPascalCase() == raw;
21+
case Case.sentence:
22+
return raw.toSentenceCase() == raw;
23+
case Case.snake:
24+
return raw.toSnakeCase() == raw;
25+
case Case.capital:
26+
return raw.toCapitalCase() == raw;
27+
}
2928
}
29+
return false;
3030
}
3131

3232
bool ensureFullStop(String raw, String target) {
@@ -38,9 +38,6 @@ bool ensureLeadingBlank(String raw) {
3838
}
3939

4040
bool ensureMaxLength(dynamic raw, num maxLength) {
41-
if (raw == null) {
42-
return true;
43-
}
4441
if (raw is String) {
4542
return raw.length <= maxLength;
4643
}
@@ -51,13 +48,12 @@ bool ensureMaxLength(dynamic raw, num maxLength) {
5148
}
5249

5350
bool ensureMaxLineLength(String raw, num maxLineLength) {
54-
return raw.split('\n').every((line) => ensureMaxLength(line, maxLineLength));
51+
return raw
52+
.split(RegExp(r'(?:\r?\n)'))
53+
.every((line) => ensureMaxLength(line, maxLineLength));
5554
}
5655

5756
bool ensureMinLength(dynamic raw, num minLength) {
58-
if (raw == null) {
59-
return false;
60-
}
6157
if (raw is String) {
6258
return raw.length >= minLength;
6359
}
@@ -68,9 +64,6 @@ bool ensureMinLength(dynamic raw, num minLength) {
6864
}
6965

7066
bool ensureEmpty(dynamic raw) {
71-
if (raw == null) {
72-
return true;
73-
}
7467
if (raw is String) {
7568
return raw.isEmpty;
7669
}
@@ -81,9 +74,6 @@ bool ensureEmpty(dynamic raw) {
8174
}
8275

8376
bool ensureEnum(dynamic raw, Iterable enums) {
84-
if (raw == null) {
85-
return true;
86-
}
8777
if (raw is String) {
8878
return raw.isEmpty || enums.contains(raw);
8979
}

lib/src/rules.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ Map<String, RuleFunction> get supportedRules => {
3939
'references-empty': emptyRule(CommitComponent.references),
4040
};
4141

42-
/// Build full stop rule for commit component.
42+
/// Build full-stop rule for commit component.
4343
RuleFunction fullStopRule(CommitComponent component) {
4444
return (Commit commit, Rule config) {
4545
if (config is! ValueRule) {
4646
throw Exception('$config is not ValueRuleConfig<String>');
4747
}
4848
final raw = commit.componentRaw(component);
49-
final result = raw != null && ensureFullStop(raw, config.value);
49+
final result = raw == null || ensureFullStop(raw, config.value);
5050
final negated = config.condition == RuleCondition.never;
5151
return RuleOutcome(
5252
valid: negated ? !result : result,
@@ -59,11 +59,11 @@ RuleFunction fullStopRule(CommitComponent component) {
5959
};
6060
}
6161

62-
/// Build leanding blank rule for commit component.
62+
/// Build leanding-blank rule for commit component.
6363
RuleFunction leadingBlankRule(CommitComponent component) {
6464
return (Commit commit, Rule config) {
6565
final raw = commit.componentRaw(component);
66-
final result = raw != null && ensureLeadingBlank(raw);
66+
final result = raw == null || ensureLeadingBlank(raw);
6767
final negated = config.condition == RuleCondition.never;
6868
return RuleOutcome(
6969
valid: negated ? !result : result,
@@ -76,11 +76,11 @@ RuleFunction leadingBlankRule(CommitComponent component) {
7676
};
7777
}
7878

79-
/// Build leanding blank rule for commit component.
79+
/// Build empty rule for commit component.
8080
RuleFunction emptyRule(CommitComponent component) {
8181
return (Commit commit, Rule config) {
8282
final raw = commit.componentRaw(component);
83-
final result = ensureEmpty(raw);
83+
final result = raw == null || ensureEmpty(raw);
8484
final negated = config.condition == RuleCondition.never;
8585
return RuleOutcome(
8686
valid: negated ? !result : result,
@@ -97,7 +97,7 @@ RuleFunction caseRule(CommitComponent component) {
9797
throw Exception('$config is not CaseRuleConfig');
9898
}
9999
final raw = commit.componentRaw(component);
100-
final result = raw != null && ensureCase(raw, config.type);
100+
final result = raw == null || ensureCase(raw, config.type);
101101
final negated = config.condition == RuleCondition.never;
102102
return RuleOutcome(
103103
valid: negated ? !result : result,
@@ -110,14 +110,14 @@ RuleFunction caseRule(CommitComponent component) {
110110
};
111111
}
112112

113-
/// Build max length rule for commit component.
113+
/// Build max-length rule for commit component.
114114
RuleFunction maxLengthRule(CommitComponent component) {
115115
return (Commit commit, Rule config) {
116116
if (config is! LengthRule) {
117117
throw Exception('$config is not LengthRuleConfig');
118118
}
119119
final raw = commit.componentRaw(component);
120-
final result = raw != null && ensureMaxLength(raw, config.length);
120+
final result = raw == null || ensureMaxLength(raw, config.length);
121121
final negated = config.condition == RuleCondition.never;
122122
return RuleOutcome(
123123
valid: negated ? !result : result,
@@ -130,14 +130,14 @@ RuleFunction maxLengthRule(CommitComponent component) {
130130
};
131131
}
132132

133-
/// Build max line length rule for commit component.
133+
/// Build max-line-length rule for commit component.
134134
RuleFunction maxLineLengthRule(CommitComponent component) {
135135
return (Commit commit, Rule config) {
136136
if (config is! LengthRule) {
137137
throw Exception('$config is not LengthRuleConfig');
138138
}
139139
final raw = commit.componentRaw(component);
140-
final result = raw != null && ensureMaxLineLength(raw, config.length);
140+
final result = raw == null || ensureMaxLineLength(raw, config.length);
141141
final negated = config.condition == RuleCondition.never;
142142
return RuleOutcome(
143143
valid: negated ? !result : result,
@@ -150,14 +150,14 @@ RuleFunction maxLineLengthRule(CommitComponent component) {
150150
};
151151
}
152152

153-
/// Build min length rule for commit component.
153+
/// Build min-length rule for commit component.
154154
RuleFunction minLengthRule(CommitComponent component) {
155155
return (Commit commit, Rule config) {
156156
if (config is! LengthRule) {
157157
throw Exception('$config is not LengthRuleConfig');
158158
}
159159
final raw = commit.componentRaw(component);
160-
final result = raw != null && ensureMinLength(raw, config.length);
160+
final result = raw == null || ensureMinLength(raw, config.length);
161161
final negated = config.condition == RuleCondition.never;
162162
return RuleOutcome(
163163
valid: negated ? !result : result,
@@ -170,13 +170,14 @@ RuleFunction minLengthRule(CommitComponent component) {
170170
};
171171
}
172172

173+
/// Build enum rule for commit component.
173174
RuleFunction enumRule(CommitComponent component) {
174175
return (Commit commit, Rule config) {
175176
if (config is! EnumRule) {
176177
throw Exception('$config is not EnumRuleConfig');
177178
}
178179
final raw = commit.componentRaw(component);
179-
final result = ensureEnum(raw, config.allowed);
180+
final result = raw == null || ensureEnum(raw, config.allowed);
180181
final negated = config.condition == RuleCondition.never;
181182
return RuleOutcome(
182183
valid: negated ? !result : result,

0 commit comments

Comments
 (0)