Skip to content

Commit

Permalink
프리뷰 제외 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluu committed Jul 31, 2024
1 parent ddd802c commit fca9a63
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ dependencies {

implementation(platform(libs.androidX.compose.bom))
implementation(libs.bundles.androidX.compose)
debugImplementation(platform(libs.androidX.compose.bom))
debugImplementation(libs.bundles.androidX.compose.debug)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pluu.lintstudy.compose
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun SampleSuccess(text: String, modifier: Modifier) {
Expand All @@ -17,4 +18,10 @@ fun SampleFailure(text: String) {
@Composable
private fun Sample3(text: String) {
BasicText(text = text)
}

@Preview
@Composable
fun SampleFailure4() {

}
3 changes: 3 additions & 0 deletions lint/src/main/java/androidx/compose/lint/ComposableUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ fun UExpression.isInvokedWithinComposable(): Boolean {
val PsiMethod.isComposable
get() = annotations.any { it.qualifiedName == Names.Runtime.Composable.javaFqn }

val PsiMethod.isComposablePreview
get() = annotations.any { it.qualifiedName == Names.ToolingPreview.Preview.javaFqn }

/** Returns whether this variable's type is @Composable or not */
val UVariable.isComposable: Boolean
get() {
Expand Down
4 changes: 4 additions & 0 deletions lint/src/main/java/androidx/compose/lint/Names.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ object Names {
val PackageName = Package("androidx.compose.animation.core")
}

object ToolingPreview {
val Preview = Name(Package("androidx.compose.ui.tooling.preview"), "Preview")
}

object Runtime {
val PackageName = Package("androidx.compose.runtime")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pluu.lint.compose
import androidx.compose.lint.Names
import androidx.compose.lint.inheritsFrom
import androidx.compose.lint.isComposable
import androidx.compose.lint.isComposablePreview
import androidx.compose.lint.returnsUnit
import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.Category
Expand All @@ -27,6 +28,8 @@ class RequiredModifierParameterDetector : Detector(), SourceCodeScanner {
override fun visitMethod(node: UMethod) {
// Ignore non-composable functions
if (!node.isComposable) return
// Ignore composable preview functions
if (node.isComposablePreview) return
// Ignore non-unit composable functions
if (!node.returnsUnit) return
// Ignore private composable functions
Expand Down
15 changes: 15 additions & 0 deletions lint/src/test/java/androidx/compose/lint/test/Compose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ import org.intellij.lang.annotations.Language
import java.util.Locale

object ComposeStubs {
val Preview: TestFile =
kotlin(
"""
package androidx.compose.ui.tooling.preview
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.FUNCTION
)
@Repeatable
annotation class Preview
""".trimIndent()
)
val Composable: TestFile =
bytecodeStub(
filename = "Composable.kt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,29 @@ fun SampleSuccess(text: String) {
""".trimIndent()
)
}

@Test
fun testPreviewSuccess() {
lint()
.files(
kotlin(
"""
package com.pluu.lintstudy.compose
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
@Preview
@Composable
fun SampleSuccess() {
}
"""
),
ComposeStubs.Composable,
ComposeStubs.Modifier,
ComposeStubs.Preview,
)
.run()
.expectClean()
}
}

0 comments on commit fca9a63

Please sign in to comment.