Skip to content

Commit

Permalink
Simplify implementation of enum case rule (#1172)
Browse files Browse the repository at this point in the history
* Simplify implementation of enum case rule

- Use the same regex as IntelliJ
- Added tests for failures
- Removed formatting code/test

* fix build
  • Loading branch information
shashachu authored Jun 28, 2021
1 parent 89db7a7 commit 42c11bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,8 +15,8 @@ class EnumEntryNameCaseRuleTest {
enum class FirstEnum {
ENUM_ENTRY
}
enum class SecondEnum {
EnumEntry
enum class NetworkInfo {
WiFi, Mobile
}
""".trimIndent()

Expand All @@ -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)
}
}

0 comments on commit 42c11bb

Please sign in to comment.