Skip to content

Commit

Permalink
Simplified no-line-break-after-else logic (pinterest#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Dec 12, 2017
1 parent 1811f2f commit 264ea7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.assertj.core.api.Assertions
import org.testng.annotations.Test

class NoLineBreakAfterElseRuleTest {

@Test
fun testViolationForLineBreakBetweenElseAndIf() {
Assertions.assertThat(NoLineBreakAfterElseRule().lint(
Expand All @@ -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\"")
))
}

Expand Down Expand Up @@ -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\"")
))
}

Expand All @@ -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\"")
))
}

Expand Down

0 comments on commit 264ea7f

Please # to comment.