Repository: analysis-dev/diktat (kotlin)

Issue:
Internal error on gradle script
## Describe the bug
```kotlin
import org.cqfn.save.buildutils.configurePublishing
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.project
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
//import org.jetbrains.kotlin.gradle.dsl.jvm

plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization")
}

kotlin {
    jvm {
        compilations.all {
            kotlinOptions {
                jvmTarget = "11"
                freeCompilerArgs = freeCompilerArgs + "-Xjvm-default=all"
            }
        }
    }
    val nativeTargets = listOf(linuxX64(), mingwX64(), macosX64())

    /**
     * Common structure for MPP libraries:
     * common
     * |
     * nonJs
     * / \
     * native JVM
     * / | \
     * linux mingw macos
     */
    sourceSets {
        all {
            languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
            languageSettings.useExperimentalAnnotation("okio.ExperimentalFileSystem")
        }
        val commonMain by getting
        val commonTest by getting
        val commonNonJsTest by creating {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
//                implementation("com.squareup.okio:okio-fakefilesystem-multiplatform:${Versions.okio}")
            }
        }
        val jvmTest by getting {
            dependsOn(commonNonJsTest)
            dependencies {
                implementation(kotlin("test-junit5"))
                implementation("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
            }
        }
        val nativeMain by creating {
            dependsOn(commonMain)
        }
        val nativeTest by creating {
            dependsOn(commonTest)
        }
        nativeTargets.forEach {
            getByName("${it.name}Main").dependsOn(nativeMain)
        }
        nativeTargets.forEach {
            getByName("${it.name}Test").dependsOn(nativeTest)
            getByName("${it.name}Test").dependsOn(commonNonJsTest)
        }
    }
}

configurePublishing()

tasks.withType<KotlinJvmTest> {
    useJUnitPlatform()
}
```
produces the following:
```
[ERROR] 2021-07-22 16:04:48 Internal error has occurred in rule [kdoc-comments-codeblocks-formatting]. Please make an issue on this bug at https://github.com/cqfn/diKTat/. 
                       As a workaround you can disable these inspections in yml config: <[COMMENT_WHITE_SPACE, FIRST_COMMENT_NO_BLANK_LINE, IF_ELSE_COMMENTS, WRONG_NEWLINES_AROUND_KDOC]>.
                       Root cause of the problem is in [...\save\buildSrc\src\main\kotlin\kotlin-library.gradle.kts] file.
java.lang.NullPointerException
	at org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting.isFirstComment(CommentsFormatting.kt:323)
	at org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting.basicCommentsChecks(CommentsFormatting.kt:107)
	at org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting.handleEolAndBlockComments(CommentsFormatting.kt:97)
	at org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting.logic(CommentsFormatting.kt:67)
	at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:50)
```

## Environment information
* diktat version: 1.0.0-rc.2

Pull request description:
### What's done:
* Changed logic
* Added tests
* Changed runner for build_and_test workflow

This pull request closes #994

Patch:
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<RulesConfig>) : 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<RulesConfig>) : 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<RulesConfig>) : 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<RulesConfig>) : 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<RulesConfig>) : 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


Test patch:
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
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..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
@@ -197,9 +197,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 +217,17 @@ 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