Skip to content

Commit

Permalink
feature/no-extension-func-with-class(#731)
Browse files Browse the repository at this point in the history
### What's done:
  * Fixed bugs
  • Loading branch information
aktsay6 committed Jan 28, 2021
1 parent 95db401 commit 9d686ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
12 changes: 6 additions & 6 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public object WarningNames {

public const val FILE_WILDCARD_IMPORTS: String = "FILE_WILDCARD_IMPORTS"

public const val UNUSED_IMPORT: String = "UNUSED_IMPORT"

public const val NO_BRACES_IN_CONDITIONALS_AND_LOOPS: String =
"NO_BRACES_IN_CONDITIONALS_AND_LOOPS"

Expand Down Expand Up @@ -203,8 +205,12 @@ public object WarningNames {

public const val RUN_BLOCKING_INSIDE_ASYNC: String = "RUN_BLOCKING_INSIDE_ASYNC"

public const val TOO_MANY_LINES_IN_LAMBDA: String = "TOO_MANY_LINES_IN_LAMBDA"

public const val CUSTOM_LABEL: String = "CUSTOM_LABEL"

public const val INVERSE_FUNCTION_PREFERRED: String = "INVERSE_FUNCTION_PREFERRED"

public const val SINGLE_CONSTRUCTOR_SHOULD_BE_PRIMARY: String =
"SINGLE_CONSTRUCTOR_SHOULD_BE_PRIMARY"

Expand Down Expand Up @@ -236,12 +242,6 @@ public object WarningNames {

public const val OBJECT_IS_PREFERRED: String = "OBJECT_IS_PREFERRED"

public const val INVERSE_FUNCTION_PREFERRED: String = "INVERSE_FUNCTION_PREFERRED"

public const val TOO_MANY_LINES_IN_LAMBDA: String = "TOO_MANY_LINES_IN_LAMBDA"

public const val UNUSED_IMPORT: String = "UNUSED_IMPORT"

public const val INLINE_CLASS_CAN_BE_USED: String = "INLINE_CLASS_CAN_BE_USED"

public const val EXTENSION_FUNCTION_WITH_CLASS: String = "EXTENSION_FUNCTION_WITH_CLASS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.hasChildOfType

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.DOT
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
import com.pinterest.ktlint.core.ast.prevSibling
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFunction
Expand Down Expand Up @@ -53,13 +51,11 @@ class ExtensionFunctionsInFileRule(private val configRules: List<RulesConfig>) :
Warnings.EXTENSION_FUNCTION_WITH_CLASS.warn(configRules, emitWarn, isFixMode, "fun ${(node.psi as KtFunction).name}", node.startOffset, node)
}

private fun collectAllExtensionFunctionsWithSameClassName(node: ASTNode, classNames: List<String>): List<ASTNode> {
return node.getAllChildrenWithType(FUN).filter { isExtensionFunctionWithClassName(it, classNames) }
}
private fun collectAllExtensionFunctionsWithSameClassName(node: ASTNode, classNames: List<String>): List<ASTNode> =
node.getAllChildrenWithType(FUN).filter { isExtensionFunctionWithClassName(it, classNames) }


@Suppress("UnsafeCallOnNullableType")
private fun isExtensionFunctionWithClassName(node: ASTNode, classNames: List<String>): Boolean =
node.getFirstChildWithType(IDENTIFIER)!!.prevSibling { it.elementType == TYPE_REFERENCE } != null
&& node.getFirstChildWithType(IDENTIFIER)!!.prevSibling { it.elementType == TYPE_REFERENCE }!!.text in classNames

node.getFirstChildWithType(IDENTIFIER)!!.prevSibling { it.elementType == TYPE_REFERENCE }?.text in classNames
}

0 comments on commit 9d686ed

Please sign in to comment.