diff --git a/CHANGELOG.md b/CHANGELOG.md index 55496d30b6..d11a8aa8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +* Issue with the `annotation` rule which would sometimes incorrectly force an array of annotations to be multi-line + ### Changed ## [0.48.1] - 2023-01-03 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt index 7fdfdd8891..cc3a475ec3 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt @@ -21,6 +21,8 @@ import com.pinterest.ktlint.core.ast.nextCodeLeaf import com.pinterest.ktlint.core.ast.nextLeaf import com.pinterest.ktlint.core.ast.prevLeaf import com.pinterest.ktlint.core.ast.upsertWhitespaceBeforeMe +import com.pinterest.ktlint.core.initKtLintKLogger +import mu.KotlinLogging import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement @@ -61,26 +63,27 @@ public class AnnotationRule : Rule("annotation") { ) { require(node.elementType == ANNOTATION_ENTRY) - if (node.isAnnotationEntryWithValueArgumentList() && - node.treeParent.treeParent.elementType != VALUE_PARAMETER && // fun fn(@Ann("blah") a: String) - node.treeParent.treeParent.elementType != VALUE_ARGUMENT && // fn(@Ann("blah") "42") - !node.isPartOf(TYPE_ARGUMENT_LIST) && // val property: Map<@Ann("blah") String, Int> - node.isNotReceiverTargetAnnotation() - ) { - checkForAnnotationWithParameterToBePlacedOnSeparateLine(node, emit, autoCorrect) - } + if (node.treeParent.elementType != ANNOTATION) { + if (node.isAnnotationEntryWithValueArgumentList() && + node.treeParent.treeParent.elementType != VALUE_PARAMETER && // fun fn(@Ann("blah") a: String) + node.treeParent.treeParent.elementType != VALUE_ARGUMENT && // fn(@Ann("blah") "42") + !node.isPartOf(TYPE_ARGUMENT_LIST) && // val property: Map<@Ann("blah") String, Int> + node.isNotReceiverTargetAnnotation() + ) { + checkForAnnotationWithParameterToBePlacedOnSeparateLine(node, emit, autoCorrect) + } - if ((node.isFollowedByOtherAnnotationEntry() && node.isOnSameLineAsNextAnnotationEntry()) || - (node.isPrecededByOtherAnnotationEntry() && node.isOnSameLineAsAnnotatedConstruct()) - ) { - checkForAnnotationToBePlacedOnSeparateLine(node, emit, autoCorrect) - } + if ((node.isFollowedByOtherAnnotationEntry() && node.isOnSameLineAsNextAnnotationEntry()) || + (node.isPrecededByOtherAnnotationEntry() && node.isOnSameLineAsAnnotatedConstruct()) + ) { + checkForAnnotationToBePlacedOnSeparateLine(node, emit, autoCorrect) + } - if (node.treeParent.elementType != ANNOTATION && - node.isPrecededByOtherAnnotationEntry() && - node.isOnSameLineAsAnnotatedConstruct() - ) { - checkForMultipleAnnotationsOnSameLineAsAnnotatedConstruct(node, emit, autoCorrect) + if (node.isPrecededByOtherAnnotationEntry() && + node.isOnSameLineAsAnnotatedConstruct() + ) { + checkForMultipleAnnotationsOnSameLineAsAnnotatedConstruct(node, emit, autoCorrect) + } } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt index e1f4dc9ae8..1cd01bb689 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt @@ -548,4 +548,14 @@ class AnnotationRuleTest { """.trimIndent() annotationRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Given an annotation with multiple annotation entries including with parameters then do not force wrapping of the entries`() { + val code = + """ + @[JvmStatic Provides Foo("bar")] + fun foo() = 42 + """.trimIndent() + annotationRuleAssertThat(code).hasNoLintViolations() + } }