From 44418ef82342a1533f234e6a2b743cfa5076dddd Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Mon, 8 Mar 2021 22:14:28 +0100 Subject: [PATCH] Fix NPE in NoConsecutiveBlankLinesRule. --- .../standard/NoConsecutiveBlankLinesRule.kt | 6 ++++-- .../standard/NoConsecutiveBlankLinesRuleTest.kt | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt index 0ac2c2c9fe..ce95cb49bd 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt @@ -9,14 +9,16 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement -class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") { +public class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") { override fun visit( node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit ) { - if (node is PsiWhiteSpace) { + if (node is PsiWhiteSpace && + node.prevSibling != null + ) { val text = node.getText() val lfcount = text.count { it == '\n' } if (lfcount < 2) { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt index fc894e69bd..7f105b852e 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt @@ -207,4 +207,20 @@ class NoConsecutiveBlankLinesRuleTest { ) ).isEmpty() } + + @Test + fun `should not raise NPE on linting Kotlin script file`() { + assertThat( + NoConsecutiveBlankLinesRule().lint( + """ + import java.net.URI + + plugins { + `java-library` + } + """.trimIndent(), + script = true + ) + ).isEmpty() + } }