Skip to content

Commit

Permalink
new Rule: No line break before assignment
Browse files Browse the repository at this point in the history
Line break is allowed only after assignment (eg. equals sign)
Auto formatting is supported via simplistic approach by putting
same line break with same indent from left side to right side of assignment

Closes pinterest#105
  • Loading branch information
MyDogTom committed Nov 22, 2017
1 parent 28a5376 commit 8e70819
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.shyiko.ktlint.ruleset.standard

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.lexer.KtTokens

class NoLineBreakBeforeAssignmentRule : Rule("no-line-break-before-assignment") {

override fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
if (node.elementType == KtTokens.EQ) {
val prevElement = node.treePrev?.psi
if (prevElement is PsiWhiteSpace && prevElement.text.contains("\n")) {
emit(node.startOffset, "Line break before assignment is not allowed", true)
if (autoCorrect) {
(node.treeNext?.psi as LeafPsiElement).replaceWithText(prevElement.text)
(prevElement as LeafPsiElement).replaceWithText(" ")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.LintError
import com.github.shyiko.ktlint.test.format
import com.github.shyiko.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test

const val ruleId = "no-line-break-before-assignment"

class NoLineBreakBeforeAssignmentRuleTest {
@Test
fun testAllPartsOnSameLineIsValid() {
assertThat(NoLineBreakBeforeAssignmentRule().lint(
"""
val valA = ""
""".trimIndent()
)).isEmpty()
}

@Test
fun testLineBreakAfterAssignmentIsValid() {
assertThat(NoLineBreakBeforeAssignmentRule().lint(
"""
val valA =
""
""".trimIndent()
)).isEmpty()
}

@Test
fun testLineBreakBeforeAssignmentIsViolation() {
assertThat(NoLineBreakBeforeAssignmentRule().lint(
"""
val valA
= ""
""".trimIndent()
)).isEqualTo(listOf(
LintError(2, 7, ruleId, "Line break before assignment is not allowed")
))
}

@Test
fun testViolationInFunction() {
assertThat(NoLineBreakBeforeAssignmentRule().lint(
"""
fun funA()
= ""
""".trimIndent()
)).isEqualTo(listOf(
LintError(2, 7, ruleId, "Line break before assignment is not allowed")
))
}

@Test
fun testFixViolationByRemovingLineBreakFromLeftAndPutItOnRightSide() {
assertThat(NoLineBreakBeforeAssignmentRule().format(
"""
fun funA()
= ""
""".trimIndent()
)).isEqualTo(
"""
fun funA() =
""
""".trimIndent()
)
}
}

0 comments on commit 8e70819

Please sign in to comment.