From 264ea7fc088ce90e32931915a1c4dfbc41dd21b2 Mon Sep 17 00:00:00 2001 From: Stanley Shyiko Date: Tue, 12 Dec 2017 12:37:43 -0800 Subject: [PATCH] Simplified no-line-break-after-else logic (#125) --- .../standard/NoLineBreakAfterElseRule.kt | 34 ++++++++----------- .../standard/NoLineBreakAfterElseRuleTest.kt | 7 ++-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRule.kt index 3b904d4034..52667915cd 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRule.kt @@ -1,31 +1,25 @@ package com.github.shyiko.ktlint.ruleset.standard import com.github.shyiko.ktlint.core.Rule -import org.jetbrains.kotlin.KtNodeTypes 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 -import org.jetbrains.kotlin.lexer.KtKeywordToken +import org.jetbrains.kotlin.lexer.KtTokens -class NoLineBreakAfterElseRule : Rule(RULE_ID) { - override fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { - if (node is PsiWhiteSpace) { - if ( - node.nextSibling?.node?.elementType == KtNodeTypes.ELSE - && node.prevSibling?.node?.elementType is KtKeywordToken - && node.prevSibling.text == "else" - && node.getText().contains("\n")) { - emit(node.startOffset + 1, - "Unexpected line break after \"else\"", - true) - if (autoCorrect) { - (node as LeafPsiElement).rawReplaceWithText(" ") - } +class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") { + + override fun visit( + node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + ) { + if (node is PsiWhiteSpace && + node.textContains('\n') && + node.prevSibling?.node?.elementType == KtTokens.ELSE_KEYWORD) { + emit(node.startOffset + 1, "Unexpected line break after \"else\"", true) + if (autoCorrect) { + (node as LeafPsiElement).rawReplaceWithText(" ") } } } - - companion object { - const val RULE_ID = "no-line-break-after-else" - } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRuleTest.kt index 6bb70bf87f..1e1ef73bdb 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRuleTest.kt @@ -7,6 +7,7 @@ import org.assertj.core.api.Assertions import org.testng.annotations.Test class NoLineBreakAfterElseRuleTest { + @Test fun testViolationForLineBreakBetweenElseAndIf() { Assertions.assertThat(NoLineBreakAfterElseRule().lint( @@ -21,7 +22,7 @@ class NoLineBreakAfterElseRuleTest { } """.trimIndent() )).isEqualTo(listOf( - LintError(5, 1, NoLineBreakAfterElseRule.RULE_ID, "Unexpected line break after \"else\"") + LintError(5, 1, "no-line-break-after-else", "Unexpected line break after \"else\"") )) } @@ -95,7 +96,7 @@ class NoLineBreakAfterElseRuleTest { } """.trimIndent() )).isEqualTo(listOf( - LintError(5, 1, NoLineBreakAfterElseRule.RULE_ID, "Unexpected line break after \"else\"") + LintError(5, 1, "no-line-break-after-else", "Unexpected line break after \"else\"") )) } @@ -111,7 +112,7 @@ class NoLineBreakAfterElseRuleTest { } """.trimIndent() )).isEqualTo(listOf( - LintError(5, 1, NoLineBreakAfterElseRule.RULE_ID, "Unexpected line break after \"else\"") + LintError(5, 1, "no-line-break-after-else", "Unexpected line break after \"else\"") )) }