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 3 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 @@ -5,11 +5,13 @@ import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_MULTIPLE_MODIFIERS_ORDER
import org.cqfn.diktat.ruleset.rules.DiktatRule

import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY
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 +33,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 +51,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
} 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 @@ -221,7 +221,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
lintMethod(
"""
/*
* Copyright (c) 2022 My Company, Ltd. All rights reserved.
* Copyright (c) 2023 My Company, Ltd. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
Expand All @@ -244,7 +244,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
lintMethod(
"""
/*
* Copyright (c) My Company, Ltd. 2012-2022. All rights reserved.
* Copyright (c) My Company, Ltd. 2012-2023. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
Expand All @@ -267,7 +267,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
lintMethod(
"""
/*
Copyright (c) My Company, Ltd. 2021-2022. All rights reserved.
Copyright (c) My Company, Ltd. 2021-2023. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
Expand All @@ -290,7 +290,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
lintMethod(
"""
/*
* Copyright (c) My Company, Ltd. 2002-2022. All rights reserved.
* Copyright (c) My Company, Ltd. 2002-2023. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
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>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ;warn:$line:1: [FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: Example5Expected.kt vs Some{{.*}}
/*
Copyright 2018-2022 John Doe.
Copyright 2018-2023 John Doe.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down