-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 'feature/underscores'
# Conflicts: # diktat-huawei-rules/src/main/kotlin/com/huawei/rri/fixbot/ruleset/huawei/constants/Warnings.kt
- Loading branch information
Showing
11 changed files
with
246 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...huawei-rules/src/main/kotlin/com/huawei/rri/fixbot/ruleset/huawei/rules/KdocFormatting.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.huawei.rri.fixbot.ruleset.huawei.rules | ||
|
||
import com.huawei.rri.fixbot.ruleset.huawei.constants.Warnings.BLANK_LINE_AFTER_KDOC | ||
import com.huawei.rri.fixbot.ruleset.huawei.utils.countSubStringOccurrences | ||
import com.huawei.rri.fixbot.ruleset.huawei.utils.getFirstChildWithType | ||
import com.huawei.rri.fixbot.ruleset.huawei.utils.leaveOnlyOneNewLine | ||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType | ||
import com.pinterest.ktlint.core.ast.ElementType.CLASS | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY | ||
import config.rules.RulesConfig | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
/** | ||
* Formatting visitor for Kdoc: | ||
* 1) removing all blank lines between Kdoc and the code it's declaring | ||
*/ | ||
class KdocFormatting : Rule("kdoc-formatting") { | ||
|
||
private lateinit var confiRules: List<RulesConfig> | ||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
private var isFixMode: Boolean = false | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
params: KtLint.Params, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
|
||
confiRules = params.rulesConfigList!! | ||
isFixMode = autoCorrect | ||
emitWarn = emit | ||
|
||
val declarationTypes = setOf(CLASS, FUN, PROPERTY) | ||
|
||
if (declarationTypes.contains(node.elementType)) { | ||
val kdoc = node.getFirstChildWithType(ElementType.KDOC) | ||
val nodeAfterKdoc = kdoc?.treeNext | ||
val name = node.getFirstChildWithType(ElementType.IDENTIFIER) | ||
if (nodeAfterKdoc?.elementType == ElementType.WHITE_SPACE && nodeAfterKdoc.text.countSubStringOccurrences("\n") > 1) { | ||
BLANK_LINE_AFTER_KDOC.warnAndFix(confiRules, emitWarn, isFixMode, name!!.text, nodeAfterKdoc.startOffset) { | ||
nodeAfterKdoc.leaveOnlyOneNewLine() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...es/src/test/kotlin/com/huawei/rri/fixbot/ruleset/huawei/chapter2/KdocFormattingFixTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.huawei.rri.fixbot.ruleset.huawei.chapter2 | ||
|
||
import com.huawei.rri.fixbot.ruleset.huawei.constants.Warnings | ||
import com.huawei.rri.fixbot.ruleset.huawei.rules.KdocComments | ||
import com.huawei.rri.fixbot.ruleset.huawei.rules.KdocFormatting | ||
import com.huawei.rri.fixbot.ruleset.huawei.rules.PackageNaming | ||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.RuleSet | ||
import com.pinterest.ktlint.test.lint | ||
import org.assertj.core.api.Assertions | ||
import org.junit.Test | ||
import test_framework.processing.TestComparatorUnit | ||
|
||
class KdocFormattingFixTest { | ||
|
||
val testComparatorUnit = TestComparatorUnit("test/paragraph2/kdoc/", ::format) | ||
|
||
|
||
@Test | ||
fun `there should be no blank line between kdoc and it's declaration code`() { | ||
Assertions.assertThat( | ||
testComparatorUnit | ||
.compareFilesFromResources("KdocEmptyLineExpected.kt", "KdocEmptyLineTest.kt") | ||
).isEqualTo(true) | ||
} | ||
|
||
private fun format(text: String, fileName: String): String = KdocFormatting().format(text, fileName) | ||
|
||
private fun Rule.format(text: String, fileName: String): String { | ||
return KtLint.format( | ||
KtLint.Params( | ||
text = text, | ||
ruleSets = listOf(RuleSet("huawei-codestyle", this@format)), | ||
fileName = fileName, | ||
cb = { _, _ -> } | ||
) | ||
) | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...rules/src/test/kotlin/com/huawei/rri/fixbot/ruleset/huawei/chapter2/KdocFormattingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.huawei.rri.fixbot.ruleset.huawei.chapter2 | ||
|
||
import com.huawei.rri.fixbot.ruleset.huawei.constants.Warnings | ||
import com.huawei.rri.fixbot.ruleset.huawei.rules.KdocFormatting | ||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.test.lint | ||
import org.assertj.core.api.Assertions | ||
import org.junit.Test | ||
|
||
class KdocFormattingTest { | ||
@Test | ||
fun `there should be no blank line between kdoc and it's declaration code`() { | ||
Assertions.assertThat( | ||
KdocFormatting().lint( | ||
""" | ||
package com.huawei.test.resources.test.paragraph2.kdoc | ||
/** | ||
* declaration for some constant | ||
*/ | ||
const val SUPER_CONSTANT = 46 | ||
/** | ||
* Kdoc docummentation | ||
*/ | ||
class SomeName { | ||
/** | ||
* another Kdoc | ||
*/ | ||
val variable = "string" | ||
/** | ||
* another Kdoc | ||
*/ | ||
fun somePublicFunction() {} | ||
} | ||
/** | ||
* another Kdoc | ||
*/ | ||
fun someFunction() {} | ||
""".trimIndent() | ||
) | ||
).containsExactly( | ||
LintError(5, 4, "kdoc-formatting", "${Warnings.BLANK_LINE_AFTER_KDOC.warnText()} SUPER_CONSTANT"), | ||
LintError(11, 4, "kdoc-formatting", "${Warnings.BLANK_LINE_AFTER_KDOC.warnText()} SomeName"), | ||
LintError(16, 8, "kdoc-formatting", "${Warnings.BLANK_LINE_AFTER_KDOC.warnText()} variable"), | ||
LintError(22, 8, "kdoc-formatting", "${Warnings.BLANK_LINE_AFTER_KDOC.warnText()} somePublicFunction"), | ||
LintError(31, 4, "kdoc-formatting", "${Warnings.BLANK_LINE_AFTER_KDOC.warnText()} someFunction") | ||
) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
diktat-huawei-rules/src/test/resources/test/paragraph2/kdoc/KdocEmptyLineExpected.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.huawei.test.resources.test.paragraph2.kdoc | ||
|
||
/** | ||
* declaration for some constant | ||
*/ | ||
const val SUPER_CONSTANT = 46 | ||
|
||
/** | ||
* Kdoc docummentation | ||
*/ | ||
class SomeName { | ||
/** | ||
* another Kdoc | ||
*/ | ||
val a = "string" | ||
|
||
/** | ||
* another Kdoc | ||
*/ | ||
fun somePublicFunction() {} | ||
|
||
} | ||
|
||
|
||
/** | ||
* another Kdoc | ||
*/ | ||
fun someFunction() {} |
33 changes: 33 additions & 0 deletions
33
diktat-huawei-rules/src/test/resources/test/paragraph2/kdoc/KdocEmptyLineTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.huawei.test.resources.test.paragraph2.kdoc | ||
|
||
/** | ||
* declaration for some constant | ||
*/ | ||
|
||
const val SUPER_CONSTANT = 46 | ||
|
||
/** | ||
* Kdoc docummentation | ||
*/ | ||
|
||
class SomeName { | ||
/** | ||
* another Kdoc | ||
*/ | ||
|
||
val a = "string" | ||
|
||
/** | ||
* another Kdoc | ||
*/ | ||
|
||
fun somePublicFunction() {} | ||
|
||
} | ||
|
||
|
||
/** | ||
* another Kdoc | ||
*/ | ||
|
||
fun someFunction() {} |