-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/no-extension-func-with-class(#731)
### What's done: * Logic done * Added warn tests
- Loading branch information
Showing
12 changed files
with
209 additions
and
5 deletions.
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
54 changes: 54 additions & 0 deletions
54
...es/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/ExtensionFunctionsInFileRule.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,54 @@ | ||
package org.cqfn.diktat.ruleset.rules.chapter6 | ||
|
||
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.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 org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
|
||
/** | ||
* This rule checks if there are any extension functions in the file, where class is already defined. | ||
*/ | ||
class ExtensionFunctionsInFileRule(private val configRules: List<RulesConfig>) : Rule("extension-functions-class-file") { | ||
private var isFixMode: Boolean = false | ||
private lateinit var emitWarn: EmitType | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.elementType == ElementType.FILE && node.hasChildOfType(CLASS)) { | ||
collectAllExtensionFunctions(node).forEach { | ||
fireWarning(it) | ||
} | ||
} | ||
} | ||
|
||
private fun fireWarning(node: ASTNode) { | ||
Warnings.EXTENSION_FUNCTION_WITH_CLASS.warn(configRules, emitWarn, isFixMode, "fun ${(node.psi as KtFunction).name}", node.startOffset, node) | ||
} | ||
|
||
private fun collectAllExtensionFunctions(node: ASTNode): List<ASTNode> { | ||
return node.findAllNodesWithSpecificType(FUN).filter { isExtensionFunction(it) } | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun isExtensionFunction(node: ASTNode): Boolean = | ||
node | ||
.getFirstChildWithType(IDENTIFIER)!! | ||
.treePrev | ||
.elementType == DOT | ||
|
||
} |
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
116 changes: 116 additions & 0 deletions
116
...ules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/ExtensionFunctionsInFileWarnTest.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,116 @@ | ||
package org.cqfn.diktat.ruleset.chapter6 | ||
|
||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.chapter6.ExtensionFunctionsInFileRule | ||
import org.cqfn.diktat.util.LintTestBase | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames.EXTENSION_FUNCTION_WITH_CLASS | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class ExtensionFunctionsInFileWarnTest : LintTestBase(::ExtensionFunctionsInFileRule) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:extension-functions-class-file" | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should warn on function`() { | ||
lintMethod( | ||
""" | ||
|class Some1 private constructor () { | ||
| | ||
|} | ||
| | ||
|private fun String.coolStr() { | ||
| | ||
|} | ||
""".trimMargin(), | ||
LintError(5, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_WITH_CLASS.warnText()} fun coolStr") | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should warn on several functions`() { | ||
lintMethod( | ||
""" | ||
|class Some1 private constructor () { | ||
| | ||
|} | ||
| | ||
|private fun /* Random comment */ String.coolStr() { | ||
| | ||
|} | ||
| | ||
|private fun Another.extMethod() { | ||
| | ||
|} | ||
""".trimMargin(), | ||
LintError(5, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_WITH_CLASS.warnText()} fun coolStr"), | ||
LintError(9, 1, ruleId, "${Warnings.EXTENSION_FUNCTION_WITH_CLASS.warnText()} fun extMethod"), | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should not raise a warning when there is no class`() { | ||
lintMethod( | ||
""" | ||
|private fun String.coolStr() { | ||
| | ||
|} | ||
| | ||
|private fun /* Random comment */ Another.extMethod() { | ||
| | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should not raise a warning when there is no extension functions`() { | ||
lintMethod( | ||
""" | ||
|class Some { | ||
| | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should raise a warning when extension function is in the class`() { | ||
lintMethod( | ||
""" | ||
|class Some { | ||
| | ||
| fun String.str() { | ||
| | ||
| } | ||
|} | ||
""".trimMargin(), | ||
LintError(3, 4, ruleId, "${Warnings.EXTENSION_FUNCTION_WITH_CLASS.warnText()} fun str") | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(EXTENSION_FUNCTION_WITH_CLASS) | ||
fun `should not trigger on regular functions in the same file with class`() { | ||
lintMethod( | ||
""" | ||
|class Some { | ||
| fun foo() { | ||
| | ||
| } | ||
|} | ||
| | ||
|fun bar() { | ||
| | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
} |
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