-
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.
Rule 6.4.1: Avoid using utility classes/objects (#508)
* Rule 6.4.1 ### What's done: Made a rule. Added documentation. Fixed according to out code-style
- Loading branch information
Showing
9 changed files
with
218 additions
and
1 deletion.
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
67 changes: 67 additions & 0 deletions
67
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/AvoidUtilityClass.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,67 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.common.config.rules.getCommonConfiguration | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.constants.Warnings.AVOID_USING_UTILITY_CLASS | ||
import org.cqfn.diktat.ruleset.utils.* | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT | ||
import com.pinterest.ktlint.core.ast.ElementType.CLASS | ||
import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY | ||
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER | ||
import com.pinterest.ktlint.core.ast.ElementType.KDOC | ||
import com.pinterest.ktlint.core.ast.ElementType.LBRACE | ||
import com.pinterest.ktlint.core.ast.ElementType.OBJECT_DECLARATION | ||
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR | ||
import com.pinterest.ktlint.core.ast.ElementType.RBRACE | ||
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.psiUtil.children | ||
|
||
/** | ||
* Rule 6.4.1 checks that class/object, with a word "util" in its name, has only functions. | ||
*/ | ||
class AvoidUtilityClass(private val configRules: List<RulesConfig>) : Rule("avoid-utility-class") { | ||
private var isFixMode: Boolean = false | ||
private lateinit var emitWarn: EmitType | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
val config by configRules.getCommonConfiguration() | ||
val fileName = node.getRootNode().getFileName() | ||
if (!(node.hasTestAnnotation() || isLocatedInTest(fileName.splitPathToDirs(), config.testAnchors))) { | ||
if (node.elementType == OBJECT_DECLARATION || node.elementType == CLASS) { | ||
checkClass(node) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType", "WRONG_NEWLINES") | ||
private fun checkClass(node: ASTNode) { | ||
// checks that class/object doesn't contain primary constructor and its identifier doesn't has "utli" | ||
if (!node.hasChildOfType(IDENTIFIER) || node.hasChildOfType(PRIMARY_CONSTRUCTOR) || | ||
!node.findChildByType(IDENTIFIER)!!.text.toLowerCase().contains("util")) { | ||
return | ||
} | ||
node.findChildByType(CLASS_BODY) | ||
?.children() | ||
?.toList() | ||
?.takeIf { childList -> childList.all { it.elementType in utilityClassChildren } } | ||
?.filter { it.elementType == FUN } | ||
?.ifEmpty { return } | ||
?: return | ||
AVOID_USING_UTILITY_CLASS.warn(configRules, emitWarn, isFixMode, node.findChildByType(IDENTIFIER)?.text ?: node.text, node.startOffset, node) | ||
} | ||
|
||
companion object { | ||
private val utilityClassChildren = listOf(LBRACE, WHITE_SPACE, FUN, RBRACE, KDOC, | ||
EOL_COMMENT, BLOCK_COMMENT, OBJECT_DECLARATION) | ||
} | ||
} |
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
136 changes: 136 additions & 0 deletions
136
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/AvoidUtilityClassWarnTest.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,136 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import org.cqfn.diktat.ruleset.constants.Warnings.AVOID_USING_UTILITY_CLASS | ||
import org.cqfn.diktat.ruleset.rules.AvoidUtilityClass | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.util.LintTestBase | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class AvoidUtilityClassWarnTest : LintTestBase(::AvoidUtilityClass) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:avoid-utility-class" | ||
|
||
@Test | ||
@Tag(WarningNames.AVOID_USING_UTILITY_CLASS) | ||
fun `simple test`() { | ||
lintMethod( | ||
""" | ||
|object StringUtil { | ||
| fun stringInfo(myString: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
| | ||
|class A() { | ||
| fun foo() { } | ||
|} | ||
| | ||
|class StringUtils { | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
| | ||
|class StringUtil { | ||
| val z = "hello" | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
LintError(1, 1, ruleId, "${AVOID_USING_UTILITY_CLASS.warnText()} StringUtil"), | ||
LintError(11, 1, ruleId, "${AVOID_USING_UTILITY_CLASS.warnText()} StringUtils") | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.AVOID_USING_UTILITY_CLASS) | ||
fun `test with comment anf companion`() { | ||
lintMethod( | ||
""" | ||
| | ||
|class StringUtils { | ||
| companion object { | ||
| private val name = "Hello" | ||
| } | ||
| /** | ||
| * @param tex | ||
| */ | ||
| fun goo(tex: String): Int { | ||
| //hehe | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
| | ||
|class StringUtil { | ||
| /* | ||
| | ||
| */ | ||
| companion object { | ||
| } | ||
| val z = "hello" | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
LintError(2, 1, ruleId, "${AVOID_USING_UTILITY_CLASS.warnText()} StringUtils") | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.AVOID_USING_UTILITY_CLASS) | ||
fun `test with class without identifier`() { | ||
lintMethod( | ||
""" | ||
fun foo() { | ||
window.addMouseListener(object : MouseAdapter() { | ||
override fun mouseClicked(e: MouseEvent) { /*...*/ } | ||
override fun mouseEntered(e: MouseEvent) { /*...*/ } | ||
}) | ||
} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.AVOID_USING_UTILITY_CLASS) | ||
fun `check test-class`() { | ||
lintMethod( | ||
""" | ||
|class StringUtils { | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
LintError(1, 1, ruleId, "${AVOID_USING_UTILITY_CLASS.warnText()} StringUtils"), | ||
fileName = "src/main/kotlin/org/cqfn/diktat/Example.kt", | ||
) | ||
lintMethod( | ||
""" | ||
|@Test | ||
|class StringUtils1 { | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
fileName = "src/test/kotlin/org/cqfn/diktat/Example.kt" | ||
) | ||
lintMethod( | ||
""" | ||
|class StringUtils2 { | ||
| fun goo(tex: String): Int { | ||
| return myString.count{ "something".contains(it) } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
fileName = "src/test/kotlin/org/cqfn/diktat/UtilTest.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
80cad9d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: