Skip to content

Commit

Permalink
Fix false positive with eol comment (annotation-spacing) (#1168)
Browse files Browse the repository at this point in the history
Co-authored-by: Yahor Berdnikau <egorr.berd@gmail.com>
  • Loading branch information
t-kameyama and Tapchicoma authored Jun 22, 2021
1 parent 67f785d commit 2c0bfdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Fixed
- Fix false positives for SpacingBetweenDeclarationsWithAnnotationsRule ([#1125](https://github.com/pinterest/ktlint/issues/1125))
- Fix false positive with eol comment (`annotation-spacing`) ([#1124](https://github.com/pinterest/ktlint/issues/1124))
### Changed
- Updated to dokka 1.4.32 ([#1148](https://github.com/pinterest/ktlint/pull/1148))
### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.leaves

/**
* Ensures annotations occur immediately prior to the annotated construct
Expand Down Expand Up @@ -74,6 +75,10 @@ class AnnotationSpacingRule : Rule("annotation-spacing") {
}
)
if (next != null) {
if (next.isPartOf(ElementType.EOL_COMMENT) &&
next.leaves(forward = false).takeWhile { it.isWhiteSpace() }.none { "\n" in it.text }
) return

if (node.elementType != ElementType.FILE_ANNOTATION_LIST) {
val psi = node.psi
emit(psi.endOffset - 1, ERROR_MESSAGE, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,17 @@ class AnnotationSpacingRuleTest {
""".trimIndent()
)
}

@Test
fun `lint eol comment on the same line as the annotation`() {
assertThat(
AnnotationSpacingRule().lint(
"""
@SuppressWarnings // foo
fun bar() {
}
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit 2c0bfdd

Please sign in to comment.