Skip to content

Commit

Permalink
Support annotation array when ignoring function-naming rule (pinteres…
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans authored Sep 25, 2023
1 parent 0b85b54 commit a679dd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN_KEYWORD
Expand Down Expand Up @@ -118,17 +119,32 @@ public class FunctionNamingRule :
.any { it.elementType == OVERRIDE_KEYWORD }

private fun ASTNode.isAnnotatedWithAnyOf(excludeWhenAnnotatedWith: Set<String>) =
findChildByType(MODIFIER_LIST)
findChildByType(MODIFIER_LIST).containsAnnotationEntryWithIdentifierIn(excludeWhenAnnotatedWith)

private fun ASTNode?.containsAnnotationEntryWithIdentifierIn(excludeWhenAnnotatedWith: Set<String>): Boolean =
this
?.children()
?.filter { it.elementType == ANNOTATION_ENTRY }
?.mapNotNull { it.findChildByType(ElementType.CONSTRUCTOR_CALLEE) }
?.mapNotNull { it.findChildByType(ElementType.TYPE_REFERENCE) }
?.mapNotNull { it.findChildByType(ElementType.USER_TYPE) }
?.mapNotNull { it.findChildByType(ElementType.REFERENCE_EXPRESSION) }
?.mapNotNull { it.findChildByType(IDENTIFIER) }
?.any { it.text in excludeWhenAnnotatedWith }
?.any {
when (it.elementType) {
ANNOTATION -> {
it.containsAnnotationEntryWithIdentifierIn(excludeWhenAnnotatedWith)
}
ANNOTATION_ENTRY -> {
it.annotationEntryName() in excludeWhenAnnotatedWith
}
else -> false
}
}
?: false

private fun ASTNode.annotationEntryName() =
findChildByType(ElementType.CONSTRUCTOR_CALLEE)
?.findChildByType(ElementType.TYPE_REFERENCE)
?.findChildByType(ElementType.USER_TYPE)
?.findChildByType(ElementType.REFERENCE_EXPRESSION)
?.findChildByType(IDENTIFIER)
?.text

public companion object {
public val IGNORE_WHEN_ANNOTATED_WITH_PROPERTY: EditorConfigProperty<Set<String>> =
EditorConfigProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,22 @@ class FunctionNamingRuleTest {
.withEditorConfigOverride(IGNORE_WHEN_ANNOTATED_WITH_PROPERTY to "Composable, Foo")
.hasNoLintViolations()
}

@Test
fun `Issue 2259 - Given a fun which is to be ignored because it is annotated with a blacklisted annotation using annotation array`() {
val code =
"""
@[Bar Foo]
fun SomeFooBar()
@[Composable Foo]
fun SomeComposableFoo()
@[Bar Composable]
fun SomeComposableBar()
""".trimIndent()
functionNamingRuleAssertThat(code)
.withEditorConfigOverride(IGNORE_WHEN_ANNOTATED_WITH_PROPERTY to "Composable, Foo")
.hasNoLintViolations()
}
}

0 comments on commit a679dd4

Please sign in to comment.