Skip to content

Commit

Permalink
Remove trailing comma's from COLLECTION_LITERAL_EXPRESSION type (pint…
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans authored and romtsn committed Aug 8, 2021
1 parent e9118ad commit b01d151
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,17 @@ fun ASTNode.lineIndent(): String {
@Suppress("unused")
fun ASTNode.logStructure(): ASTNode =
also {
println("Processing ${text.replace("\n", "\\n")} : Type $elementType with parent ${treeParent?.elementType} ")
println("Processing ${text.replaceTabAndNewline()} : Type $elementType with parent ${treeParent?.elementType} ")
children()
.toList()
.map {
println(" ${it.text.replace("\n", "\\n")} : Type ${it.elementType}")
println(" ${it.text.replaceTabAndNewline()} : Type ${it.elementType}")
}
}

private fun String.replaceTabAndNewline(): String =
replace("\t", "\\t").replace("\n", "\\n")

/**
* Assert the element type of the node
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.assertElementType
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.logStructure
import com.pinterest.ktlint.core.ast.prevLeaf
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.psiUtil.endOffset
Expand All @@ -16,11 +17,7 @@ class NoTrailingCommaRule : Rule("no-trailing-comma") {
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
when (node.elementType) {
ElementType.COLLECTION_LITERAL_EXPRESSION ->
// TODO: According to https://github.com/JetBrains/intellij-kotlin/blob/master/formatter/src/org/jetbrains/kotlin/idea/formatter/trailingComma/util.kt
// it is possible to have a trailing comma with this type of element. Need an example before it can
// be implemented
Unit
ElementType.COLLECTION_LITERAL_EXPRESSION -> visitCollectionLiteralExpression(node, emit, autoCorrect)
ElementType.DESTRUCTURING_DECLARATION -> visitDestructuringDeclaration(node, emit, autoCorrect)
ElementType.FUNCTION_LITERAL -> visitFunctionLiteral(node, emit, autoCorrect)
ElementType.FUNCTION_TYPE -> visitFunctionType(node, emit, autoCorrect)
Expand All @@ -34,6 +31,19 @@ class NoTrailingCommaRule : Rule("no-trailing-comma") {
}
}

private fun visitCollectionLiteralExpression(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.COLLECTION_LITERAL_EXPRESSION)
.children()
.last { it.elementType == ElementType.RBRACKET }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}

private fun visitDestructuringDeclaration(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,56 @@ class NoTrailingCommaRuleTest {
assertThat(NoTrailingCommaRule().format(code))
.isEqualTo(autoCorrectedCode)
}

@Test
fun testFormatIsCorrectWithX() {
val code =
"""
annotation class Annotation(val params: IntArray)
@Annotation([1, 2,])
val foo1: Int = 0
@Annotation([
1,
2, // The comma before the comment should be removed without removing the comment itself
])
val foo2: Int = 0
@Annotation([
1,
2, /* The comma before the comment should be removed without removing the comment itself */
])
val foo3: Int = 0
""".trimIndent()
val autoCorrectedCode =
"""
annotation class Annotation(val params: IntArray)
@Annotation([1, 2])
val foo1: Int = 0
@Annotation([
1,
2 // The comma before the comment should be removed without removing the comment itself
])
val foo2: Int = 0
@Annotation([
1,
2 /* The comma before the comment should be removed without removing the comment itself */
])
val foo3: Int = 0
""".trimIndent()

assertThat(NoTrailingCommaRule().lint(code)).isEqualTo(
listOf(
LintError(line = 3, col = 18, ruleId = "no-trailing-comma", detail = "Trailing comma is redundant"),
LintError(line = 8, col = 6, ruleId = "no-trailing-comma", detail = "Trailing comma is redundant"),
LintError(line = 14, col = 6, ruleId = "no-trailing-comma", detail = "Trailing comma is redundant"),
)
)
assertThat(NoTrailingCommaRule().format(code))
.isEqualTo(autoCorrectedCode)
}
}

0 comments on commit b01d151

Please sign in to comment.