-
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.
* Custom label ### What's done: Implemented rule, added tests and documentation.
- Loading branch information
Showing
15 changed files
with
173 additions
and
10 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
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
44 changes: 44 additions & 0 deletions
44
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/CustomLabel.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,44 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.constants.Warnings.CUSTOM_LABEL | ||
import org.cqfn.diktat.ruleset.utils.loopType | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.BREAK | ||
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.CONTINUE | ||
import com.pinterest.ktlint.core.ast.ElementType.LABEL_QUALIFIER | ||
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.RETURN | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.psiUtil.parents | ||
|
||
/** | ||
* Rule that checks using custom label | ||
*/ | ||
class CustomLabel(private val configRules: List<RulesConfig>) : Rule("custom-label") { | ||
private var isFixMode: Boolean = false | ||
private val forEachReference = listOf("forEach", "forEachIndexed") | ||
private val labels = listOf("@loop", "@forEach", "@forEachIndexed") | ||
private val stopWords = listOf(RETURN, BREAK, CONTINUE) | ||
private lateinit var emitWarn: EmitType | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.elementType == LABEL_QUALIFIER && node.text !in labels && node.treeParent.elementType in stopWords) { | ||
val nestedCount = node.parents().count { | ||
it.elementType in loopType || | ||
(it.elementType == CALL_EXPRESSION && it.findChildByType(REFERENCE_EXPRESSION)?.text in forEachReference) | ||
} | ||
if (nestedCount == 1) { | ||
CUSTOM_LABEL.warn(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) | ||
} | ||
} | ||
} | ||
} |
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
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
75 changes: 75 additions & 0 deletions
75
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/CustomLabelsTest.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,75 @@ | ||
package org.cqfn.diktat.ruleset.chapter5 | ||
|
||
import org.cqfn.diktat.ruleset.constants.Warnings.CUSTOM_LABEL | ||
import org.cqfn.diktat.ruleset.rules.CustomLabel | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.util.LintTestBase | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class CustomLabelsTest : LintTestBase(::CustomLabel) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:custom-label" | ||
|
||
@Test | ||
@Tag(WarningNames.CUSTOM_LABEL) | ||
fun `should trigger custom label`() { | ||
lintMethod( | ||
""" | ||
fun foo() { | ||
run qwe@ { | ||
q.forEach { | ||
return@qwe | ||
} | ||
} | ||
q.forEachIndexed { index, i -> | ||
return@forEachIndexed | ||
} | ||
loop@ for(i: Int in q) { | ||
println(i) | ||
break@loop | ||
} | ||
qq@ for(i: Int in q) { | ||
println(i) | ||
break@qq | ||
} | ||
q.run { | ||
it.map { | ||
it.foreach{ | ||
break@forEach | ||
} | ||
} | ||
} | ||
} | ||
""".trimMargin(), | ||
LintError(4, 39, ruleId, "${CUSTOM_LABEL.warnText()} @qwe", false), | ||
LintError(16, 34, ruleId, "${CUSTOM_LABEL.warnText()} @qq", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.CUSTOM_LABEL) | ||
fun `should not trigger custom label in nested expression`() { | ||
lintMethod( | ||
""" | ||
fun foo() { | ||
qq@ for(i: Int in q) { | ||
for (j: Int in q) { | ||
println(i) | ||
break@qq | ||
} | ||
} | ||
q.forEach outer@ { | ||
it.forEach { | ||
if(it == 21) | ||
return@outer | ||
} | ||
} | ||
} | ||
""".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
6b0b9d2
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.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: