Skip to content

Commit

Permalink
feature/more-prefixes-boolean-methods(#417)
Browse files Browse the repository at this point in the history
### What's done:
  * Added more prefixes
  • Loading branch information
aktsay6 committed Dec 29, 2020
1 parent 58cb8fa commit 6ff4edb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
enabled: true
- name: FUNCTION_BOOLEAN_PREFIX
enabled: true
configuration:
allowedFunctions: "" # A list of functions that return boolean and are allowed to use. Input is in a form "foo, bar".
- name: FUNCTION_NAME_INCORRECT_CASE
enabled: true
- name: GENERIC_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
import org.cqfn.diktat.ruleset.constants.Warnings
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -358,7 +359,10 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident
if (!node.isOverridden()) {
// if function has Boolean return type in 99% of cases it is much better to name it with isXXX or hasXXX prefix
if (functionReturnType != null && functionReturnType == PrimitiveType.BOOLEAN.typeName.asString()) {
if (booleanMethodPrefixes.none { functionName.text.startsWith(it) }) {
val configuration = BooleanFunctionsConfiguration(
this.configRules.getRuleConfig(FUNCTION_BOOLEAN_PREFIX)?.configuration ?: emptyMap()
)
if (booleanMethodPrefixes.none { functionName.text.startsWith(it) } && functionName.text !in configuration.allowedBooleanFunctions) {
FUNCTION_BOOLEAN_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, functionName.text, functionName.startOffset, functionName) {
// FixMe: add agressive autofix for this
}
Expand Down Expand Up @@ -426,6 +430,13 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident
} ?: Style.SNAKE_CASE
}

class BooleanFunctionsConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
/**
* A list of functions that return boolean and are allowed to use. Input is in a form "foo, bar".
*/
val allowedBooleanFunctions = config["allowedFunctions"]?.split(",")?.map { it.trim() } ?: emptyList()
}

companion object {
const val MAX_IDENTIFIER_LENGTH = 64
const val MIN_IDENTIFIER_LENGTH = 2
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
enabled: true
- name: FUNCTION_BOOLEAN_PREFIX
enabled: true
configuration:
allowedFunctions: "" # A list of functions that return boolean and are allowed to use. Input is in a form "foo, bar".
- name: FUNCTION_NAME_INCORRECT_CASE
enabled: true
- name: GENERIC_NAME
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
enabled: true
- name: FUNCTION_BOOLEAN_PREFIX
enabled: true
configuration:
allowedFunctions: "" # A list of functions that return boolean and are allowed to use. Input is in a form "foo, bar".
- name: FUNCTION_NAME_INCORRECT_CASE
enabled: true
- name: GENERIC_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import org.junit.jupiter.api.Test
class IdentifierNamingWarnTest : LintTestBase(::IdentifierNaming) {
private val ruleId: String = "$DIKTAT_RULE_SET_ID:identifier-naming"

private val rulesConfigBooleanFunctions: List<RulesConfig> = listOf(
RulesConfig(FUNCTION_BOOLEAN_PREFIX.name, true,
mapOf("allowedFunctions" to "equals, equivalent, foo"))
)
// ======== checks for generics ========
@Test
@Tag(WarningNames.GENERIC_NAME)
Expand Down Expand Up @@ -392,6 +396,33 @@ class IdentifierNamingWarnTest : LintTestBase(::IdentifierNaming) {
)
}

@Test
@Tag(WarningNames.FUNCTION_BOOLEAN_PREFIX)
fun `all prefixes for boolean methods`() {
lintMethod(
"""
fun hasEmptyLineAfter(): Boolean { }
fun haveEmptyLineAfter(): Boolean { }
fun isEmpty(): Boolean { }
fun shouldBeEmpty(): Boolean { }
fun areEmpty(): Boolean { }
""".trimIndent()
)
}

@Test
@Tag(WarningNames.FUNCTION_BOOLEAN_PREFIX)
fun `test allowed boolean functions in configuration`() {
lintMethod(
"""
fun equals(): Boolean { }
fun foo(): Boolean { }
fun equivalent(): Boolean { }
""".trimIndent(),
rulesConfigList = rulesConfigBooleanFunctions
)
}

@Test
@Tag(WarningNames.IDENTIFIER_LENGTH)
fun `regression - function argument type`() {
Expand Down

0 comments on commit 6ff4edb

Please sign in to comment.