Skip to content

Commit cd7c7c4

Browse files
committed
refactor: only allow footer types with whitespace in lenient mode
1 parent c809a49 commit cd7c7c4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/main/kotlin/com/github/lppedd/cc/parser/ConventionalCommitParser.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,21 @@ internal fun parseConventionalCommit(message: String, lenient: Boolean = false):
112112
// Consume/skip the separator (':' or ' '), if present
113113
reader.consume(CCToken.Type.SEPARATOR)
114114

115-
// Allow a missing footer value, even in non-lenient mode
116-
val footerValue = reader.consume(CCToken.Type.FOOTER_VALUE) ?: ""
117-
footers += CommitFooter(footerType, footerValue)
115+
val footerValue = reader.consume(CCToken.Type.FOOTER_VALUE)
116+
117+
if (!lenient) {
118+
// If the footer type does not have an associated value,
119+
// we can disregard trailing whitespace when validating it
120+
val fv = if (footerValue.isNullOrBlank()) footerType.trimEnd() else footerType
121+
122+
// The BREAKING CHANGE footer type is a special case
123+
if (!fv.equals("BREAKING CHANGE", ignoreCase = true) && fv.any(Char::isWhitespace)) {
124+
return ParseResult.Error("The commit footer type '$footerType' is invalid")
125+
}
126+
}
127+
128+
// Allow a missing footer value even in non-lenient mode
129+
footers += CommitFooter(footerType, footerValue ?: "")
118130
}
119131

120132
return ParseResult.Success(

src/test/kotlin/com/github/lppedd/cc/parser/ConventionalCommitParserTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class ConventionalCommitParserTest {
253253
"""
254254
|build(npm): switch to yarn
255255
|
256-
|Co-authored by: this may be a long
256+
|Co-authored-by: this may be a long
257257
| multiline footer value that spans
258258
| multiple lines.
259259
""".trimMargin()
@@ -268,7 +268,7 @@ class ConventionalCommitParserTest {
268268
assertEquals(1, message.footers.size)
269269

270270
val (type, value) = message.footers.first()
271-
assertEquals("Co-authored by", type)
271+
assertEquals("Co-authored-by", type)
272272
assertEquals(
273273
"""
274274
| this may be a long

0 commit comments

Comments
 (0)