Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface #1601

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
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>
}