Skip to content

Commit

Permalink
Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface (#1601)
Browse files Browse the repository at this point in the history
### What's done:
* fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface

Closes #1598
  • Loading branch information
Cheshiriks authored Jan 30, 2023
1 parent 9456032 commit 1cf345b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package org.cqfn.diktat.ruleset.rules.chapter3
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_MULTIPLE_MODIFIERS_ORDER
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType

import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.FUN_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.psiUtil.children

/**
Expand All @@ -31,6 +35,9 @@ class MultipleModifiersSequence(configRules: List<RulesConfig>) : DiktatRule(
val modifierListOfPair = node
.getChildren(KtTokens.MODIFIER_KEYWORDS)
.toList()
.filter {
!isSamInterfaces(node, it)
}
.map { Pair(it, modifierOrder.indexOf(it.elementType)) }
val sortModifierListOfPair = modifierListOfPair.sortedBy { it.second }.map { it.first }
modifierListOfPair.forEachIndexed { index, (modifierNode, _) ->
Expand All @@ -46,6 +53,15 @@ class MultipleModifiersSequence(configRules: List<RulesConfig>) : DiktatRule(
}
}

private fun isSamInterfaces(parent: ASTNode, node: ASTNode): Boolean {
val parentPsi = parent.treeParent.psi
return if (parentPsi is KtClass) {
(parentPsi.isInterface()) && node.elementType == FUN_KEYWORD && parent.treeParent.findAllDescendantsWithSpecificType(FUN).size == 1
} else {
false
}
}

private fun checkAnnotation(node: ASTNode) {
val firstModifierIndex = node
.children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ protected data class Counter(val dayIndex: Int) {
public final fun foo() {
protected open lateinit var a: List<ASTNode>
}

public internal fun interface Factory {
public fun create(): List<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ data protected class Counter(val dayIndex: Int) {
final public fun foo() {
lateinit open protected var a: List<ASTNode>
}

internal public fun interface Factory {
public fun create(): List<Int>
}

0 comments on commit 1cf345b

Please sign in to comment.