-
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
DebugPrintRule #1314
Merged
+261
−3
Merged
DebugPrintRule #1314
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d98c073
Added DebugPrintRule
nulls 9afa904
Added DebugPrintRule
nulls 2ad50f4
Added DebugPrintRule
nulls 5ab33b4
fixed failed tests
nulls 123143d
Merge branch 'master' into feature/debug_print#954
nulls 979e9d9
added detection of kotlin.js.console.*
nulls e0c4320
Merge branch 'feature/debug_print#954' of https://github.com/saveourt…
nulls 84867c2
increased test coverage
nulls abaffa8
checking is split to separate methods
nulls 4c35881
reverted changes in available-rules.md
nulls 761b60c
changed the message for warning
nulls 5839ee2
added description to guide
nulls 9d1b512
Merge branch 'master' into feature/debug_print#954
nulls 51e1b65
fixed id for CONVENTIONAL_RANGE
nulls 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
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
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.
May be we will have
when
byelementType
?And move logic in these branches of when to separate methods - method for
console
and method forprint
?Sorry for this note, depends on you