From f2747f3cb4d9c0c778ee0d54a0ed26122f2cb512 Mon Sep 17 00:00:00 2001 From: Stanley Shyiko Date: Tue, 20 Mar 2018 17:37:47 -0700 Subject: [PATCH] Relaxed no-line-break-after-else rule to allow multi-line if/else without curly braces --- CHANGELOG.md | 3 ++- .../ruleset/standard/NoLineBreakAfterElseRule.kt | 13 ++++++++----- .../standard/NoLineBreakAfterElseRuleTest.kt | 4 +--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df6ac345b5..8174116fbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). - `+`, `-`, `*`, `/`, `%`, `&&`, `||` wrapping ([#168](https://github.com/shyiko/ktlint/issues/168)). ### Changed -- `comma-spacing` rule to be more strict ([#173](https://github.com/shyiko/ktlint/issues/173)). +- `comma-spacing` rule to be more strict ([#173](https://github.com/shyiko/ktlint/issues/173)). +- `no-line-break-after-else` rule to allow multi-line `if/else` without curly braces. ## [0.19.0] - 2018-03-04 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 52667915cd..d28ba5ebcf 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 @@ -4,6 +4,7 @@ import com.github.shyiko.ktlint.core.Rule 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.com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.lexer.KtTokens class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") { @@ -14,11 +15,13 @@ class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") { 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(" ") + node.textContains('\n')) { + if (PsiTreeUtil.prevLeaf(node, true)?.node?.elementType == KtTokens.ELSE_KEYWORD && + PsiTreeUtil.nextLeaf(node, true)?.node?.elementType.let { it == KtTokens.IF_KEYWORD || it == KtTokens.LBRACE }) { + emit(node.startOffset + 1, "Unexpected line break after \"else\"", true) + if (autoCorrect) { + (node as LeafPsiElement).rawReplaceWithText(" ") + } } } } 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 1e1ef73bdb..8b645ecffe 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 @@ -111,9 +111,7 @@ class NoLineBreakAfterElseRuleTest { doAnotherThing() } """.trimIndent() - )).isEqualTo(listOf( - LintError(5, 1, "no-line-break-after-else", "Unexpected line break after \"else\"") - )) + )).isEmpty() } @Test