Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AnnotationRule exception #509

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}