Repository: analysis-dev/diktat (kotlin)

Issue:
Diktat codestyle should prohibit empty line before the closing quote
```
fun foo() {
    bar()

}
```

the following code should be prohibited

Pull request:
## Blank line rule fixed

This pull request closes #1054

## Actions checklist
* [ ] Added a check if there are blank lines at the end of a block.
* [ ] Added tests on checks
* [ ] Added tests on fixers

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt
index d638f25096..6e363f52b0 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/BlankLinesRule.kt
@@ -3,11 +3,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files
 import org.cqfn.diktat.common.config.rules.RulesConfig
 import org.cqfn.diktat.ruleset.constants.Warnings.TOO_MANY_BLANK_LINES
 import org.cqfn.diktat.ruleset.rules.DiktatRule
-import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType
-import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
-import org.cqfn.diktat.ruleset.utils.leaveExactlyNumNewLines
-import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine
-import org.cqfn.diktat.ruleset.utils.numNewLines
+import org.cqfn.diktat.ruleset.utils.*
 
 import com.pinterest.ktlint.core.ast.ElementType.BLOCK
 import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
@@ -66,7 +62,8 @@ class BlankLinesRule(configRules: List<RulesConfig>) : DiktatRule(
 
     private fun handleTooManyBlankLines(node: ASTNode) {
         TOO_MANY_BLANK_LINES.warnAndFix(configRules, emitWarn, isFixMode, "do not use more than two consecutive blank lines", node.startOffset, node) {
-            if (node.treeParent.elementType != FILE && node.treeParent.getFirstChildWithType(WHITE_SPACE) == node) {
+            if (node.treeParent.elementType != FILE && (node.treeParent.getFirstChildWithType(WHITE_SPACE) == node ||
+                    node.treeParent.getAllChildrenWithType(WHITE_SPACE).last() == node)) {
                 node.leaveExactlyNumNewLines(1)
             } else {
                 node.leaveExactlyNumNewLines(2)


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesFixTest.kt
index bc1f3a4391..7855629c65 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesFixTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesFixTest.kt
@@ -19,4 +19,10 @@ class BlankLinesFixTest : FixTestBase("test/paragraph3/blank_lines", ::BlankLine
     fun `should remove blank lines in the beginning and at the end of code block`() {
         fixAndCompare("CodeBlockWithBlankLinesExpected.kt", "CodeBlockWithBlankLinesTest.kt")
     }
+
+    @Test
+    @Tag(WarningNames.TOO_MANY_BLANK_LINES)
+    fun `should remove empty line before the closing quote`() {
+        fixAndCompare("RedundantBlankLinesAtTheEndOfBlockExpected.kt", "RedundantBlankLinesAtTheEndOfBlockTest.kt")
+    }
 }
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt
index ff4ddae24e..fb4d513566 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/BlankLinesWarnTest.kt
@@ -65,7 +65,7 @@ class BlankLinesWarnTest : LintTestBase(::BlankLinesRule) {
 
     @Test
     @Tag(WarningNames.TOO_MANY_BLANK_LINES)
-    fun `should prohibit blank lines in the beginning or at the end of block`() {
+    fun `should prohibit blank lines in the beginning and at the end of code block`() {
         lintMethod(
             """
                     |class Example {
@@ -82,4 +82,22 @@ class BlankLinesWarnTest : LintTestBase(::BlankLinesRule) {
             LintError(5, 14, ruleId, blankLinesInBlockWarn(false), true)
         )
     }
+
+    @Test
+    @Tag(WarningNames.TOO_MANY_BLANK_LINES)
+    fun `should prohibit empty line before the closing quote`() {
+        lintMethod(
+            """
+                    |class Example {
+                    |    fun foo() {
+                    |        bar()
+                    |        
+                    |    }
+                    |    
+                    |}
+                """.trimMargin(),
+            LintError(3, 14, ruleId, blankLinesInBlockWarn(false), true),
+            LintError(5, 6, ruleId, blankLinesInBlockWarn(false), true)
+        )
+    }
 }
diff --git a/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockExpected.kt
new file mode 100644
index 0000000000..1f303bb16b
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockExpected.kt
@@ -0,0 +1,12 @@
+package test.paragraph3.blank_lines
+
+class Example {
+    fun foo() {
+        bar()
+    }
+
+    fun bar() {
+        println()
+        println()
+    }
+}
\ No newline at end of file
diff --git a/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockTest.kt b/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockTest.kt
new file mode 100644
index 0000000000..73726d44dc
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/paragraph3/blank_lines/RedundantBlankLinesAtTheEndOfBlockTest.kt
@@ -0,0 +1,19 @@
+package test.paragraph3.blank_lines
+
+class Example {
+    fun foo() {
+        bar()
+
+
+    }
+
+    fun bar() {
+        println()
+        println()
+
+
+
+    }
+
+
+}
\ No newline at end of file