From 64f3269579e52b091ccbadc3a367f9f80f9edb9f Mon Sep 17 00:00:00 2001 From: Sowmya Viswanathan Date: Thu, 27 Jun 2019 05:16:30 +0530 Subject: [PATCH] Excluding the keyword data from annotation rule (#497) --- .../ktlint/ruleset/standard/AnnotationRule.kt | 3 ++- .../ruleset/standard/AnnotationRuleTest.kt | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt index 4bcbdb6d57..e5f2d11ef7 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt @@ -15,6 +15,7 @@ class AnnotationRule : Rule("annotation") { "Multiple annotations should not be placed on the same line as the annotated construct" const val annotationsWithParametersAreNotOnSeparateLinesErrorMessage = "Annotations with parameters should all be placed on separate lines prior to the annotated construct" + const val DATA_KEYWORD = "data" } override fun visit( @@ -23,7 +24,7 @@ class AnnotationRule : Rule("annotation") { emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit ) { val root = - node.children().firstOrNull { it.elementType == MODIFIER_LIST } + node.children().firstOrNull { it.elementType == MODIFIER_LIST && it.text != DATA_KEYWORD } ?: return val annotations = diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt index 6a5d563fce..598ee015b2 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt @@ -320,4 +320,30 @@ class AnnotationRuleTest { """.trimIndent() ) } + + @Test + fun `no annotation present for data class passes`() { + val code = + """ + package com.example.application.a.b + + data class FileModel(val uri: String, val name: String) + """.trimIndent() + assertThat(AnnotationRule().format(code)).isEqualTo(code) + } + + @Test + fun `no annotation present succeeds for class`() { + val code = + """ + package com.example.application.a + + import android.os.Environment + + class PathProvider { + fun gallery(): String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path + } + """.trimIndent() + assertThat(AnnotationRule().format(code)).isEqualTo(code) + } }