Skip to content

Commit

Permalink
Fix AnnotationRule exception
Browse files Browse the repository at this point in the history
    * Return if there's no annotations found instead of throwing an exception
    * Fies #490
  • Loading branch information
shashachu committed Jul 3, 2019
1 parent 0fef759 commit f098571
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class AnnotationRule : Rule("annotation") {
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val root =
node.children().firstOrNull { it.elementType == MODIFIER_LIST && it.text != DATA_KEYWORD }
node.children().firstOrNull { it.elementType == MODIFIER_LIST }
?: return

val annotations =
root.children()
.mapNotNull { it.psi as? KtAnnotationEntry }
.toList()
check(!annotations.isEmpty()) { "Annotations list should not be empty" }
if (annotations.isEmpty()) {
return
}

// Join the nodes that immediately follow the annotations (whitespace), then add the final whitespace
// if it's not a child of root. This happens when a new line separates the annotations from the annotated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,18 @@ class AnnotationRuleTest {
data class FileModel(val uri: String, val name: String)
""".trimIndent()
assertThat(AnnotationRule().format(code)).isEqualTo(code)
assertThat(AnnotationRule().lint(code)).isEmpty()
}

@Test
fun `no annotation present for function override passes`() {
val code =
"""
package com.example.application.a.b
override fun foo()
""".trimIndent()
assertThat(AnnotationRule().lint(code)).isEmpty()
}

@Test
Expand All @@ -344,6 +355,6 @@ class AnnotationRuleTest {
fun gallery(): String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path
}
""".trimIndent()
assertThat(AnnotationRule().format(code)).isEqualTo(code)
assertThat(AnnotationRule().lint(code)).isEmpty()
}
}

0 comments on commit f098571

Please sign in to comment.