-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding experimental rule for detecting first line blank in method block
- Loading branch information
Showing
6 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
...ain/kotlin/com/pinterest/ktlint/ruleset/experimental/NoFirstLineBlankInMethodBlockRule.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,34 @@ | ||
package com.pinterest.ktlint.ruleset.experimental | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.isPartOf | ||
import com.pinterest.ktlint.core.ast.prevLeaf | ||
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 | ||
|
||
class NoFirstLineBlankInMethodBlockRule : Rule("no-first-line-blank-in-method-block-rule") { | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
if (node is PsiWhiteSpace && node.textContains('\n') && | ||
node.prevLeaf()?.elementType == ElementType.LBRACE && node.isPartOf(FUN) | ||
) { | ||
val split = node.getText().split("\n") | ||
if (split.size > 2) { | ||
emit( | ||
node.startOffset + 1, | ||
"First line in a method block should not be empty", true | ||
) | ||
if (autoCorrect) { | ||
(node as LeafPsiElement).rawReplaceWithText("${split.first()}\n${split.last()}") | ||
} | ||
} | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
...kotlin/com/pinterest/ktlint/ruleset/experimental/NoFirstLineBlankInMethodBlockRuleTest.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,143 @@ | ||
package com.pinterest.ktlint.ruleset.experimental | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.test.format | ||
import com.pinterest.ktlint.test.lint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
|
||
class NoFirstLineBlankInMethodBlockRuleTest { | ||
|
||
@Test | ||
fun testFormatIsCorrect() { | ||
val formattedFunction = | ||
""" | ||
fun bar() { | ||
val a = 2 | ||
} | ||
""".trimIndent() | ||
|
||
assertThat(NoFirstLineBlankInMethodBlockRule().lint(formattedFunction)).isEmpty() | ||
assertThat(NoFirstLineBlankInMethodBlockRule().format(formattedFunction)).isEqualTo(formattedFunction) | ||
} | ||
|
||
@Test | ||
fun testFormatWhenFirstLineIsEmptyInMethod() { | ||
val unformattedFunction = | ||
""" | ||
fun bar() { | ||
val a = 2 | ||
} | ||
""".trimIndent() | ||
val formattedFunction = | ||
""" | ||
fun bar() { | ||
val a = 2 | ||
} | ||
""".trimIndent() | ||
|
||
assertThat(NoFirstLineBlankInMethodBlockRule().lint(unformattedFunction)).isEqualTo( | ||
listOf( | ||
LintError(2, 1, "no-first-line-blank-in-method-block-rule", "First line in a method block should not be empty") | ||
) | ||
) | ||
assertThat(NoFirstLineBlankInMethodBlockRule().format(unformattedFunction)).isEqualTo(formattedFunction) | ||
} | ||
|
||
@Test | ||
fun testFormatWhenFirstLineIsEmptyInABlockWithinMethod() { | ||
val unformattedFunction = | ||
""" | ||
fun funA() { | ||
if (conditionA()) { | ||
doSomething() | ||
} else if (conditionB()) { | ||
doAnotherThing() | ||
} | ||
} | ||
""".trimIndent() | ||
val formattedFunction = | ||
""" | ||
fun funA() { | ||
if (conditionA()) { | ||
doSomething() | ||
} else if (conditionB()) { | ||
doAnotherThing() | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
assertThat(NoFirstLineBlankInMethodBlockRule().lint(unformattedFunction)).isEqualTo( | ||
listOf( | ||
LintError(2, 1, "no-first-line-blank-in-method-block-rule", "First line in a method block should not be empty"), | ||
LintError(4, 1, "no-first-line-blank-in-method-block-rule", "First line in a method block should not be empty") | ||
) | ||
) | ||
assertThat(NoFirstLineBlankInMethodBlockRule().format(unformattedFunction)).isEqualTo(formattedFunction) | ||
} | ||
|
||
@Test | ||
fun testFormatWhenFirstLineIsEmptyOnlyInABlockWithinMethod() { | ||
val unformattedFunction = | ||
""" | ||
fun funA() { | ||
if (conditionA()) { | ||
doSomething() | ||
} else if (conditionB()) { | ||
doAnotherThing() | ||
} | ||
} | ||
""".trimIndent() | ||
val formattedFunction = | ||
""" | ||
fun funA() { | ||
if (conditionA()) { | ||
doSomething() | ||
} else if (conditionB()) { | ||
doAnotherThing() | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
assertThat(NoFirstLineBlankInMethodBlockRule().lint(unformattedFunction)).isEqualTo( | ||
listOf( | ||
LintError(3, 1, "no-first-line-blank-in-method-block-rule", "First line in a method block should not be empty") | ||
) | ||
) | ||
assertThat(NoFirstLineBlankInMethodBlockRule().format(unformattedFunction)).isEqualTo(formattedFunction) | ||
} | ||
|
||
@Test | ||
fun testFormatWhenFirstLineIsEmptyInFunctionButIgnoreAtClassLevel() { | ||
val unformattedFunction = | ||
""" | ||
class A { | ||
fun bar() { | ||
val a = 2 | ||
} | ||
} | ||
""".trimIndent() | ||
val formattedFunction = | ||
""" | ||
class A { | ||
fun bar() { | ||
val a = 2 | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
assertThat(NoFirstLineBlankInMethodBlockRule().lint(unformattedFunction)).isEqualTo( | ||
listOf( | ||
LintError(4, 1, "no-first-line-blank-in-method-block-rule", "First line in a method block should not be empty") | ||
) | ||
) | ||
assertThat(NoFirstLineBlankInMethodBlockRule().format(unformattedFunction)).isEqualTo(formattedFunction) | ||
} | ||
} |
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
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
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