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 forbids redundant spaces between parentheses. Fixes pinterest#223
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...rd/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoSpaceBetweenParentheses.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,36 @@ | ||
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.lexer.KtTokens | ||
import org.jetbrains.kotlin.psi.psiUtil.siblings | ||
|
||
class NoSpaceBetweenParentheses : Rule("no-space-between-parentheses") { | ||
|
||
override fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
if (node.elementType == KtTokens.LPAR) { | ||
val iterator = node.siblings().iterator() | ||
var foundEnclosingPar = false | ||
var hasSpaces = false | ||
while (iterator.hasNext() && !foundEnclosingPar) { | ||
val nextNode = iterator.next() | ||
if (!hasSpaces && nextNode is PsiWhiteSpace) { | ||
hasSpaces = true | ||
} else if (nextNode.elementType == KtTokens.RPAR) { | ||
foundEnclosingPar = true | ||
} else { | ||
// the case when there are parameters or multiline declaration | ||
break | ||
} | ||
} | ||
if (hasSpaces && foundEnclosingPar) { | ||
emit(node.startOffset + 1, "Unexpected space between parentheses", true) | ||
if (autoCorrect) { | ||
val enclosingPar = node.siblings().first { it.elementType == KtTokens.RPAR } | ||
node.treeParent.removeRange(node.siblings().first(), enclosingPar) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
...rc/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoSpaceBetweenParenthesesTest.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,40 @@ | ||
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 | ||
|
||
class NoSpaceBetweenParenthesesTest { | ||
@Test | ||
fun testFailWhenEncounterSpace() { | ||
assertThat(NoSpaceBetweenParentheses().lint("fun main( ) {}")) | ||
.isEqualTo(listOf( | ||
LintError(1, 10, "no-space-between-parentheses", "Unexpected space between parentheses") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testNoFailureWhenOnlyParentheses() { | ||
assertThat(NoSpaceBetweenParentheses().lint("fun main() {}")) | ||
.isEmpty() | ||
} | ||
|
||
@Test | ||
fun testNoFailureWhenMultilineDeclaration() { | ||
assertThat(NoSpaceBetweenParentheses().lint( | ||
""" | ||
fun main( | ||
val a: String | ||
) {} | ||
""" | ||
)).isEmpty() | ||
} | ||
|
||
@Test | ||
fun testAutoFormat() { | ||
assertThat(NoSpaceBetweenParentheses().format("fun main( ) {}")) | ||
.isEqualTo("fun main() {}") | ||
} | ||
} |