Skip to content

Commit 083f115

Browse files
fix(cli): description empty parse panic (#299)
* fix(cli): description empty parse panic Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> * fix(cli): scope empty parse error Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> * chore(cli): error handle format field getter Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --------- Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
1 parent 1b29def commit 083f115

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

src/git.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ pub fn parse_commit_message(
119119
/// does not have any rules for it.
120120
/// See: https://commitlint.js.org/#/reference-rules
121121
pub fn parse_subject(subject: &str) -> (Option<String>, Option<String>, Option<String>) {
122-
let re =
123-
regex::Regex::new(r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(!)?\:\s(?P<description>.+)$")
124-
.unwrap();
122+
let re = regex::Regex::new(
123+
r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(?:!)?\:\s?(?P<description>.*)$",
124+
)
125+
.unwrap();
125126
if let Some(captures) = re.captures(subject) {
126127
let r#type = captures.name("type").map(|m| m.as_str().to_string());
127128
let scope = captures.name("scope").map(|m| m.as_str().to_string());
@@ -232,7 +233,6 @@ Name: Keke";
232233
)
233234
);
234235
}
235-
236236

237237
#[test]
238238
fn test_parse_subject_with_emphasized_type_without_scope() {
@@ -246,11 +246,39 @@ Name: Keke";
246246
)
247247
);
248248
}
249+
250+
#[test]
251+
fn test_parse_subject_with_empty_description() {
252+
let input = "feat(cli): ";
253+
assert_eq!(
254+
parse_subject(input),
255+
(
256+
Some("feat".to_string()),
257+
Some("cli".to_string()),
258+
Some("".to_string())
259+
)
260+
);
261+
}
262+
263+
#[test]
264+
fn test_parse_subject_with_empty_scope() {
265+
let input = "feat: add dummy commit";
266+
assert_eq!(
267+
parse_subject(input),
268+
(
269+
Some("feat".to_string()),
270+
None,
271+
Some("add dummy commit".to_string())
272+
)
273+
);
274+
}
275+
249276
#[test]
250277
fn test_parse_subject_without_message() {
251278
let input = "";
252279
assert_eq!(parse_subject(input), (None, None, Some("".to_string())));
253280
}
281+
254282
#[test]
255283
fn test_parse_subject_with_error_message() {
256284
let input = "test";

src/rule/description_format.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ impl Rule for DescriptionFormat {
4040
}
4141
};
4242

43-
if !regex.is_match(&message.description.as_ref().unwrap()) {
44-
return Some(Violation {
45-
level: self.level.unwrap_or(Self::LEVEL),
46-
message: self.message(message),
47-
});
43+
match &message.description {
44+
None => {
45+
return Some(Violation {
46+
level: self.level.unwrap_or(Self::LEVEL),
47+
message: "found no description".to_string(),
48+
});
49+
}
50+
Some(description) => {
51+
if !regex.is_match(description) {
52+
return Some(Violation {
53+
level: self.level.unwrap_or(Self::LEVEL),
54+
message: self.message(message),
55+
});
56+
}
57+
}
4858
}
4959
}
5060

src/rule/scope_format.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ impl Rule for ScopeFormat {
4040
}
4141
};
4242

43-
if !regex.is_match(&message.scope.as_ref().unwrap()) {
44-
return Some(Violation {
45-
level: self.level.unwrap_or(Self::LEVEL),
46-
message: self.message(message),
47-
});
43+
match &message.scope {
44+
None => {
45+
return Some(Violation {
46+
level: self.level.unwrap_or(Self::LEVEL),
47+
message: "found no description".to_string(),
48+
});
49+
}
50+
Some(description) => {
51+
if !regex.is_match(description) {
52+
return Some(Violation {
53+
level: self.level.unwrap_or(Self::LEVEL),
54+
message: self.message(message),
55+
});
56+
}
57+
}
4858
}
4959
}
5060

src/rule/type_format.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ impl Rule for TypeFormat {
4040
}
4141
};
4242

43-
if !regex.is_match(&message.r#type.as_ref().unwrap()) {
44-
return Some(Violation {
45-
level: self.level.unwrap_or(Self::LEVEL),
46-
message: self.message(message),
47-
});
43+
match &message.r#type {
44+
None => {
45+
return Some(Violation {
46+
level: self.level.unwrap_or(Self::LEVEL),
47+
message: "found no type".to_string(),
48+
});
49+
}
50+
Some(description) => {
51+
if !regex.is_match(description) {
52+
return Some(Violation {
53+
level: self.level.unwrap_or(Self::LEVEL),
54+
message: self.message(message),
55+
});
56+
}
57+
}
4858
}
4959
}
5060

0 commit comments

Comments
 (0)