Skip to content

Commit

Permalink
Handle single top level function declaration for which the type recei…
Browse files Browse the repository at this point in the history
…ver contains a nullable type and or a generic type. The implementation is a bit crude by just removing "?", "<" and ">"" from the TYPE_REFERENCE.
  • Loading branch information
paul-dingemans committed Jun 30, 2022
1 parent c8e39fb commit 9de014c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public class FilenameRule : Rule(
"File '$this.kt' contains a single top level declaration and should be named '$filename.kt'",
false
)
qualifier != null && this != "$qualifier$filename" ->
qualifier != null && this != filename && this != "$qualifier$filename" ->
emit(
0,
"File '$this.kt' contains a single top level declaration and should be named '$filename.kt' or '$qualifier$filename.kt'",
Expand Down Expand Up @@ -188,6 +188,9 @@ public class FilenameRule : Rule(
.findChildByType(TYPE_REFERENCE)
?.text
?.replace(".", "")
?.replace("?", "")
?.replace("<", "")
?.replace(">", "")
TopLevelDeclaration(elementType, it, typeReference)
} else {
TopLevelDeclaration(elementType, it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@ class FilenameRuleTest {
.hasLintViolationWithoutAutoCorrect(1, 1, "File '$UNEXPECTED_FILE_NAME' contains a single top level declaration and should be named 'Image.kt' or 'FooStatusImage.kt'")
}

@Test
fun `Issue 1521 - Given a file containing a single toplevel extension function on a custom receiver class defined in another file and the filename equals the qualifier followed by function name then do not report a lint violation`() {
val code =
"""
fun Foo.Status.image() {}
""".trimIndent()
fileNameRuleAssertThat(code)
.asFileWithPath("FooStatusImage.kt")
.hasNoLintViolations()
}

@Test
fun `Issue 1521 - Given a file containing a single toplevel extension function on a nullable custom receiver class defined in another file and the filename equals the qualifier followed by function name then do not report a lint violation`() {
val code =
"""
fun Foo.Status?.image() {}
""".trimIndent()
fileNameRuleAssertThat(code)
.asFileWithPath("FooStatusImage.kt")
.hasNoLintViolations()
}

@Test
fun `Issue 1521 - Given a file containing a single toplevel extension function on a generic custom receiver class defined in another file and the filename equals the qualifier followed by function name then do not report a lint violation`() {
val code =
"""
fun List<Foo.Status?>.image() {}
""".trimIndent()
fileNameRuleAssertThat(code)
.asFileWithPath("ListFooStatusImage.kt")
.hasNoLintViolations()
}

@Test
fun `Issue 1521 - Given a file containing a single toplevel extension function on a custom receiver class defined in another file and the filename equals the function name then do not report a lint violation`() {
val code =
"""
fun Foo.Status.image() {}
""".trimIndent()
fileNameRuleAssertThat(code)
.asFileWithPath("Image.kt")
.hasNoLintViolations()
}

@ParameterizedTest(name = "Top level declaration: {0}")
@ValueSource(
Expand Down

0 comments on commit 9de014c

Please sign in to comment.