Skip to content

Commit

Permalink
Merge branch 'feature/case-enum' into 'master'
Browse files Browse the repository at this point in the history
Changes to format fixing. Added aggressive autofix for Enum values format

See merge request codestyle/huawei-ktlint!31
  • Loading branch information
petertrr committed Jun 15, 2020
2 parents 948c349 + 9d597cd commit 4bf6535
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ fun String.toUpperSnakeCase(): String {
// lower_snake -> LOWER_SNAKE
if (this.isLowerSnakeCase()) return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, this)

val idx = getFirstLetterOrDigit()
if (idx != -1) {
// any other format -> UPPER_SNAKE_CASE
// [p]a[SC]a[_]l -> [P]A_[SC]_A_[L]
return this[idx].toUpperCase().toString() + convertUnknownCaseToUpperSnake(this.substring(idx + 1), true)
}

log.error("Not able to fix case format for: $this")
return this
}

Expand All @@ -51,11 +59,17 @@ fun String.toLowerCamelCase(): String {
// lower_snake -> LOWER_SNAKE
if (this.isLowerSnakeCase()) return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, this)

// any other format -> camelCase
// changing first letter to uppercase and replacing several uppercase letters in raw to lowercase:
// example of change: [P]a[SC]a[_]l -> [p]a[Sc]a[L]
// FixMe: there is some discussion on how lowerN_Case should be resolved: to lowerNcase or to lowernCase or lowerNCase (current version)
return this[0].toLowerCase().toString() + convertUnknownCaseToCamel(this.substring(1), false)
val idx = getFirstLetterOrDigit()
if (idx != -1) {
// any other format -> camelCase
// changing first letter to uppercase and replacing several uppercase letters in raw to lowercase:
// example of change: [P]a[SC]a[_]l -> [p]a[Sc]a[L]
// FixMe: there is some discussion on how lowerN_Case should be resolved: to lowerNcase or to lowernCase or lowerNCase (current version)
return this[idx].toLowerCase().toString() + convertUnknownCaseToCamel(this.substring(idx + 1), false)
}

log.error("Not able to fix case format for: $this")
return this
}

/**
Expand All @@ -71,18 +85,25 @@ fun String.toPascalCase(): String {
// lower_snake -> LowerSnake
if (this.isLowerSnakeCase()) return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, this)

// any other format -> PascalCase
// changing first letter to uppercase and replacing several uppercase letters in raw to lowercase:
// example of change: [p]a[SC]a[_]l -> [P]a[Sc]a[L]
// FixMe: there is some discussion on how PascalN_Case should be resolved: to PascalNcase or to PascalnCase or PascalNCase (current version)
return this[0].toUpperCase().toString() + convertUnknownCaseToCamel(this.substring(1), true)
val idx = getFirstLetterOrDigit()
if (idx != -1) {
// any other format -> PascalCase
// changing first letter to uppercase and replacing several uppercase letters in raw to lowercase:
// example of change: [p]a[SC]a[_]l -> [P]a[Sc]a[L]
// FixMe: there is some discussion on how PascalN_Case should be resolved: to PascalNcase or to PascalnCase or PascalNCase (current version)
return this[idx].toUpperCase().toString() + convertUnknownCaseToCamel(this.substring(idx + 1), true)
}

log.error("Not able to fix case format for: $this")
return this
}

private fun convertUnknownCaseToCamel(str: String, isFirstLetterCapital: Boolean): String {
// [p]a[SC]a[_]l -> [P]a[Sc]a[L]
var isPreviousLetterCapital = isFirstLetterCapital
var isPreviousLetterUnderscore = false
return str.map {
return@map if (it.isUpperCase()) {
if (it.isUpperCase()) {
val result = if (isPreviousLetterCapital && !isPreviousLetterUnderscore) it.toLowerCase() else it
isPreviousLetterCapital = true
isPreviousLetterUnderscore = false
Expand All @@ -104,3 +125,25 @@ private fun convertUnknownCaseToCamel(str: String, isFirstLetterCapital: Boolean
}
}.joinToString("")
}

private fun convertUnknownCaseToUpperSnake(str: String, isFirstLetterCapital: Boolean): String {
// [p]a[SC]a[_]l -> [P]A_[SC]_A_[L]
var alreadyInsertedUnderscore = isFirstLetterCapital
return str.map {
if (it.isUpperCase()) {
if (!alreadyInsertedUnderscore) {
alreadyInsertedUnderscore = true
"_$it"
} else {
it
}
} else {
alreadyInsertedUnderscore = (it == '_')
it.toUpperCase()
}

}.joinToString("")
}

private fun String.getFirstLetterOrDigit() =
indexOfFirst { it.isLetterOrDigit() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.huawei.rri.fixbot.ruleset.huawei.chapter1

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class IdentifierNamingFixTest {
).isEqualTo(true)
}

@Test
fun `incorrect enum values case (fix)`() {
assertThat(
TestComparatorUnit("test/paragraph1/naming/enum_", ::format)
.compareFilesFromResources("EnumValueCaseExpected.kt", "EnumValueCaseTest.kt")
).isEqualTo(true)

}

private fun format(text: String, fileName: String): String = IdentifierNaming().format(text, fileName)

private fun Rule.format(text: String, fileName: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ class Pascalcase6 {}
class PascAlCase7 {}
class PascaLCase8 {}
class PascAlCase9 {}
class PascAlCase10 {}
class PascAlCase11 {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ class Pascalcase6 {}
class PascAl_Case7 {}
class PascaL_Case8 {}
class PascAL_Case9 {}
class _PascAL_Case10 {}
class PascAL_Case11_ {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.huawei.test.resources.test.paragraph1.naming.enum_

enum class EnumValueCaseTest {
PA_SC_SAL_L,
PASC_ASL_F,
START_PSAAA_DFE,
NAME_MYA_SAY_R,
NAME_MYA_SAY_R_
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.huawei.test.resources.test.paragraph1.naming.enum_

enum class EnumValueCaseTest {
paSC_SAl_l,
PascAsl_f,
START_PSaaa_DFE,
_NAme_MYa_sayR,
NAme_MYa_sayR_
}

5 changes: 5 additions & 0 deletions rules-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
"enabled": true,
"configuration": ""
},
{
"name": "KDOC_WITHOUT_THROWS_TAG",
"enabled": true,
"configuration": ""
},
{
"name": "KDOC_WITHOUT_THROWS_TAG",
"enabled": true,
Expand Down

0 comments on commit 4bf6535

Please sign in to comment.