forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new Rule: No line break before assignment
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
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
.../main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakBeforeAssignmentRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ") | ||
} | ||
} | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...t/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakBeforeAssignmentRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |