Skip to content

Commit

Permalink
fix: rule should pass if commit component raw is null (fix #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyiso committed Oct 5, 2023
1 parent 99c88a7 commit c07687e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 47 deletions.
56 changes: 23 additions & 33 deletions lib/src/ensure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ bool ensureCase(dynamic raw, Case target) {
if (raw is Iterable) {
return raw.isEmpty || raw.every((element) => ensureCase(element, target));
}
if (raw is! String) {
return false;
}
switch (target) {
case Case.lower:
return raw.toLowerCase() == raw;
case Case.upper:
return raw.toUpperCase() == raw;
case Case.camel:
return raw.toCamelCase() == raw;
case Case.kebab:
return raw.toKebabCase() == raw;
case Case.pascal:
return raw.toPascalCase() == raw;
case Case.sentence:
return raw.toSentenceCase() == raw;
case Case.snake:
return raw.toSnakeCase() == raw;
case Case.capital:
return raw.toCapitalCase() == raw;
if (raw is String) {
switch (target) {
case Case.lower:
return raw.toLowerCase() == raw;
case Case.upper:
return raw.toUpperCase() == raw;
case Case.camel:
return raw.toCamelCase() == raw;
case Case.kebab:
return raw.toKebabCase() == raw;
case Case.pascal:
return raw.toPascalCase() == raw;
case Case.sentence:
return raw.toSentenceCase() == raw;
case Case.snake:
return raw.toSnakeCase() == raw;
case Case.capital:
return raw.toCapitalCase() == raw;
}
}
return false;
}

bool ensureFullStop(String raw, String target) {
Expand All @@ -38,9 +38,6 @@ bool ensureLeadingBlank(String raw) {
}

bool ensureMaxLength(dynamic raw, num maxLength) {
if (raw == null) {
return true;
}
if (raw is String) {
return raw.length <= maxLength;
}
Expand All @@ -51,13 +48,12 @@ bool ensureMaxLength(dynamic raw, num maxLength) {
}

bool ensureMaxLineLength(String raw, num maxLineLength) {
return raw.split('\n').every((line) => ensureMaxLength(line, maxLineLength));
return raw
.split(RegExp(r'(?:\r?\n)'))
.every((line) => ensureMaxLength(line, maxLineLength));
}

bool ensureMinLength(dynamic raw, num minLength) {
if (raw == null) {
return false;
}
if (raw is String) {
return raw.length >= minLength;
}
Expand All @@ -68,9 +64,6 @@ bool ensureMinLength(dynamic raw, num minLength) {
}

bool ensureEmpty(dynamic raw) {
if (raw == null) {
return true;
}
if (raw is String) {
return raw.isEmpty;
}
Expand All @@ -81,9 +74,6 @@ bool ensureEmpty(dynamic raw) {
}

bool ensureEnum(dynamic raw, Iterable enums) {
if (raw == null) {
return true;
}
if (raw is String) {
return raw.isEmpty || enums.contains(raw);
}
Expand Down
29 changes: 15 additions & 14 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ Map<String, RuleFunction> get supportedRules => {
'references-empty': emptyRule(CommitComponent.references),
};

/// Build full stop rule for commit component.
/// Build full-stop rule for commit component.
RuleFunction fullStopRule(CommitComponent component) {
return (Commit commit, Rule config) {
if (config is! ValueRule) {
throw Exception('$config is not ValueRuleConfig<String>');
}
final raw = commit.componentRaw(component);
final result = raw != null && ensureFullStop(raw, config.value);
final result = raw == null || ensureFullStop(raw, config.value);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -59,11 +59,11 @@ RuleFunction fullStopRule(CommitComponent component) {
};
}

/// Build leanding blank rule for commit component.
/// Build leanding-blank rule for commit component.
RuleFunction leadingBlankRule(CommitComponent component) {
return (Commit commit, Rule config) {
final raw = commit.componentRaw(component);
final result = raw != null && ensureLeadingBlank(raw);
final result = raw == null || ensureLeadingBlank(raw);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -76,11 +76,11 @@ RuleFunction leadingBlankRule(CommitComponent component) {
};
}

/// Build leanding blank rule for commit component.
/// Build empty rule for commit component.
RuleFunction emptyRule(CommitComponent component) {
return (Commit commit, Rule config) {
final raw = commit.componentRaw(component);
final result = ensureEmpty(raw);
final result = raw == null || ensureEmpty(raw);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -97,7 +97,7 @@ RuleFunction caseRule(CommitComponent component) {
throw Exception('$config is not CaseRuleConfig');
}
final raw = commit.componentRaw(component);
final result = raw != null && ensureCase(raw, config.type);
final result = raw == null || ensureCase(raw, config.type);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -110,14 +110,14 @@ RuleFunction caseRule(CommitComponent component) {
};
}

/// Build max length rule for commit component.
/// Build max-length rule for commit component.
RuleFunction maxLengthRule(CommitComponent component) {
return (Commit commit, Rule config) {
if (config is! LengthRule) {
throw Exception('$config is not LengthRuleConfig');
}
final raw = commit.componentRaw(component);
final result = raw != null && ensureMaxLength(raw, config.length);
final result = raw == null || ensureMaxLength(raw, config.length);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -130,14 +130,14 @@ RuleFunction maxLengthRule(CommitComponent component) {
};
}

/// Build max line length rule for commit component.
/// Build max-line-length rule for commit component.
RuleFunction maxLineLengthRule(CommitComponent component) {
return (Commit commit, Rule config) {
if (config is! LengthRule) {
throw Exception('$config is not LengthRuleConfig');
}
final raw = commit.componentRaw(component);
final result = raw != null && ensureMaxLineLength(raw, config.length);
final result = raw == null || ensureMaxLineLength(raw, config.length);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -150,14 +150,14 @@ RuleFunction maxLineLengthRule(CommitComponent component) {
};
}

/// Build min length rule for commit component.
/// Build min-length rule for commit component.
RuleFunction minLengthRule(CommitComponent component) {
return (Commit commit, Rule config) {
if (config is! LengthRule) {
throw Exception('$config is not LengthRuleConfig');
}
final raw = commit.componentRaw(component);
final result = raw != null && ensureMinLength(raw, config.length);
final result = raw == null || ensureMinLength(raw, config.length);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand All @@ -170,13 +170,14 @@ RuleFunction minLengthRule(CommitComponent component) {
};
}

/// Build enum rule for commit component.
RuleFunction enumRule(CommitComponent component) {
return (Commit commit, Rule config) {
if (config is! EnumRule) {
throw Exception('$config is not EnumRuleConfig');
}
final raw = commit.componentRaw(component);
final result = ensureEnum(raw, config.allowed);
final result = raw == null || ensureEnum(raw, config.allowed);
final negated = config.condition == RuleCondition.never;
return RuleOutcome(
valid: negated ? !result : result,
Expand Down

0 comments on commit c07687e

Please sign in to comment.