Skip to content

Commit

Permalink
Fix AnnotationRule exception and re-enable rule (#509)
Browse files Browse the repository at this point in the history
* Fix AnnotationRule exception
    * Return if there's no annotations found instead of throwing an exception
    * Fixes #490 and #502
    * Re-enable rule in standard ruleset
  • Loading branch information
shashachu authored Jul 5, 2019
1 parent 657299b commit e5fd74c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 1 addition & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ insert_final_newline = true

[*.{java,kt,kts,scala,rs,xml,kt.spec,kts.spec}]
indent_size = 4
# annotation - "./mvnw clean verify" fails with "Internal Error")
# multiline-if-else - disabled until auto-correct is working properly
# (e.g. try formatting "if (true)\n return { _ ->\n _\n}")
# no-it-in-multiline-lambda - disabled until it's clear what to do in case of `import _.it`
disabled_rules=annotation,multiline-if-else
disabled_rules=multiline-if-else

[{Makefile,*.go}]
indent_style = tab
Expand Down
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 e5fd74c

Please sign in to comment.