Skip to content

Commit

Permalink
Fix false positive violation in annotation rule (#2400)
Browse files Browse the repository at this point in the history
Closes #2399
  • Loading branch information
paul-dingemans authored Dec 2, 2023
1 parent 365d657 commit 6617166
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
Expand Down Expand Up @@ -281,31 +282,27 @@ public class AnnotationRule :
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean,
) {
val expectedIndent = indentConfig.childIndentOf(node)

node
.children()
.filter { it.elementType == TYPE_PROJECTION }
.forEach { typeProjection ->
typeProjection
.prevLeaf()
?.let { prevLeaf ->
if (prevLeaf.text != expectedIndent) {
emit(prevLeaf.startOffset, "Expected newline", true)
if (autoCorrect) {
prevLeaf.upsertWhitespaceAfterMe(expectedIndent)
}
}
val prevLeaf = typeProjection.prevLeaf().takeIf { it.isWhiteSpace() }
if (prevLeaf == null || prevLeaf.isWhiteSpaceWithoutNewline()) {
emit(typeProjection.startOffset - 1, "Expected newline", true)
if (autoCorrect) {
typeProjection.upsertWhitespaceBeforeMe(indentConfig.childIndentOf(node))
}
}
}

node
.findChildByType(GT)
?.let { gt ->
if (gt.prevLeaf()?.text != expectedIndent) {
val prevLeaf = gt.prevLeaf().takeIf { it.isWhiteSpace() }
if (prevLeaf == null || prevLeaf.isWhiteSpaceWithoutNewline()) {
emit(gt.startOffset, "Expected newline", true)
if (autoCorrect) {
gt.upsertWhitespaceBeforeMe(expectedIndent)
gt.upsertWhitespaceBeforeMe(indentConfig.childIndentOf(node))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ class AnnotationRuleTest {
}

@Test
fun `Given a custom type with multiple annotations on it type parameter(s)`() {
fun `Given a custom type with multiple annotations on it type parameter(s)`() { // xxx
val code =
"""
val fooBar: FooBar<String, @Foo String, @Foo @Bar String, @Bar("bar") @Foo String> = FooBar()
Expand Down Expand Up @@ -986,4 +986,16 @@ class AnnotationRuleTest {
""".trimIndent()
annotationRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2399 - Given a type projection with an annotated type reference`() {
val code =
"""
val foo: List<
@Bar("bar")
Any,
>? = null
""".trimIndent()
annotationRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 6617166

Please sign in to comment.