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.
Add new experimental rule to remove trailing comma's from argument an…
…d values lists (pinterest#709)
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 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
31 changes: 31 additions & 0 deletions
31
...rimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NoTrailingCommaRule.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,31 @@ | ||
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.children | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.psiUtil.endOffset | ||
|
||
class NoTrailingCommaRule : Rule("no-trailing-comma") { | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
if (node.elementType == ElementType.VALUE_ARGUMENT_LIST || node.elementType == ElementType.VALUE_PARAMETER_LIST) { | ||
val lastNode = node | ||
.children() | ||
.filter { it.elementType != ElementType.WHITE_SPACE } | ||
.filter { it.elementType != ElementType.EOL_COMMENT } | ||
.filter { it.elementType != ElementType.RPAR } | ||
.last() | ||
if (lastNode.elementType == ElementType.COMMA) { | ||
emit(lastNode.psi.endOffset - 1, "Trailing command in argument list is redundant", true) | ||
if (autoCorrect) { | ||
node.removeChild(lastNode) | ||
} | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ntal/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NoTrailingCommaRuleTest.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,65 @@ | ||
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 NoTrailingCommaRuleTest { | ||
@Test | ||
fun testFormatIsCorrectWithArgumentList() { | ||
val code = | ||
""" | ||
val list1 = listOf("a", "b",) | ||
val list2 = listOf( | ||
"a", | ||
"b", // The comma before the comment should be removed without removing the comment itself | ||
) | ||
""".trimIndent() | ||
val autoCorrectedCode = | ||
""" | ||
val list1 = listOf("a", "b") | ||
val list2 = listOf( | ||
"a", | ||
"b" // The comma before the comment should be removed without removing the comment itself | ||
) | ||
""".trimIndent() | ||
|
||
assertThat(NoTrailingCommaRule().lint(code)).isEqualTo( | ||
listOf( | ||
LintError(line = 1, col = 28, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"), | ||
LintError(line = 4, col = 8, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"), | ||
) | ||
) | ||
assertThat(NoTrailingCommaRule().format(code)) | ||
.isEqualTo(autoCorrectedCode) | ||
} | ||
|
||
@Test | ||
fun testFormatIsCorrectWithValueList() { | ||
val code = | ||
""" | ||
data class Foo1( | ||
val bar: Int, // The comma before the comment should be removed without removing the comment itself | ||
) | ||
data class Foo2(val bar: Int,) | ||
""".trimIndent() | ||
val autoCorrectedCode = | ||
""" | ||
data class Foo1( | ||
val bar: Int // The comma before the comment should be removed without removing the comment itself | ||
) | ||
data class Foo2(val bar: Int) | ||
""".trimIndent() | ||
|
||
assertThat(NoTrailingCommaRule().lint(code)).isEqualTo( | ||
listOf( | ||
LintError(line = 2, col = 16, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"), | ||
LintError(line = 4, col = 29, ruleId = "no-trailing-comma", detail = "Trailing command in argument list is redundant"), | ||
) | ||
) | ||
assertThat(NoTrailingCommaRule().format(code)) | ||
.isEqualTo(autoCorrectedCode) | ||
} | ||
} |