diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt index c8624f2fd1..1d1b1d711e 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt @@ -15,7 +15,8 @@ class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") { if (split.size > 3 || split.size == 3 && PsiTreeUtil.nextLeaf(node) == null /* eof */) { emit(node.startOffset + split[0].length + split[1].length + 2, "Needless blank line(s)", true) if (autoCorrect) { - (node as LeafPsiElement).rawReplaceWithText("${split.first()}\n\n${split.last()}") + (node as LeafPsiElement) + .rawReplaceWithText("${split.first()}\n${if (split.size > 3) "\n" else ""}${split.last()}") } } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt index cc65e68c23..5054d99284 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt @@ -107,4 +107,23 @@ class NoConsecutiveBlankLinesRuleTest { """ ) } + + @Test + fun testFormatAtTheEndOfFile() { + assertThat(NoConsecutiveBlankLinesRule().format( + """ + fun main() { + } + + + """, + script = true + )).isEqualTo( + """ + fun main() { + } + + """ + ) + } }