Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive violation in annotation rule #2400

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}