Skip to content

Commit

Permalink
Detekt
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 committed Sep 29, 2023
1 parent 57419d7 commit 8976789
Showing 1 changed file with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ fun List<KSAnnotated>.functionsWithPreviewAnnotation(): Map<KSFunctionDeclaratio
}

fun List<KSAnnotated>.functionsWithMultiPreviewAnnotations(
resolver: Resolver
resolver: Resolver,
): Map<KSFunctionDeclaration, List<ComposePreviewSnapshotConfig>> {
val uniqueSnapshotConfigs = filterIsInstance<KSFunctionDeclaration>()
.map { function ->
val allPreviewAnnotations = function.annotations.flatMap { resolver.findAllDirectOrTransitivePreviewAnnotations(it) }.toList()
val allPreviewAnnotations = function.annotations.flatMap {
resolver.findAllDirectOrTransitivePreviewAnnotations(
it
)
}.toList()
function to getUniqueSnapshotConfigsFromMultiPreviewAnnotation(
annotations = allPreviewAnnotations,
previewFunction = function,
Expand Down Expand Up @@ -54,19 +58,28 @@ fun Resolver.getMultiPreviewAnnotations(): List<KSAnnotation> {
.toSet()
.filter {
val annotationQN = it.annotationType.resolve().declaration.qualifiedName
val annotationClassDecl = annotationQN?.let { qualifiedName -> getClassDeclarationByName(qualifiedName) }
annotationClassDecl?.let { classDecl -> hasDirectOrTransitivePreviewAnnotation(classDecl) } ?: false
val annotationClassDecl =
annotationQN?.let { qualifiedName -> getClassDeclarationByName(qualifiedName) }
annotationClassDecl?.let { classDecl -> hasDirectOrTransitivePreviewAnnotation(classDecl) }
?: false
}
.toList()
.sortedBy { it.shortName.asString() }
}

fun Resolver.hasDirectOrTransitivePreviewAnnotation(declaration: KSAnnotated, seenAnnotations: MutableSet<KSAnnotated> = mutableSetOf()): Boolean {
fun Resolver.hasDirectOrTransitivePreviewAnnotation(
declaration: KSAnnotated,
seenAnnotations: MutableSet<KSAnnotated> = mutableSetOf(),
): Boolean {
if (declaration in seenAnnotations) {
return false
}

if (declaration.annotations.any { it.annotationType.resolve().declaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME }) {
val hasPreviewAnnotation = declaration.annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString() ==
COMPOSE_PREVIEW_ANNOTATION_NAME
}
if (hasPreviewAnnotation) {
return true
}

Expand All @@ -75,13 +88,23 @@ fun Resolver.hasDirectOrTransitivePreviewAnnotation(declaration: KSAnnotated, se
return declaration.annotations.any { annotation ->
val annotationQualifiedName = annotation.annotationType.resolve().declaration.qualifiedName
val classDeclaration = annotationQualifiedName?.let { getClassDeclarationByName(it) }
classDeclaration?.let { hasDirectOrTransitivePreviewAnnotation(classDeclaration, seenAnnotations) } ?: false
classDeclaration?.let {
hasDirectOrTransitivePreviewAnnotation(classDeclaration, seenAnnotations)
} ?: false
}
}

fun Resolver.findAllDirectOrTransitivePreviewAnnotations(annotation: KSAnnotation, seenAnnotations: MutableSet<KSClassDeclaration> = mutableSetOf()): List<KSAnnotation> {
val classDeclaration = annotation.annotationType.resolve().declaration.qualifiedName?.let { getClassDeclarationByName(it) }
val isPreviewAnnotation = classDeclaration?.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME
fun Resolver.findAllDirectOrTransitivePreviewAnnotations(
annotation: KSAnnotation,
seenAnnotations: MutableSet<KSClassDeclaration> = mutableSetOf(),
): List<KSAnnotation> {
val classDeclaration = annotation.annotationType.resolve().declaration.qualifiedName?.let {
getClassDeclarationByName(
it
)
}
val isPreviewAnnotation =
classDeclaration?.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME

// Annotations can recursively reference each other so be sure to have a base recursion case
// @Preview itself can't have a recursive relation so we can exclude them from our check
Expand All @@ -91,11 +114,12 @@ fun Resolver.findAllDirectOrTransitivePreviewAnnotations(annotation: KSAnnotatio

seenAnnotations.add(classDeclaration)

val currentPreviewAnnotations = if (classDeclaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME) {
listOf(annotation)
} else {
emptyList()
}
val currentPreviewAnnotations =
if (classDeclaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME) {
listOf(annotation)
} else {
emptyList()
}

val nestedPreviewAnnotations = classDeclaration.annotations.flatMap {
findAllDirectOrTransitivePreviewAnnotations(it, seenAnnotations)
Expand Down

0 comments on commit 8976789

Please sign in to comment.