Skip to content

Commit

Permalink
Fix allowed names for Unit-returning functions (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
dellisd authored Jun 25, 2023
1 parent b38c981 commit 99492b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ constructor(
val functionName = function.name?.takeUnless(String::isEmpty) ?: return
val firstLetter = functionName.first()

// If it's allowed, we don't report it
val isAllowed = allowedNames.value.any { it.toRegex().matches(functionName) }
if (isAllowed) return

if (function.returnsValue) {
// If it returns value, the composable should start with a lowercase letter
if (firstLetter.isUpperCase()) {
// If it's allowed, we don't report it
val isAllowed = allowedNames.value.any { it.toRegex().matches(functionName) }
if (isAllowed) return
context.report(
ISSUE_LOWERCASE,
function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ class ComposableFunctionNamingDetectorTest : BaseSlackLintTest() {
lint().files(kotlin(code)).allowCompilationErrors().run().expectClean()
}

@Test
fun `passes when a composable that returns nothing or Unit is lowercase but allowed`() {
@Language("kotlin")
val code =
"""
@Composable
fun myPresenter() { }
@Composable
fun myPresenter(): Unit { }
"""
.trimIndent()
lint().files(kotlin(code)).allowCompilationErrors().run().expectClean()
}

@Test
fun `passes when a composable doesn't have a body block, is a property or a lambda`() {
@Language("kotlin")
Expand Down

0 comments on commit 99492b6

Please sign in to comment.