-
Notifications
You must be signed in to change notification settings - Fork 39
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
Rule 6.2.2(#447) #501
Merged
Merged
Rule 6.2.2(#447) #501
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0af4041
feature/rule-6.2.2(#447)
aktsay6 946c05f
feature/rule-6.2.2(#447)
aktsay6 1898746
feature/rule-6.2.2(#447)
aktsay6 157bb60
Merge branch 'master' into feature/rule-6.2.2(#447)
aktsay6 af5857a
feature/rule-6.2.2($447)
aktsay6 2448a35
feature/rule-6.2.2(#447)
aktsay6 de12494
Merge branch 'master' into feature/rule-6.2.2(#447)
aktsay6 5824e47
Merge branch 'master' into feature/rule-6.2.2(#447)
aktsay6 163a46c
feature/rule-6.2.2(#447)
aktsay6 d8f5a85
feature/rule-6.2.2(#447)
aktsay6 fb83104
Merge branch 'master' into feature/rule-6.2.2(#447)
aktsay6 346e1ee
Merge branch 'master' into feature/rule-6.2.2(#447)
aktsay6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/ExtensionFunctionsSameNameRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.CLASS | ||
import com.pinterest.ktlint.core.ast.ElementType.COLON | ||
import com.pinterest.ktlint.core.ast.ElementType.DOT | ||
import com.pinterest.ktlint.core.ast.ElementType.FILE | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER | ||
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_CALL_ENTRY | ||
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_LIST | ||
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE | ||
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST | ||
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE | ||
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType | ||
import org.cqfn.diktat.ruleset.utils.findChildAfter | ||
import org.cqfn.diktat.ruleset.utils.findChildBefore | ||
import org.cqfn.diktat.ruleset.utils.findLeafWithSpecificType | ||
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType | ||
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType | ||
import org.cqfn.diktat.ruleset.utils.hasChildOfType | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtClass | ||
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.jetbrains.kotlin.psi.KtParameter | ||
import org.jetbrains.kotlin.psi.KtParameterList | ||
|
||
typealias RelatedClasses = List<Pair<String, String>> | ||
typealias SimilarSignatures = List<Pair<ExtensionFunctionsSameNameRule.ExtensionFunction, ExtensionFunctionsSameNameRule.ExtensionFunction>> | ||
|
||
/** | ||
* This rule checks if extension functions with the same signature don't have related classes | ||
*/ | ||
class ExtensionFunctionsSameNameRule(private val configRules: List<RulesConfig>) : Rule("extension-functions-same-name") { | ||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
private var isFixMode: Boolean = false | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
/** | ||
* 1) Collect all classes that extend other classes (collect related classes) | ||
* 2) Collect all extension functions with same signature | ||
* 3) Check if classes of functions are related | ||
*/ | ||
if (node.elementType == FILE) { | ||
val relatedClasses = collectAllRelatedClasses(node) | ||
val extFunctionsWithSameName = collectAllExtensionFunctions(node) | ||
handleFunctions(relatedClasses, extFunctionsWithSameName) | ||
} | ||
|
||
} | ||
|
||
// Fixme: should find all related classes in project, not only in file | ||
@Suppress("UnsafeCallOnNullableType") | ||
private fun collectAllRelatedClasses(node: ASTNode) : List<Pair<String, String>> { | ||
val classListWithInheritance = node.findAllNodesWithSpecificType(CLASS) | ||
.filterNot { (it.psi as KtClass).isInterface() } | ||
.filter { it.hasChildOfType(SUPER_TYPE_LIST) } | ||
|
||
val pairs = mutableListOf<Pair<String, String>>() | ||
classListWithInheritance.forEach { classNode -> | ||
val callEntries = classNode.findChildByType(SUPER_TYPE_LIST)!!.getAllChildrenWithType(SUPER_TYPE_CALL_ENTRY) | ||
|
||
callEntries.forEach { entry -> | ||
aktsay6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val className = (classNode.psi as KtClass).name!! | ||
val entryName = entry.findLeafWithSpecificType(IDENTIFIER)!! | ||
pairs.add(Pair(className, entryName.text)) | ||
} | ||
} | ||
return pairs | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun collectAllExtensionFunctions(node: ASTNode) : SimilarSignatures { | ||
val extensionFunctionList = node.findAllNodesWithSpecificType(FUN).filter { it.hasChildOfType(TYPE_REFERENCE) && it.hasChildOfType(DOT) } | ||
val distinctFunctionSignatures = mutableMapOf<FunctionSignature, ASTNode>() // maps function signatures on node it is used by | ||
val extensionFunctionsPairs = mutableListOf<Pair<ExtensionFunction, ExtensionFunction>>() // pairs extension functions with same signature | ||
|
||
extensionFunctionList.forEach { func -> | ||
val functionName = (func.psi as KtNamedFunction).name!! | ||
// List<String> is used to show param names in warning | ||
val params = (func.getFirstChildWithType(VALUE_PARAMETER_LIST)!!.psi as KtParameterList).parameters.map { it.name!! } | ||
val returnType = func.findChildAfter(COLON, TYPE_REFERENCE)?.text | ||
val className = func.findChildBefore(DOT, TYPE_REFERENCE)!!.text | ||
val signature = FunctionSignature(functionName, params, returnType) | ||
|
||
if (distinctFunctionSignatures.contains(signature)) { | ||
val secondFuncClassName = distinctFunctionSignatures[signature]!!.findChildBefore(DOT, TYPE_REFERENCE)!!.text | ||
extensionFunctionsPairs.add(Pair( | ||
ExtensionFunction(secondFuncClassName, signature, distinctFunctionSignatures[signature]!!), | ||
ExtensionFunction(className, signature, func))) | ||
} else { | ||
distinctFunctionSignatures[signature] = func | ||
} | ||
} | ||
|
||
return extensionFunctionsPairs | ||
} | ||
|
||
private fun handleFunctions(relatedClasses: RelatedClasses, functions: SimilarSignatures) { | ||
functions.forEach { | ||
val firstClassName = it.first.className | ||
val secondClassName = it.second.className | ||
|
||
if (relatedClasses.hasRelatedClasses(Pair(firstClassName, secondClassName))) { | ||
raiseWarning(it.first.node, it.first, it.second) | ||
raiseWarning(it.second.node, it.first, it.second) | ||
} | ||
} | ||
} | ||
|
||
private fun RelatedClasses.hasRelatedClasses(pair: Pair<String, String>): Boolean { | ||
return any { it.first == pair.first && it.second == pair.second | ||
|| it.first == pair.second && it.second == pair.first } | ||
} | ||
|
||
private fun raiseWarning(node: ASTNode, firstFunc: ExtensionFunction, secondFunc: ExtensionFunction) { | ||
EXTENSION_FUNCTION_SAME_SIGNATURE.warn(configRules, emitWarn, isFixMode, "$firstFunc and $secondFunc", node.startOffset, node) | ||
} | ||
|
||
data class FunctionSignature(val name: String, val parameters: List<String>, val returnType: String?) { | ||
override fun toString(): String { | ||
return "$name$parameters${if(returnType != null) ": $returnType" else ""}" | ||
} | ||
} | ||
data class ExtensionFunction(val className: String, val signature: FunctionSignature, val node: ASTNode) { | ||
override fun toString(): String { | ||
return "fun $className.$signature" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...es/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/ExtensionFunctionsSameNameWarnTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames.EXTENSION_FUNCTION_SAME_SIGNATURE | ||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.ExtensionFunctionsSameNameRule | ||
import org.cqfn.diktat.util.LintTestBase | ||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class ExtensionFunctionsSameNameWarnTest : LintTestBase(::ExtensionFunctionsSameNameRule) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:extension-functions-same-name" | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should trigger on functions with same signatures`() { | ||
lintMethod( | ||
""" | ||
|open class A | ||
|class B: A(), C | ||
|class D: A() | ||
| | ||
|fun A.foo() = "A" | ||
|fun B.foo() = "B" | ||
|fun D.foo() = "D" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin(), | ||
LintError(5, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[] and fun B.foo[]"), | ||
LintError(5, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[] and fun D.foo[]"), | ||
LintError(6, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[] and fun B.foo[]"), | ||
LintError(7, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[] and fun D.foo[]"), | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should trigger on functions with same signatures 2`() { | ||
lintMethod( | ||
""" | ||
|open class A | ||
|class B: A(), C | ||
| | ||
|fun A.foo(some: Int) = "A" | ||
|fun B.foo(some: Int) = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin(), | ||
LintError(4, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[some] and fun B.foo[some]"), | ||
LintError(5, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo[some] and fun B.foo[some]") | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should not trigger on functions with different signatures`() { | ||
lintMethod( | ||
""" | ||
|open class A | ||
|class B: A(), C | ||
| | ||
|fun A.foo(): Boolean = return true | ||
|fun B.foo() = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should not trigger on functions with different signatures 2`() { | ||
lintMethod( | ||
""" | ||
|open class A | ||
|class B: A(), C | ||
| | ||
|fun A.foo() = return true | ||
|fun B.foo(some: Int) = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should not trigger on functions with unrelated classes`() { | ||
lintMethod( | ||
""" | ||
|interface A | ||
|class B: A | ||
|class C | ||
| | ||
|fun C.foo() = "C" | ||
|fun B.foo() = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should not trigger on regular func`() { | ||
lintMethod( | ||
""" | ||
|interface A | ||
|class B: A | ||
|class C | ||
| | ||
|fun foo() = "C" | ||
|fun bar() = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@Tag(EXTENSION_FUNCTION_SAME_SIGNATURE) | ||
fun `should trigger on classes in other files`() { | ||
lintMethod( | ||
""" | ||
|fun A.foo() = "A" | ||
|fun B.foo() = "B" | ||
| | ||
|fun printClassName(s: A) { print(s.foo()) } | ||
| | ||
|fun main() { printClassName(B()) } | ||
""".trimMargin(), | ||
LintError(1, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo() and fun B.foo()"), | ||
LintError(2, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_SAME_SIGNATURE.warnText()} fun A.foo() and fun B.foo()") | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if there are more than two matching classes? It should be a rare case, but at least we shouldn't crash when we encounter it and output something reasonable.