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:
 * Fixed bugs
  • Loading branch information
aktsay6 committed Dec 31, 2020
1 parent c488882 commit 09ffef9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident
private var isFixMode: Boolean = false
private lateinit var emitWarn: EmitType

val configuration by lazy {
BooleanFunctionsConfiguration(
this.configRules.getRuleConfig(FUNCTION_BOOLEAN_PREFIX)?.configuration ?: emptyMap()
)
}

private val allMethodPrefixes by lazy {
if (configuration.allowedBooleanPrefixes.isNullOrEmpty()) {
booleanMethodPrefixes
} else {
booleanMethodPrefixes + configuration.allowedBooleanPrefixes.filter { it.isNotEmpty() }
}
}

override fun visit(
node: ASTNode,
autoCorrect: Boolean,
Expand Down Expand Up @@ -359,18 +373,6 @@ 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()) {
val configuration by lazy {
BooleanFunctionsConfiguration(
this.configRules.getRuleConfig(FUNCTION_BOOLEAN_PREFIX)?.configuration ?: emptyMap()
)
}
val allMethodPrefixes by lazy {
if (configuration.allowedBooleanPrefixes.isNullOrEmpty()) {
booleanMethodPrefixes
} else {
booleanMethodPrefixes + configuration.allowedBooleanPrefixes.filter { it.isNotEmpty() }
}
}
if (allMethodPrefixes.none { functionName.text.startsWith(it) }) {
FUNCTION_BOOLEAN_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, functionName.text, functionName.startOffset, functionName) {
// FixMe: add agressive autofix for this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class PackageNaming(private val configRules: List<RulesConfig>) : Rule("package-
/**
* only letters, digits and underscore are allowed
*/
@Suppress("FUNCTION_BOOLEAN_PREFIX")
private fun areCorrectSymbolsUsed(word: String): Boolean {
// underscores are allowed in some cases - see "exceptionForUnderscore"
val wordFromPackage = word.replace("_", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class DataClassesRule(private val configRule: List<RulesConfig>) : Rule("data-cl
/**
* Checks if any property with accessor contains logic in accessor
*/
@Suppress("FUNCTION_BOOLEAN_PREFIX")
private fun areGoodProps(node: ASTNode): Boolean {
val propertiesWithAccessors = node.getAllChildrenWithType(PROPERTY).filter { it.hasChildOfType(PROPERTY_ACCESSOR) }

Expand All @@ -109,7 +108,7 @@ class DataClassesRule(private val configRule: List<RulesConfig>) : Rule("data-cl
return true
}

@Suppress("UnsafeCallOnNullableType", "FUNCTION_BOOLEAN_PREFIX")
@Suppress("UnsafeCallOnNullableType")
private fun areGoodAccessors(accessors: List<ASTNode>): Boolean {
accessors.forEach {
if (it.hasChildOfType(BLOCK)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ fun ASTNode.isChildBeforeGroup(child: ASTNode, group: List<ASTNode>): Boolean =
*
* @return boolean result
*/
@Suppress("FUNCTION_BOOLEAN_PREFIX")
fun ASTNode.areChildrenBeforeChild(children: List<ASTNode>, beforeChild: ASTNode): Boolean =
areChildrenBeforeGroup(children, listOf(beforeChild))

Expand All @@ -635,7 +634,7 @@ fun ASTNode.areChildrenBeforeChild(children: List<ASTNode>, beforeChild: ASTNode
*
* @return boolean result
*/
@Suppress("UnsafeCallOnNullableType", "FUNCTION_BOOLEAN_PREFIX")
@Suppress("UnsafeCallOnNullableType")
fun ASTNode.areChildrenBeforeGroup(children: List<ASTNode>, group: List<ASTNode>): Boolean {
require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" }
return children.map { getChildren(null).indexOf(it) }.max()!! < group.map { getChildren(null).indexOf(it) }.min()!!
Expand Down

0 comments on commit 09ffef9

Please sign in to comment.