Skip to content

Commit

Permalink
Fix TopLevelPropertyNaming
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Sep 10, 2022
1 parent 50da109 commit 28eddd7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Expand Up @@ -38,6 +38,8 @@ class TopLevelPropertyNaming(config: Config = Config.empty) : Rule(config) {
private val privatePropertyPattern: Regex by config("_?[A-Za-z][_A-Za-z0-9]*") { it.toRegex() }

override fun visitProperty(property: KtProperty) {
super.visitProperty(property)
if (!property.isTopLevel) return
if (property.isConstant()) {
handleConstant(property)
} else {
Expand Down
Expand Up @@ -12,18 +12,12 @@ class TopLevelPropertyNamingSpec {
val subject = TopLevelPropertyNaming()

@Test
fun `should use custom name top level propeties`() {
assertThat(
TopLevelPropertyNaming(TestConfig(mapOf(TopLevelPropertyNaming.CONSTANT_PATTERN to "^lowerCaseConst$"))).compileAndLint(
"""
class Foo{
companion object {
const val lowerCaseConst = ""
}
}
""".trimIndent()
)
).isEmpty()
fun `should use custom name top level properties`() {
val code = """
const val lowerCaseConst = ""
""".trimIndent()
val subject = TopLevelPropertyNaming(TestConfig(TopLevelPropertyNaming.CONSTANT_PATTERN to "^lowerCaseConst$"))
assertThat(subject.compileAndLint(code)).isEmpty()
}

@Nested
Expand Down Expand Up @@ -87,4 +81,19 @@ class TopLevelPropertyNamingSpec {
io.gitlab.arturbosch.detekt.test.assertThat(subject.lint(code)).hasSize(1)
}
}

@Test
fun `should not care about no top level properties`() {
val code = """
class Foo{
val __baz = ""
companion object {
const val __bar = ""
val __foo = ""
}
}
""".trimIndent()

assertThat(subject.compileAndLint(code)).isEmpty()
}
}

0 comments on commit 28eddd7

Please sign in to comment.