Skip to content

Commit

Permalink
Do not remove the trailing comma after a named parameter of type array
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans committed Feb 23, 2022
1 parent e1674f8 commit 17a7eca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
import org.jetbrains.kotlin.utils.addToStdlib.cast

Expand Down Expand Up @@ -236,7 +237,7 @@ public class TrailingCommaRule :
prevNode.psi.parent.addAfter(comma, prevNode.psi)
}
}
TrailingCommaState.REDUNDANT -> {
TrailingCommaState.REDUNDANT -> { //if (!isTrailingCommaAllowed) {
emit(
trailingCommaNode!!.startOffset,
"Unnecessary trailing comma before \"${inspectNode.text}\"",
Expand Down Expand Up @@ -281,11 +282,22 @@ public class TrailingCommaRule :
// "something",
// ])
val lastChild = element.collectDescendantsOfType<KtCollectionLiteralExpression>().last()
containsLineBreakInRange(element.rightParenthesis!!, lastChild.rightBracket!!)
containsLineBreakInLeafsRange(lastChild.rightBracket!!, element.rightParenthesis!!)
}
else -> element.textContains('\n')
}

private fun containsLineBreakInLeafsRange(from: PsiElement, to: PsiElement): Boolean {
var leaf: PsiElement? = from
while (leaf != null && !leaf.isEquivalentTo(to)) {
if (leaf.textContains('\n')) {
return true
}
leaf = leaf.nextLeaf(skipEmptyElements = false)
}
return leaf?.textContains('\n') ?: false
}

private fun ASTNode.isIgnorable(): Boolean =
elementType == ElementType.WHITE_SPACE ||
elementType == ElementType.EOL_COMMENT ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.pinterest.ktlint.test.format
import com.pinterest.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.ec4j.core.model.PropertyType
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

Expand Down Expand Up @@ -1097,10 +1096,28 @@ class TrailingCommaRuleTest {
}

@Test
fun assertTrue() {
val ktlintOutput = "Some unused import"
fun `Issue 1379 - Trailing comma is allowed after array in annotation`() {
val code =
"""
import kotlin.reflect.KClass
@Foo(
values = [
Foo::class,
Foo::class,
],
)
annotation class Foo(val values: Array<KClass<*>>)
""".trimIndent()

val editorConfigFilePath = writeEditorConfigFile(
ALLOW_TRAILING_COMMA_ON_DECLARATION_SITE,
ALLOW_TRAILING_COMMA_ON_CALL_SITE
).absolutePath

assertTrue(ktlintOutput.contains("unused import"))
assertThat(TrailingCommaRule().lint(editorConfigFilePath, code)).isEmpty()
assertThat(TrailingCommaRule().format(editorConfigFilePath, code))
.isEqualTo(code)
}

private fun writeEditorConfigFile(vararg editorConfigProperties: Pair<PropertyType<Boolean>, String>) = editorConfigTestRule
Expand Down

0 comments on commit 17a7eca

Please sign in to comment.