From 42c11bb1bc421d94df044e815c92a2b890d92c72 Mon Sep 17 00:00:00 2001 From: Sha Sha Chu Date: Mon, 28 Jun 2021 11:06:41 -0700 Subject: [PATCH] Simplify implementation of enum case rule (#1172) * Simplify implementation of enum case rule - Use the same regex as IntelliJ - Added tests for failures - Removed formatting code/test * fix build --- .../experimental/EnumEntryNameCaseRule.kt | 45 ++++-------------- .../experimental/EnumEntryNameCaseRuleTest.kt | 46 +++++++++---------- 2 files changed, 30 insertions(+), 61 deletions(-) diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRule.kt index 945f9d829b..505fef3240 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRule.kt @@ -5,10 +5,11 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement import org.jetbrains.kotlin.psi.KtEnumEntry -class EnumEntryNameCaseRule : Rule("enum-entry-name-case") { +public class EnumEntryNameCaseRule : Rule("enum-entry-name-case") { - companion object { + internal companion object { const val ERROR_MESSAGE = "Enum entry name should be uppercase underscore-separated names like \"ENUM_ENTRY\" or upper camel-case like \"EnumEntry\"" + val regex = Regex("[A-Z]([A-Za-z\\d]*|[A-Z_\\d]*)") } override fun visit( @@ -22,40 +23,12 @@ class EnumEntryNameCaseRule : Rule("enum-entry-name-case") { val enumEntry = node.psi as? KtEnumEntry ?: return val name = enumEntry.name ?: return - if (name.containsLowerCase()) { - // In case of lower camel case like "enumName", or all lower case like "enumname" - if (!name.startsWithUpperCase()) { - emit( - node.startOffset, - ERROR_MESSAGE, - false - ) - - if (autoCorrect) correct(enumEntry, name) - } - - // In case of lower case with underscore like "enum_name" - else if (name.contains("_") && name.containsLowerCase()) { - emit( - node.startOffset, - ERROR_MESSAGE, - false - ) - - if (autoCorrect) correct(enumEntry, name) - } + if (!name.matches(regex)) { + emit( + node.startOffset, + ERROR_MESSAGE, + false + ) } } - - private fun correct(enumEntry: KtEnumEntry, originalName: String) { - enumEntry.setName(originalName.toUpperCase()) - } - - private fun String.startsWithUpperCase(): Boolean { - return this.isNotEmpty() && this[0].isUpperCase() - } - - private fun String.containsLowerCase(): Boolean { - return this.any { it.isLowerCase() } - } } diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRuleTest.kt index 7b6d1e2394..d991413a7e 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/EnumEntryNameCaseRuleTest.kt @@ -4,7 +4,6 @@ import com.pinterest.ktlint.core.LintError import com.pinterest.ktlint.test.format import com.pinterest.ktlint.test.lint import org.assertj.core.api.Assertions.assertThat -import org.junit.Ignore import org.junit.Test class EnumEntryNameCaseRuleTest { @@ -16,8 +15,8 @@ class EnumEntryNameCaseRuleTest { enum class FirstEnum { ENUM_ENTRY } - enum class SecondEnum { - EnumEntry + enum class NetworkInfo { + WiFi, Mobile } """.trimIndent() @@ -26,33 +25,30 @@ class EnumEntryNameCaseRuleTest { } @Test - @Ignore("https://github.com/pinterest/ktlint/pull/638#issuecomment-558119749") - fun testFormat() { - val unformatted = - """ - enum class FirstEnum { - enumEntry - } - enum class SecondEnum { - enum_entry - } - """.trimIndent() + fun `invalid formats should trigger lint errors`() { val formatted = """ enum class FirstEnum { - ENUMENTRY - } - enum class SecondEnum { - ENUM_ENTRY + helloWorld, + ALMOST_xVALID } """.trimIndent() - - assertThat(EnumEntryNameCaseRule().lint(unformatted)).isEqualTo( - listOf( - LintError(2, 5, "enum-entry-name-case", EnumEntryNameCaseRule.ERROR_MESSAGE), - LintError(5, 5, "enum-entry-name-case", EnumEntryNameCaseRule.ERROR_MESSAGE) + assertThat(EnumEntryNameCaseRule().lint(formatted)) + .isEqualTo( + listOf( + LintError( + 2, + 5, + "enum-entry-name-case", + EnumEntryNameCaseRule.ERROR_MESSAGE + ), + LintError( + 3, + 5, + "enum-entry-name-case", + EnumEntryNameCaseRule.ERROR_MESSAGE + ), + ) ) - ) - assertThat(EnumEntryNameCaseRule().format(unformatted)).isEqualTo(formatted) } }