Skip to content

Commit 752f1fd

Browse files
authored
Merge pull request #46 from skydoves/fix/preview-analysis
Ignore the preview annotated composable function in the stability analysis process.
2 parents f3e60bb + 2161a2d commit 752f1fd

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

app/src/main/kotlin/com/skydoves/myapplication/MainActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ import androidx.compose.ui.graphics.Shape
4444
import androidx.compose.ui.graphics.painter.Painter
4545
import androidx.compose.ui.text.font.FontWeight
4646
import androidx.compose.ui.text.style.TextAlign
47+
import androidx.compose.ui.tooling.preview.Preview
4748
import androidx.compose.ui.unit.dp
4849
import androidx.lifecycle.ViewModel
50+
import com.skydoves.compose.stability.runtime.IgnoreStabilityReport
4951
import com.skydoves.myapplication.models.ImmutableData
5052
import com.skydoves.myapplication.models.MyClass2
5153
import com.skydoves.myapplication.models.NormalClass
@@ -356,9 +358,9 @@ fun StableUserCardPreview() {
356358
/**
357359
* Example: Debug composable excluded from stability reports.
358360
*/
359-
@com.skydoves.compose.stability.runtime.IgnoreStabilityReport
361+
@Preview
360362
@Composable
361-
fun DebugInfoPanel() {
363+
fun DebugInfoPanelPreview() {
362364
Card {
363365
Column(modifier = Modifier.padding(16.dp)) {
364366
Text("Debug Information", fontWeight = FontWeight.Bold)

stability-compiler/src/main/kotlin/com/skydoves/compose/stability/compiler/lower/StabilityAnalyzerTransformer.kt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import org.jetbrains.kotlin.ir.util.render
4141
import org.jetbrains.kotlin.name.FqName
4242

4343
public class StabilityAnalyzerTransformer(
44-
private val context: IrPluginContext,
44+
context: IrPluginContext,
4545
private val stabilityCollector: StabilityInfoCollector? = null,
4646
) : IrElementTransformerVoidWithContext() {
4747

@@ -52,6 +52,7 @@ public class StabilityAnalyzerTransformer(
5252
FqName("com.skydoves.compose.stability.runtime.TraceRecomposition")
5353
private val ignoreStabilityReportFqName =
5454
FqName("com.skydoves.compose.stability.runtime.IgnoreStabilityReport")
55+
private val previewFqName = FqName("androidx.compose.ui.tooling.preview.Preview")
5556

5657
private val irBuilder = RecompositionIrBuilder(context)
5758
private var irBuilderInitialized = false
@@ -71,8 +72,9 @@ public class StabilityAnalyzerTransformer(
7172
return super.visitFunctionNew(declaration)
7273
}
7374

74-
// Skip stability reporting if function has @IgnoreStabilityReport annotation
75-
val shouldIgnoreReport = declaration.hasAnnotation(ignoreStabilityReportFqName)
75+
// Skip stability reporting if function has @IgnoreStabilityReport annotation or @Preview annotation
76+
val shouldIgnoreReport = declaration.hasAnnotation(ignoreStabilityReportFqName) ||
77+
hasPreviewAnnotation(declaration)
7678

7779
// Collect stability information if collector is available and not ignored
7880
if (!shouldIgnoreReport) {
@@ -601,6 +603,35 @@ public class StabilityAnalyzerTransformer(
601603
return fqName in KNOWN_STABLE_TYPES
602604
}
603605

606+
/**
607+
* Check if a function has @Preview annotation (directly or via meta-annotation).
608+
* This includes:
609+
* - Direct @Preview annotation
610+
* - Custom annotations that are meta-annotated with @Preview
611+
*/
612+
private fun hasPreviewAnnotation(function: IrFunction): Boolean {
613+
// Check direct @Preview annotation
614+
if (function.hasAnnotation(previewFqName)) {
615+
return true
616+
}
617+
618+
// Check for meta-annotations (annotations on annotations)
619+
for (annotation in function.annotations) {
620+
try {
621+
val annotationType = annotation.type
622+
val annotationClass = annotationType.classOrNull?.owner
623+
if (annotationClass != null && annotationClass.hasAnnotation(previewFqName)) {
624+
return true
625+
}
626+
} catch (e: Exception) {
627+
// Skip annotations that can't be resolved
628+
continue
629+
}
630+
}
631+
632+
return false
633+
}
634+
604635
/**
605636
* Check if a type is a known unstable Java class.
606637
*

0 commit comments

Comments
 (0)