-
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.
* added DebugPrintRule * supported detection of kotlin.io.print()\kotlin.io.println() * added detection of kotlin.js.console.*
- Loading branch information
Showing
14 changed files
with
261 additions
and
3 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
70 changes: 70 additions & 0 deletions
70
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/DebugPrintRule.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,70 @@ | ||
package org.cqfn.diktat.ruleset.rules.chapter3 | ||
|
||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.rules.DiktatRule | ||
|
||
import com.pinterest.ktlint.core.ast.ElementType | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet | ||
|
||
/** | ||
* This rule detects `print()` or `println()`. | ||
* Assumption that it's a debug logging | ||
* | ||
*/ | ||
class DebugPrintRule(configRules: List<RulesConfig>) : DiktatRule( | ||
NAME_ID, | ||
configRules, | ||
listOf(Warnings.DEBUG_PRINT) | ||
) { | ||
override fun logic(node: ASTNode) { | ||
checkPrintln(node) | ||
checkJsConsole(node) | ||
} | ||
|
||
// check kotlin.io.print()/kotlin.io.println() | ||
private fun checkPrintln(node: ASTNode) { | ||
if (node.elementType == ElementType.CALL_EXPRESSION) { | ||
val referenceExpression = node.findChildByType(ElementType.REFERENCE_EXPRESSION)?.text | ||
val valueArgumentList = node.findChildByType(ElementType.VALUE_ARGUMENT_LIST) | ||
if (referenceExpression in setOf("print", "println") && | ||
node.treePrev?.elementType != ElementType.DOT && | ||
valueArgumentList?.getChildren(TokenSet.create(ElementType.VALUE_ARGUMENT))?.size?.let { it <= 1 } == true && | ||
node.findChildByType(ElementType.LAMBDA_ARGUMENT) == null) { | ||
Warnings.DEBUG_PRINT.warn( | ||
configRules, emitWarn, isFixMode, | ||
"found $referenceExpression()", node.startOffset, node, | ||
) | ||
} | ||
} | ||
} | ||
|
||
// check kotlin.js.console.*() | ||
private fun checkJsConsole(node: ASTNode) { | ||
if (node.elementType == ElementType.DOT_QUALIFIED_EXPRESSION) { | ||
val isConsole = node.firstChildNode.let { referenceExpression -> | ||
referenceExpression.elementType == ElementType.REFERENCE_EXPRESSION && | ||
referenceExpression.firstChildNode.let { it.elementType == ElementType.IDENTIFIER && it.text == "console" } | ||
} | ||
if (isConsole) { | ||
val logMethod = node.lastChildNode | ||
.takeIf { it.elementType == ElementType.CALL_EXPRESSION } | ||
?.takeIf { it.findChildByType(ElementType.LAMBDA_ARGUMENT) == null } | ||
?.firstChildNode | ||
?.takeIf { it.elementType == ElementType.REFERENCE_EXPRESSION } | ||
?.text | ||
if (logMethod in setOf("error", "info", "log", "warn")) { | ||
Warnings.DEBUG_PRINT.warn( | ||
configRules, emitWarn, isFixMode, | ||
"found console.$logMethod()", node.startOffset, node, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal companion object { | ||
const val NAME_ID = "debug-print" | ||
} | ||
} |
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
146 changes: 146 additions & 0 deletions
146
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/DebugPrintRuleWarnTest.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,146 @@ | ||
package org.cqfn.diktat.ruleset.chapter3 | ||
|
||
import org.cqfn.diktat.ruleset.constants.Warnings | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.chapter3.DebugPrintRule | ||
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 DebugPrintRuleWarnTest : LintTestBase(::DebugPrintRule) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:${DebugPrintRule.NAME_ID}" | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `call of print`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| print("test print") | ||
|} | ||
""".trimMargin(), | ||
LintError(2, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found print()", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `call of println`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| println("test println") | ||
|} | ||
""".trimMargin(), | ||
LintError(2, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found println()", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `call of println without arguments`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| println() | ||
|} | ||
""".trimMargin(), | ||
LintError(2, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found println()", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `custom method print by argument list`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| print("test custom args", 123) | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `custom method print with lambda as last parameter`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| print("test custom method") { | ||
| foo("") | ||
| } | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `custom method print in another object`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| foo.print("test custom method") | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `call of console`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| console.error("1") | ||
| console.info("1", "2") | ||
| console.log("1", "2", "3") | ||
| console.warn("1", "2", "3", "4") | ||
|} | ||
""".trimMargin(), | ||
LintError(2, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found console.error()", false), | ||
LintError(3, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found console.info()", false), | ||
LintError(4, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found console.log()", false), | ||
LintError(5, 5, ruleId, "${Warnings.DEBUG_PRINT.warnText()} found console.warn()", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `custom method console with lambda as last parameter`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| console.error("1") { | ||
| foo("") | ||
| } | ||
| console.info("1", "2") { | ||
| foo("") | ||
| } | ||
| console.log("1", "2", "3") { | ||
| foo("") | ||
| } | ||
| console.warn("1", "2", "3", "4") { | ||
| foo("") | ||
| } | ||
|} | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.DEBUG_PRINT) | ||
fun `call parameter from console`() { | ||
lintMethod( | ||
""" | ||
|fun test() { | ||
| val foo = console.size | ||
|} | ||
""".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
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