From bceedd9405ac55f1e54b24e6f5bd0268bd4835a1 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 26 Jul 2021 13:10:46 +0300 Subject: [PATCH 1/3] Fix NPE with commented import in kotlin scripts ### What's done: * Changed logic * Added tests --- .../rules/chapter2/kdoc/CommentsFormatting.kt | 13 +++++++------ .../diktat/ruleset/utils/indentation/Checkers.kt | 1 - .../cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 15 ++++++++++++--- .../kotlin/kotlin-library-expected.gradle.kts | 7 +++++++ .../src/main/kotlin/kotlin-library.gradle.kts | 6 ++++++ 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/kdoc/CommentsFormatting.kt index f8251255d7..e8fd022ad8 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter2/kdoc/CommentsFormatting.kt @@ -105,7 +105,7 @@ class CommentsFormatting(configRules: List) : DiktatRule( // Checking if comment is inside a code block like fun{} // Not checking last comment as well if (isFirstComment(node)) { - if (node.isBlockOrClassBody()) { + if (node.isChildOfBlockOrClassBody()) { // Just check white spaces before comment checkFirstCommentSpaces(node) } @@ -166,7 +166,7 @@ class CommentsFormatting(configRules: List) : DiktatRule( private fun checkCommentsInCodeBlocks(node: ASTNode) { if (isFirstComment(node)) { - if (node.isBlockOrClassBody()) { + if (node.isChildOfBlockOrClassBody()) { // Just check white spaces before comment checkFirstCommentSpaces(node) } @@ -289,7 +289,7 @@ class CommentsFormatting(configRules: List) : DiktatRule( private fun checkClassComment(node: ASTNode) { if (isFirstComment(node)) { - if (node.isBlockOrClassBody()) { + if (node.isChildOfBlockOrClassBody()) { checkFirstCommentSpaces(node) } else { checkFirstCommentSpaces(node.treeParent) @@ -317,10 +317,11 @@ class CommentsFormatting(configRules: List) : DiktatRule( } } - private fun isFirstComment(node: ASTNode) = if (node.isBlockOrClassBody()) { + private fun isFirstComment(node: ASTNode) = if (node.isChildOfBlockOrClassBody()) { // In case when comment is inside of a function or class if (node.treePrev.isWhiteSpace()) { - node.treePrev.treePrev.elementType == LBRACE + // in some cases (e.g. kts files) BLOCK doesn't have a leading brace + node.treePrev?.treePrev?.elementType == LBRACE } else { node.treePrev == null || node.treePrev.elementType == LBRACE // null is handled for functional literal } @@ -335,7 +336,7 @@ class CommentsFormatting(configRules: List) : DiktatRule( node.treeParent.getAllChildrenWithType(node.elementType).first() == node } - private fun ASTNode.isBlockOrClassBody(): Boolean = treeParent.elementType == BLOCK || treeParent.elementType == CLASS_BODY + private fun ASTNode.isChildOfBlockOrClassBody(): Boolean = treeParent.elementType == BLOCK || treeParent.elementType == CLASS_BODY /** * [RuleConfiguration] for [CommentsFormatting] rule diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt index f1b1bc328e..0708cd2adf 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt @@ -90,7 +90,6 @@ internal class ValueParameterListChecker(configuration: IndentationConfig) : Cus it.node.elementType.run { this == VALUE_ARGUMENT || this == VALUE_PARAMETER } } - @Suppress("ANNOTATION_NEW_LINE") // https://github.com/cqfn/diKTat/issues/609 override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { if (isCheckNeeded(whiteSpace)) { val parameterList = whiteSpace.parent.node 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 fafb72d0b9..5c71be823f 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 @@ -32,6 +32,7 @@ import java.time.LocalDate import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.createTempFile import kotlinx.serialization.encodeToString +import org.cqfn.diktat.ruleset.rules.chapter2.comments.CommentsRule typealias RuleToConfig = Map> @@ -197,9 +198,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", @Tag("DiktatRuleSetProvider") fun `smoke test with kts files`() { overrideRulesConfig( - listOf( - HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE // because build.gradle.kts doesn't need extra comments, and this rule can be manually disabled if needed - ), + emptyList(), mapOf( Warnings.WRONG_INDENTATION.name to mapOf( "newlineAtEnd" to "false", @@ -219,6 +218,16 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", tmpTestFile.delete() } + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test with gradle script plugin`() { + fixAndCompare("kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") + Assertions.assertEquals( + LintError(2, 1, "$DIKTAT_RULE_SET_ID:comments", "[COMMENTED_OUT_CODE] you should not comment out code, use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), + unfixedLintErrors.single() + ) + } + @Test @Tag("DiktatRuleSetProvider") fun `smoke test with kts files #2`() { diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts new file mode 100644 index 0000000000..12689cc10c --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library-expected.gradle.kts @@ -0,0 +1,7 @@ +import org.gradle.kotlin.dsl.plugins +// import org.jetbrains.kotlin.gradle.dsl.jvm + +plugins { + kotlin("jvm") +} + diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts new file mode 100644 index 0000000000..5586a354c9 --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/kotlin-library.gradle.kts @@ -0,0 +1,6 @@ +import org.gradle.kotlin.dsl.plugins +//import org.jetbrains.kotlin.gradle.dsl.jvm + +plugins { + kotlin("jvm") +} \ No newline at end of file From 05848733e4f774d85707dceb2ae265d291f7ecb1 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 26 Jul 2021 14:10:51 +0300 Subject: [PATCH 2/3] Update build_and_test.yml --- .github/workflows/build_and_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index d4b89cb00d..c8d304212f 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -6,7 +6,7 @@ on: jobs: build_and_test_with_code_coverage: name: Build, test and upload code coverage - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2.3.4 From c26283bb6d2f4113aa27bcb6df0a356fb6a89d49 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 26 Jul 2021 14:14:48 +0300 Subject: [PATCH 3/3] Fix NPE with commented import in kotlin scripts ### What's done: * Code style --- .../kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 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 5c71be823f..7c355f9458 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 @@ -32,7 +32,6 @@ import java.time.LocalDate import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.createTempFile import kotlinx.serialization.encodeToString -import org.cqfn.diktat.ruleset.rules.chapter2.comments.CommentsRule typealias RuleToConfig = Map> @@ -223,7 +222,8 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", fun `smoke test with gradle script plugin`() { fixAndCompare("kotlin-library-expected.gradle.kts", "kotlin-library.gradle.kts") Assertions.assertEquals( - LintError(2, 1, "$DIKTAT_RULE_SET_ID:comments", "[COMMENTED_OUT_CODE] you should not comment out code, use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), + LintError(2, 1, "$DIKTAT_RULE_SET_ID:comments", "[COMMENTED_OUT_CODE] you should not comment out code, " + + "use VCS to save it in history and delete this block: import org.jetbrains.kotlin.gradle.dsl.jvm", false), unfixedLintErrors.single() ) }