Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new Rule: No line break before assignment #106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -28,6 +28,7 @@ class StandardRuleSetProvider : RuleSetProvider {
SpacingAroundCurlyRule(),
SpacingAroundKeywordRule(),
SpacingAroundOperatorsRule(),
StringTemplateRule()
StringTemplateRule(),
NoLineBreakBeforeAssignmentRule()
)
}
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()
)
}
}