Skip to content

Commit

Permalink
Merge branch 'feature/underscores' into 'master'
Browse files Browse the repository at this point in the history
Added aggressive autofix for package name separators like underscore

See merge request codestyle/huawei-ktlint!32
  • Loading branch information
petertrr committed Jun 15, 2020
2 parents 4bf6535 + 185a253 commit 67fe432
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ enum class Warnings(private val id: Int, private val canBeAutoCorrected: Boolean
KDOC_WITHOUT_PARAM_TAG(22, true, "all methods which take arguments should have @param tags in KDoc, the following parameters are missing"),
KDOC_WITHOUT_RETURN_TAG(23, true, "all methods which return values should have @return tag in KDoc"),
KDOC_WITHOUT_THROWS_TAG(24, true, "all methods which throw exceptions should have @throws tag in KDoc"),
BLANK_LINE_AFTER_KDOC(25, true, "there should be no empty line between Kdoc and code it is describing")
BLANK_LINE_AFTER_KDOC(25, true, "there should be no empty line between Kdoc and code it is describing"),

// ====== incorrect place and warn number ====
INCORRECT_PACKAGE_SEPARATOR(26, true, "package name parts should be separated only by dots - there should be no other symbols like underscores (_)")
;

override fun ruleName(): String = this.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,34 @@ class PackageNaming : Rule("package-naming") {
}
}

// all words should contain only letters or digits
// all words should contain only ASCII letters or digits
wordsInPackageName
.filter { word -> !correctSymbolsAreUsed(word.text) }
.forEach { PACKAGE_NAME_INCORRECT_SYMBOLS.warn(confiRules, emitWarn, isFixMode, it.text, it.startOffset) }

// all words should contain only ASCII letters or digits
wordsInPackageName.forEach { correctPackageWordSeparatorsUsed(it) }
}

/**
* only letters, digits and underscore are allowed
*/
private fun correctSymbolsAreUsed(word: String): Boolean {
if (word.isASCIILettersAndDigits()) {
return true
} else {
// underscores are allowed in some cases - see "exceptionForUnderscore"
val wordFromPackage = word.replace("_", "")
if (wordFromPackage.isASCIILettersAndDigits() && exceptionForUnderscore(wordFromPackage)) {
return true
// underscores are allowed in some cases - see "exceptionForUnderscore"
val wordFromPackage = word.replace("_", "")
return wordFromPackage.isASCIILettersAndDigits()
}

/**
* in package name no other separators except dot should be used, package words (parts) should be concatenated
* without any symbols or should use dot symbol - this is the only way
*/
private fun correctPackageWordSeparatorsUsed(word: ASTNode) {
if (word.text.contains("_") && !exceptionForUnderscore(word.text)) {
INCORRECT_PACKAGE_SEPARATOR.warnAndFix(confiRules, emitWarn, isFixMode, word.text, word.startOffset) {
(word as LeafPsiElement).replaceWithText(word.text.replace("_", ""))
}
}
return false
}

/** Underscores! In some cases, if the package name starts with a number or other characters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class PackageNamingFixTest {
).isEqualTo(true)
}

@Test
fun `incorrect usage of package separator (fix)`() {
assertThat(
testComparatorUnit
.compareFilesFromResources("FixUnderscoreExpected.kt", "FixUnderscoreTest.kt")
).isEqualTo(true)
}


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PackageNamingWarnTest {
""".trimIndent()
)
).containsExactly(LintError(1, 27, "package-naming", "${PACKAGE_NAME_INCORRECT_SYMBOLS.warnText()} test_"))
).containsExactly(LintError(1, 27, "package-naming", "${INCORRECT_PACKAGE_SEPARATOR.warnText()} test_"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package /* AAAAAA */ com.huawei.ktlint.ruleset.standarddd

import com.huawei.ktlint.CORE.Rule


/**
* some comments
*
*/
class TestPackageName {
var dfsGGGG =""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package /* AAAAAA */ com.huawei.ktlint.rule_set.standarddd

import com.huawei.ktlint.CORE.Rule


/**
* some comments
*
*/
class TestPackageName {
var dfsGGGG =""
}
5 changes: 5 additions & 0 deletions rules-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,10 @@
"name": "KDOC_WITHOUT_THROWS_TAG",
"enabled": true,
"configuration": ""
},
{
"name": "INCORRECT_PACKAGE_SEPARATOR",
"enabled": true,
"configuration": ""
}
]

0 comments on commit 67fe432

Please sign in to comment.