|
| 1 | +package kiwi.orbit.compose.lint.detectors |
| 2 | + |
| 3 | +import com.android.tools.lint.detector.api.Category |
| 4 | +import com.android.tools.lint.detector.api.Detector |
| 5 | +import com.android.tools.lint.detector.api.Implementation |
| 6 | +import com.android.tools.lint.detector.api.Issue |
| 7 | +import com.android.tools.lint.detector.api.JavaContext |
| 8 | +import com.android.tools.lint.detector.api.Scope |
| 9 | +import com.android.tools.lint.detector.api.Severity |
| 10 | +import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | +import com.android.tools.lint.detector.api.computeKotlinArgumentMapping |
| 12 | +import com.intellij.psi.PsiElement |
| 13 | +import com.intellij.psi.PsiJavaFile |
| 14 | +import com.intellij.psi.PsiMember |
| 15 | +import com.intellij.psi.PsiMethod |
| 16 | +import com.intellij.psi.PsiNamedElement |
| 17 | +import org.jetbrains.uast.UBlockExpression |
| 18 | +import org.jetbrains.uast.UCallExpression |
| 19 | +import org.jetbrains.uast.ULambdaExpression |
| 20 | +import org.jetbrains.uast.skipParenthesizedExprDown |
| 21 | +import org.jetbrains.uast.tryResolveNamed |
| 22 | + |
| 23 | +class MaterialDialogWithOrbitButtonsDetector : Detector(), SourceCodeScanner { |
| 24 | + |
| 25 | + companion object { |
| 26 | + internal val ISSUE: Issue = Issue.create( |
| 27 | + id = "MaterialDialogWithOrbitButtonsDetector", |
| 28 | + briefDescription = "Usage of Orbit buttons inside Material Dialogs.", |
| 29 | + explanation = "Use Material TextButton inside of the Material Dialogs to preserve the material look.", |
| 30 | + category = Category.CUSTOM_LINT_CHECKS, |
| 31 | + priority = 7, |
| 32 | + severity = Severity.ERROR, |
| 33 | + implementation = Implementation( |
| 34 | + MaterialDialogWithOrbitButtonsDetector::class.java, |
| 35 | + Scope.JAVA_FILE_SCOPE, |
| 36 | + ), |
| 37 | + ) |
| 38 | + |
| 39 | + private const val PACKAGE_MATERIAL_ALERT = "androidx.compose.material3" |
| 40 | + private const val MATERIAL_ALERT_CONFIRM_BUTTON_PARAM = "confirmButton" |
| 41 | + private const val MATERIAL_ALERT_DISMISS_BUTTON_PARAM = "dismissButton" |
| 42 | + |
| 43 | + private const val PACKAGE_MATERIAL_TEXT_BUTTON = "androidx.compose.material3.TextButton" |
| 44 | + private const val NAME_MATERIAL_TEXT_BUTTON = "TextButton" |
| 45 | + |
| 46 | + private const val PACKAGE_ORBIT_BUTTON = "kiwi.orbit.compose.ui.controls" |
| 47 | + private const val NAME_ORBIT_BUTTON = "Button" |
| 48 | + } |
| 49 | + |
| 50 | + override fun getApplicableMethodNames(): List<String> = listOf("AlertDialog") |
| 51 | + |
| 52 | + override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 53 | + if (method.isInPackageName(PACKAGE_MATERIAL_ALERT)) { |
| 54 | + val confirmButtonArgument = getArgument(node, method, MATERIAL_ALERT_CONFIRM_BUTTON_PARAM) |
| 55 | + |
| 56 | + (confirmButtonArgument?.body as? UBlockExpression)?.let { body -> |
| 57 | + val resolvedBodyExpression = body.resolveFirstBodyExpression() ?: return@let |
| 58 | + val packageName = resolvedBodyExpression.getPackageName() ?: return@let |
| 59 | + |
| 60 | + val expressionName = resolvedBodyExpression.name |
| 61 | + val isButton = expressionName?.contains(NAME_ORBIT_BUTTON) == true |
| 62 | + val isOrbit = packageName == PACKAGE_ORBIT_BUTTON |
| 63 | + if (isButton && isOrbit) { |
| 64 | + context.report( |
| 65 | + issue = ISSUE, |
| 66 | + scope = body, |
| 67 | + location = context.getLocation(body), |
| 68 | + message = "$expressionName from Orbit used in Material Dialog's confirmButton slot.", |
| 69 | + quickfixData = fix() |
| 70 | + .replace() |
| 71 | + .text(expressionName) |
| 72 | + .with(NAME_MATERIAL_TEXT_BUTTON) |
| 73 | + .imports(PACKAGE_MATERIAL_TEXT_BUTTON) |
| 74 | + .shortenNames() |
| 75 | + .build(), |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + val dismissButtonArgument = getArgument(node, method, MATERIAL_ALERT_DISMISS_BUTTON_PARAM) |
| 81 | + |
| 82 | + (dismissButtonArgument?.body as? UBlockExpression)?.let { body -> |
| 83 | + val resolvedBodyExpression = body.resolveFirstBodyExpression() ?: return@let |
| 84 | + val packageName = resolvedBodyExpression.getPackageName() ?: return@let |
| 85 | + |
| 86 | + val expressionName = resolvedBodyExpression.name |
| 87 | + val isButton = expressionName?.contains(NAME_ORBIT_BUTTON) == true |
| 88 | + val isOrbit = packageName == PACKAGE_ORBIT_BUTTON |
| 89 | + if (isButton && isOrbit) { |
| 90 | + context.report( |
| 91 | + issue = ISSUE, |
| 92 | + scope = body, |
| 93 | + location = context.getLocation(body), |
| 94 | + message = "$expressionName from Orbit used in Material Dialog's dismissButton slot.", |
| 95 | + quickfixData = fix() |
| 96 | + .replace() |
| 97 | + .text(expressionName) |
| 98 | + .with(NAME_MATERIAL_TEXT_BUTTON) |
| 99 | + .imports(PACKAGE_MATERIAL_TEXT_BUTTON) |
| 100 | + .shortenNames() |
| 101 | + .build(), |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun getArgument( |
| 109 | + node: UCallExpression, |
| 110 | + method: PsiMethod, |
| 111 | + argumentName: String, |
| 112 | + ): ULambdaExpression? = computeKotlinArgumentMapping(node, method) |
| 113 | + .orEmpty() |
| 114 | + .filter { (_, parameter) -> |
| 115 | + parameter.name == argumentName |
| 116 | + } |
| 117 | + .keys |
| 118 | + .filterIsInstance<ULambdaExpression>() |
| 119 | + .firstOrNull() |
| 120 | + |
| 121 | + private fun PsiMethod.isInPackageName(packageName: String): Boolean { |
| 122 | + val actual = (containingFile as? PsiJavaFile)?.packageName |
| 123 | + return packageName == actual |
| 124 | + } |
| 125 | + |
| 126 | + private fun PsiElement.getPackageName(): String? = when (this) { |
| 127 | + is PsiMember -> this.containingClass?.qualifiedName?.let { it.substring(0, it.lastIndexOf(".")) } |
| 128 | + else -> null |
| 129 | + } |
| 130 | + |
| 131 | + private fun UBlockExpression.resolveFirstBodyExpression(): PsiNamedElement? { |
| 132 | + return expressions.firstOrNull()?.skipParenthesizedExprDown()?.tryResolveNamed() |
| 133 | + } |
| 134 | +} |
0 commit comments