Skip to content

Commit

Permalink
Fix another issue with annotations incorrectly being forced to be mul…
Browse files Browse the repository at this point in the history
…ti-line

Similar to pinterest#1688, an array of annotations would be forced to be multi-line if any of the entries had a parameter.
  • Loading branch information
shashachu committed Jan 4, 2023
1 parent 480dd87 commit e03fd6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
Expand Up @@ -61,26 +61,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)
}
}
}

Expand Down
Expand Up @@ -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()
}
}

0 comments on commit e03fd6d

Please sign in to comment.