From c0630b11231148a083ad1385975e7a888bca93d0 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 15:52:18 +0300 Subject: [PATCH 01/14] Copyright as code ### What's done: Fixed bug, added comment and test --- .../rules/chapter2/comments/CommentsRule.kt | 16 +++++++++++++-- .../chapter2/comments/CommentedCodeTest.kt | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index f169fed02a..913cb1373d 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -6,6 +6,8 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType import com.pinterest.ktlint.core.Rule +import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.FILE @@ -52,6 +54,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" * with '// ' with whitespace, while automatic commenting in, e.g., IDEA creates slashes in the beginning of the line * */ + @Suppress("UnsafeCallOnNullableType") private fun checkCommentedCode(node: ASTNode) { val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node) val blockCommentsOffsetToText = node @@ -70,8 +73,17 @@ class CommentsRule(private val configRules: List) : Rule("comments" offset to ktPsiFactory.createImportDirective(ImportPath.fromString(text.substringAfter("$importKeyword "))).node text.contains(packageKeyword) -> offset to ktPsiFactory.createPackageDirective(FqName(text.substringAfter("$packageKeyword "))).node - else -> - offset to ktPsiFactory.createBlockCodeFragment(text, null).node + else -> { + var newNode = ktPsiFactory.createBlockCodeFragment(text, null).node + val isPossibleError = newNode.findChildByType(BLOCK)!!.firstChildNode.elementType == BINARY_EXPRESSION + if (isPossibleError) { + val nodeInsideClass = ktPsiFactory.createBlockCodeFragment("class A {$text}", null).node + if (nodeInsideClass.findAllNodesWithSpecificType(TokenType.ERROR_ELEMENT).isNotEmpty()) + newNode = nodeInsideClass + } + offset to newNode + // fixme A check is performed for the presence of a BINARY_EXPRESSION as first child, because the one-line copyright is perceived by the parser as a BINARY EXPRESSION + } } } .filter { (_, parsedNode) -> diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index 05517afc0c..1ec589c041 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -293,4 +293,24 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { | */ """.trimMargin()) } + + @Test + @Tag(WarningNames.COMMENTED_OUT_CODE) + fun `should not trigger on Copyright and another comment`() { + lintMethod( + """ + /* + Copyright (c) Your Company Name Here. 2010-2021 + */ + + package org.cqfn.diktat + + /* + x = 2 + 4 + 1 + */ + // x = 2+4 + + // if true make this + """.trimMargin()) + } } From 16255f004f318c3db9fdad69469c060ab2559b98 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 16:02:16 +0300 Subject: [PATCH 02/14] Copyright as code ### What's done: Fixed according our code style --- .../ruleset/rules/chapter2/comments/CommentsRule.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 913cb1373d..26d0a88fd3 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -54,7 +54,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" * with '// ' with whitespace, while automatic commenting in, e.g., IDEA creates slashes in the beginning of the line * */ - @Suppress("UnsafeCallOnNullableType") + @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkCommentedCode(node: ASTNode) { val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node) val blockCommentsOffsetToText = node @@ -76,13 +76,15 @@ class CommentsRule(private val configRules: List) : Rule("comments" else -> { var newNode = ktPsiFactory.createBlockCodeFragment(text, null).node val isPossibleError = newNode.findChildByType(BLOCK)!!.firstChildNode.elementType == BINARY_EXPRESSION + // A check is performed for the presence of a BINARY_EXPRESSION as first child, + // because the one-line copyright is perceived by the parser as a BINARY EXPRESSION if (isPossibleError) { val nodeInsideClass = ktPsiFactory.createBlockCodeFragment("class A {$text}", null).node - if (nodeInsideClass.findAllNodesWithSpecificType(TokenType.ERROR_ELEMENT).isNotEmpty()) + if (nodeInsideClass.findAllNodesWithSpecificType(TokenType.ERROR_ELEMENT).isNotEmpty()) { newNode = nodeInsideClass + } } offset to newNode - // fixme A check is performed for the presence of a BINARY_EXPRESSION as first child, because the one-line copyright is perceived by the parser as a BINARY EXPRESSION } } } From 09bd1ea5d028bfd9fc9129b460824f96475162a1 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 17:51:58 +0300 Subject: [PATCH 03/14] Copyright as code ### What's done: Fixed after review --- .../rules/chapter2/comments/CommentsRule.kt | 21 +++++++------------ .../chapter2/comments/CommentedCodeTest.kt | 19 +++++++---------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 26d0a88fd3..caafd8db2d 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -6,8 +6,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION -import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.FILE @@ -67,24 +65,18 @@ class CommentsRule(private val configRules: List) : Rule("comments" val block = if (blockLines.isNotEmpty()) listOf(blockLines.joinToString("\n")) else emptyList() (singleLines + block).map { offset to it } } - .map { (offset, text) -> + .mapNotNull { (offset, text) -> when { text.contains(importKeyword) -> offset to ktPsiFactory.createImportDirective(ImportPath.fromString(text.substringAfter("$importKeyword "))).node text.contains(packageKeyword) -> offset to ktPsiFactory.createPackageDirective(FqName(text.substringAfter("$packageKeyword "))).node else -> { - var newNode = ktPsiFactory.createBlockCodeFragment(text, null).node - val isPossibleError = newNode.findChildByType(BLOCK)!!.firstChildNode.elementType == BINARY_EXPRESSION - // A check is performed for the presence of a BINARY_EXPRESSION as first child, - // because the one-line copyright is perceived by the parser as a BINARY EXPRESSION - if (isPossibleError) { - val nodeInsideClass = ktPsiFactory.createBlockCodeFragment("class A {$text}", null).node - if (nodeInsideClass.findAllNodesWithSpecificType(TokenType.ERROR_ELEMENT).isNotEmpty()) { - newNode = nodeInsideClass - } + if (text.contains(requirePartOfCode)) { + offset to ktPsiFactory.createBlockCodeFragment(text, null).node + } else { + null } - offset to newNode } } } @@ -153,7 +145,8 @@ class CommentsRule(private val configRules: List) : Rule("comments" private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+;*$""".toRegex() private val functionRegex = """^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${'$'}""".toRegex() private val rightBraceRegex = """^\s*}$""".toRegex() - private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex) + private val requirePartOfCode = """val|var|=|[{}]""".toRegex() + private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex, requirePartOfCode) private val eolCommentStart = """// \S""".toRegex() } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index 1ec589c041..ab59734010 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -55,9 +55,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { | */ | return 0 |} - """.trimMargin(), - LintError(4, 5, ruleId, "${COMMENTED_OUT_CODE.warnText()} println(a + 42)", false) - ) + """.trimMargin()) } @Test @@ -72,9 +70,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { |// println("This is a test string") | return 0 |} - """.trimMargin(), - LintError(4, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} println(a + 42)", false) - ) + """.trimMargin()) } @Test @@ -179,8 +175,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { lintMethod( """ |// class Test: Exception() - """.trimMargin(), - LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} class Test: Exception()", false)) + """.trimMargin()) } @Test @@ -199,8 +194,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { lintMethod( """ |// internal sealed class Test: Exception() - """.trimMargin(), - LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} internal sealed class Test: Exception()", false)) + """.trimMargin()) } @Test @@ -311,6 +305,9 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { // x = 2+4 // if true make this - """.trimMargin()) + """.trimMargin(), + LintError(7, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false), + LintError(10, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} x = 2+4", false) + ) } } From 001685361a8d16f8172d30be6b557aeb6f4be004 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 17:53:51 +0300 Subject: [PATCH 04/14] Copyright as code ### What's done: Fixed after review --- diktat-rules/src/main/resources/diktat-analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index e304559596..e950b05411 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -131,7 +131,7 @@ - name: HEADER_MISSING_OR_WRONG_COPYRIGHT enabled: true configuration: - isCopyrightMandatory: true + isCopyrightMandatory: false copyrightText: 'Copyright (c) Your Company Name Here. 2010-2021' # Checks that header kdoc is located before package directive - name: HEADER_NOT_BEFORE_PACKAGE From 8c80ffe36ef4175e2a6c5790d744c7d38b96c447 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 18:07:43 +0300 Subject: [PATCH 05/14] Copyright as code ### What's done: Fixed test --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 8affcfdbe7..ba81e98b34 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -160,8 +160,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", fun `smoke test #2`() { fixAndCompare("Example2Expected.kt", "Example2Test.kt") unfixedLintErrors.assertEquals( - LintError(1, 1, "$DIKTAT_RULE_SET_ID:header-comment", "${HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warnText()} there are 2 declared classes and/or objects", false), - LintError(12, 26, "$DIKTAT_RULE_SET_ID:comments", "${Warnings.COMMENTED_OUT_CODE.warnText()} private class Test : RuntimeException()", false) + LintError(1, 1, "$DIKTAT_RULE_SET_ID:header-comment", "${HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warnText()} there are 2 declared classes and/or objects", false) ) } From bff77e3e18e2762d845dc9bd9256ff5c43c74d21 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 18:11:01 +0300 Subject: [PATCH 06/14] Copyright as code ### What's done: Fixed test --- .../diktat/ruleset/chapter2/comments/CommentedCodeTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index ab59734010..8487945b93 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -52,10 +52,13 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { |fun foo(a: Int): Int { | /* println(a + 42) | println("This is a test string") + | val b = a*10 | */ | return 0 |} - """.trimMargin()) + """.trimMargin(), + LintError(4, 5, ruleId, "${COMMENTED_OUT_CODE.warnText()} println(a + 42)", false) + ) } @Test From 3f0621ff082a72285cdb5707b70fa90dc0d8f9f5 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 18:36:26 +0300 Subject: [PATCH 07/14] Copyright as code ### What's done: Fixed according to our code style --- .../rules/chapter2/comments/CommentsRule.kt | 2 +- .../chapter2/comments/CommentedCodeTest.kt | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index caafd8db2d..0f8a1c7f9b 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -145,7 +145,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+;*$""".toRegex() private val functionRegex = """^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${'$'}""".toRegex() private val rightBraceRegex = """^\s*}$""".toRegex() - private val requirePartOfCode = """val|var|=|[{}]""".toRegex() + private val requirePartOfCode = """val |var|=|(\{.+?|\})""".toRegex() private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex, requirePartOfCode) private val eolCommentStart = """// \S""".toRegex() } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index 8487945b93..d986a316ba 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -57,7 +57,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { | return 0 |} """.trimMargin(), - LintError(4, 5, ruleId, "${COMMENTED_OUT_CODE.warnText()} println(a + 42)", false) + LintError(4, 5, ruleId, "${COMMENTED_OUT_CODE.warnText()} println(a + 42)", false) ) } @@ -308,9 +308,19 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { // x = 2+4 // if true make this + + /* + class A { + + fun foo() + + } + + */ """.trimMargin(), LintError(7, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false), - LintError(10, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} x = 2+4", false) - ) + LintError(10, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} x = 2+4", false), + LintError(14, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false) + ) } } From 52f13752ff81af7932c7747c1e87fb52f295db81 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 19:08:10 +0300 Subject: [PATCH 08/14] Copyright as code ### What's done: Fixed regex --- .../cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 0f8a1c7f9b..364446a42f 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -145,7 +145,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+;*$""".toRegex() private val functionRegex = """^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${'$'}""".toRegex() private val rightBraceRegex = """^\s*}$""".toRegex() - private val requirePartOfCode = """val |var|=|(\{.+?|\})""".toRegex() + private val requirePartOfCode = """val |var |=|(\{((.|\n)*)\})""".toRegex() private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex, requirePartOfCode) private val eolCommentStart = """// \S""".toRegex() } From 1ceeeafb6f8993513b2b061f550738fe86796cb2 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 19:32:25 +0300 Subject: [PATCH 09/14] Copyright as code ### What's done: Fixed this bug in another files --- .../ruleset/rules/chapter6/classes/CompactInitialization.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt index 4c64fa4f95..bfceac63bd 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt @@ -81,7 +81,7 @@ class CompactInitialization(private val configRules: List) : Rule(" } } - @Suppress("UnsafeCallOnNullableType", "NestedBlockDepth") + @Suppress("UnsafeCallOnNullableType", "NestedBlockDepth", "COMMENTED_OUT_CODE") private fun moveAssignmentIntoApply(property: KtProperty, assignment: KtBinaryExpression) { // get apply expression or create empty; convert `apply(::foo)` to `apply { foo(this) }` if necessary getOrCreateApplyBlock(property).let(::convertValueParametersToLambdaArgument) From a331573d90459708caa95d882761bf029233c18c Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 19:41:11 +0300 Subject: [PATCH 10/14] Copyright as code ### What's done: Fixed according to our code style --- .../ruleset/rules/chapter2/comments/CommentsRule.kt | 10 ++++------ .../rules/chapter6/classes/CompactInitialization.kt | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 364446a42f..82330b1826 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -71,12 +71,10 @@ class CommentsRule(private val configRules: List) : Rule("comments" offset to ktPsiFactory.createImportDirective(ImportPath.fromString(text.substringAfter("$importKeyword "))).node text.contains(packageKeyword) -> offset to ktPsiFactory.createPackageDirective(FqName(text.substringAfter("$packageKeyword "))).node - else -> { - if (text.contains(requirePartOfCode)) { - offset to ktPsiFactory.createBlockCodeFragment(text, null).node - } else { - null - } + else -> if (text.contains(requirePartOfCode)) { + offset to ktPsiFactory.createBlockCodeFragment(text, null).node + } else { + null } } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt index bfceac63bd..ce1ec0b205 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt @@ -52,7 +52,7 @@ class CompactInitialization(private val configRules: List) : Rule(" * Check property's initializer: if it is a method call, we find all consecutive statements that are this property's * fields accessors and wrap them in an `apply` function. */ - @Suppress("UnsafeCallOnNullableType") + @Suppress("UnsafeCallOnNullableType", "COMMENTED_OUT_CODE") private fun handleProperty(property: KtProperty) { property.run { val propertyName = name From 08fc9a4a95ae5e246c5e33469565f5866af473f4 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 22:00:17 +0300 Subject: [PATCH 11/14] Copyright as code ### What's done: Fixed according to our code style --- .../rules/chapter2/comments/CommentsRule.kt | 19 +++++++++++------ .../chapter2/comments/CommentedCodeTest.kt | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 82330b1826..8b6daff1c7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -54,16 +54,21 @@ class CommentsRule(private val configRules: List) : Rule("comments" */ @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkCommentedCode(node: ASTNode) { - val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node) + val errorNodesWithText = mutableListOf>() + val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node, errorNodesWithText) val blockCommentsOffsetToText = node .findAllNodesWithSpecificType(BLOCK_COMMENT) - .map { it.startOffset to it.text.trim().removeSurrounding("/*", "*/") } - + .map { + errorNodesWithText.add(it to it.text.trim().removeSurrounding("/*", "*/")) + it.startOffset to it.text.trim().removeSurrounding("/*", "*/") + } (eolCommentsOffsetToText + blockCommentsOffsetToText) .flatMap { (offset, text) -> val (singleLines, blockLines) = text.lines().partition { it.contains(importOrPackage) } val block = if (blockLines.isNotEmpty()) listOf(blockLines.joinToString("\n")) else emptyList() - (singleLines + block).map { offset to it } + (singleLines + block).map { + offset to it + } } .mapNotNull { (offset, text) -> when { @@ -84,7 +89,8 @@ class CommentsRule(private val configRules: List) : Rule("comments" .isEmpty() } .forEach { (offset, parsedNode) -> - COMMENTED_OUT_CODE.warn(configRules, emitWarn, isFixMode, parsedNode.text.substringBefore("\n").trim(), offset, parsedNode) + COMMENTED_OUT_CODE.warn(configRules, emitWarn, isFixMode, parsedNode.text.substringBefore("\n").trim(), offset, + errorNodesWithText.find { it.second == parsedNode.text }?.first ?: parsedNode) } } @@ -94,7 +100,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" * Splitting back into lines, if necessary, will be done outside of this method, for both text from EOL and block. * fixme: in this case offset is lost for lines which will be split once more */ - private fun getOffsetsToTextBlocksFromEolComments(node: ASTNode): List> { + private fun getOffsetsToTextBlocksFromEolComments(node: ASTNode, errorNodesWithText: MutableList>): List> { val comments = node .findAllNodesWithSpecificType(EOL_COMMENT) .filter { !it.text.contains(eolCommentStart) || isCodeAfterCommentStart(it.text) } @@ -113,6 +119,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" acc } .map { list -> + list.forEach { errorNodesWithText.add(it to it.text.removePrefix("//")) } list.first().startOffset to list.joinToString("\n") { it.text.removePrefix("//") } } } else { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index d986a316ba..c880266260 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -323,4 +323,25 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { LintError(14, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false) ) } + + @Test + @Tag(WarningNames.COMMENTED_OUT_CODE) + fun `should not trigger with suppress`() { + lintMethod( + """ + @Suppress("UnsafeCallOnNullableType", "COMMENTED_OUT_CODE") + private fun handleProperty(property: KtProperty) { + + /* + x = 1 + */ + } + + @Suppress("COMMENTED_OUT_CODE") + class A { + // val x = 10 + } + """.trimMargin() + ) + } } From e932859a1d6d010c6cd5172da27f1f562a1d88a6 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 22:23:45 +0300 Subject: [PATCH 12/14] Copyright as code ### What's done: Fixed according to our code style --- .../kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt | 2 ++ .../diktat/ruleset/rules/chapter2/comments/CommentsRule.kt | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt index 57d5e9cd55..48347021b6 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt @@ -10,6 +10,8 @@ typealias EmitType = ((offset: Int, errorMessage: String, canBeAutoCorrected: Bo typealias ListOfList = MutableList> +typealias ListOfPairs = MutableList> + /** * This class represent individual inspections of diktat code style. * A [Warnings] entry contains rule name, warning message and is used in code check. diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 8b6daff1c7..3a386132f2 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -11,6 +11,7 @@ import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.prevSibling +import org.cqfn.diktat.ruleset.constants.ListOfPairs import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.TokenType import org.jetbrains.kotlin.lexer.KtTokens @@ -54,7 +55,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" */ @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkCommentedCode(node: ASTNode) { - val errorNodesWithText = mutableListOf>() + val errorNodesWithText: ListOfPairs = mutableListOf() val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node, errorNodesWithText) val blockCommentsOffsetToText = node .findAllNodesWithSpecificType(BLOCK_COMMENT) @@ -90,7 +91,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" } .forEach { (offset, parsedNode) -> COMMENTED_OUT_CODE.warn(configRules, emitWarn, isFixMode, parsedNode.text.substringBefore("\n").trim(), offset, - errorNodesWithText.find { it.second == parsedNode.text }?.first ?: parsedNode) + errorNodesWithText.find { it.second == parsedNode.text }?.first ?: parsedNode) } } @@ -100,7 +101,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" * Splitting back into lines, if necessary, will be done outside of this method, for both text from EOL and block. * fixme: in this case offset is lost for lines which will be split once more */ - private fun getOffsetsToTextBlocksFromEolComments(node: ASTNode, errorNodesWithText: MutableList>): List> { + private fun getOffsetsToTextBlocksFromEolComments(node: ASTNode, errorNodesWithText: ListOfPairs): List> { val comments = node .findAllNodesWithSpecificType(EOL_COMMENT) .filter { !it.text.contains(eolCommentStart) || isCodeAfterCommentStart(it.text) } From 1ed3f84895184d25839aded7bb9eb0f9424bb37d Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 1 Feb 2021 22:33:35 +0300 Subject: [PATCH 13/14] Copyright as code ### What's done: Fixed according to our code style --- .../cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index 3a386132f2..c5795243b7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules.chapter2.comments import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.EmitType +import org.cqfn.diktat.ruleset.constants.ListOfPairs import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType @@ -11,7 +12,6 @@ import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.prevSibling -import org.cqfn.diktat.ruleset.constants.ListOfPairs import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.TokenType import org.jetbrains.kotlin.lexer.KtTokens From ce74f67d0a21b6a38f2e414cf68548c5c6344026 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 2 Feb 2021 11:34:50 +0300 Subject: [PATCH 14/14] Copyright as code ### What's done: Fixed after review --- .../diktat/ruleset/rules/chapter2/comments/CommentsRule.kt | 2 +- .../ruleset/rules/chapter6/classes/CompactInitialization.kt | 4 ++-- .../diktat/ruleset/chapter2/comments/CommentedCodeTest.kt | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt index c5795243b7..d3413646c0 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/comments/CommentsRule.kt @@ -152,7 +152,7 @@ class CommentsRule(private val configRules: List) : Rule("comments" private val functionRegex = """^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${'$'}""".toRegex() private val rightBraceRegex = """^\s*}$""".toRegex() private val requirePartOfCode = """val |var |=|(\{((.|\n)*)\})""".toRegex() - private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex, requirePartOfCode) + private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex) private val eolCommentStart = """// \S""".toRegex() } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt index ce1ec0b205..4c64fa4f95 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt @@ -52,7 +52,7 @@ class CompactInitialization(private val configRules: List) : Rule(" * Check property's initializer: if it is a method call, we find all consecutive statements that are this property's * fields accessors and wrap them in an `apply` function. */ - @Suppress("UnsafeCallOnNullableType", "COMMENTED_OUT_CODE") + @Suppress("UnsafeCallOnNullableType") private fun handleProperty(property: KtProperty) { property.run { val propertyName = name @@ -81,7 +81,7 @@ class CompactInitialization(private val configRules: List) : Rule(" } } - @Suppress("UnsafeCallOnNullableType", "NestedBlockDepth", "COMMENTED_OUT_CODE") + @Suppress("UnsafeCallOnNullableType", "NestedBlockDepth") private fun moveAssignmentIntoApply(property: KtProperty, assignment: KtBinaryExpression) { // get apply expression or create empty; convert `apply(::foo)` to `apply { foo(this) }` if necessary getOrCreateApplyBlock(property).let(::convertValueParametersToLambdaArgument) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt index c880266260..b35327d1bd 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt @@ -319,7 +319,6 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) { */ """.trimMargin(), LintError(7, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false), - LintError(10, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} x = 2+4", false), LintError(14, 13, ruleId, "${COMMENTED_OUT_CODE.warnText()} ", false) ) }