|
| 1 | +/** |
| 2 | + * Backpack for Android - Skyscanner's Design System |
| 3 | + * |
| 4 | + * Copyright 2018 - 2026 Skyscanner Ltd |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package net.skyscanner.backpack.lint.check |
| 20 | + |
| 21 | +import com.android.tools.lint.detector.api.Category |
| 22 | +import net.skyscanner.backpack.lint.util.LintConstants |
| 23 | +import com.android.tools.lint.detector.api.Detector |
| 24 | +import com.android.tools.lint.detector.api.Implementation |
| 25 | +import com.android.tools.lint.detector.api.Issue |
| 26 | +import com.android.tools.lint.detector.api.JavaContext |
| 27 | +import com.android.tools.lint.detector.api.LintFix |
| 28 | +import com.android.tools.lint.detector.api.Scope |
| 29 | +import com.android.tools.lint.detector.api.Severity |
| 30 | +import com.android.tools.lint.detector.api.SourceCodeScanner |
| 31 | +import com.intellij.psi.PsiElement |
| 32 | +import com.intellij.psi.PsiField |
| 33 | +import com.intellij.psi.PsiMethod |
| 34 | +import org.jetbrains.uast.UCallExpression |
| 35 | +import org.jetbrains.uast.ULiteralExpression |
| 36 | +import org.jetbrains.uast.UQualifiedReferenceExpression |
| 37 | +import org.jetbrains.uast.UReferenceExpression |
| 38 | + |
| 39 | +@Suppress("UnstableApiUsage") |
| 40 | +class HardcodedComposeColorDetector : Detector(), SourceCodeScanner { |
| 41 | + |
| 42 | + companion object { |
| 43 | + private const val EXPLANATION = |
| 44 | + "This color doesn't exist in Backpack. Please check BpkTheme.colors for available colors.\n\n${LintConstants.SUPPORT_MESSAGE}" |
| 45 | + |
| 46 | + private const val COMPOSE_COLOR_CLASS = "androidx.compose.ui.graphics.Color" |
| 47 | + private const val COMPOSE_COLOR_KT_CLASS = "androidx.compose.ui.graphics.ColorKt" |
| 48 | + |
| 49 | + private fun getColorSuggestion(hex: String): String { |
| 50 | + val tokens = GeneratedColorTokenMap.COLOR_TOKEN_MAP[hex] |
| 51 | + return if (tokens != null) { |
| 52 | + if (tokens.size == 1) { |
| 53 | + "Use ${tokens[0]} instead of Color($hex)" |
| 54 | + } else { |
| 55 | + val tokenList = tokens.joinToString("\n- ") |
| 56 | + "Use one of these tokens instead of Color($hex). All these tokens have the same color value:\n- $tokenList" |
| 57 | + } |
| 58 | + } else { |
| 59 | + EXPLANATION |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + val ISSUE = Issue.create( |
| 64 | + id = "HardcodedComposeColor", |
| 65 | + briefDescription = "Hardcoded Compose Color detected", |
| 66 | + explanation = EXPLANATION, |
| 67 | + category = Category.CORRECTNESS, |
| 68 | + severity = Severity.ERROR, |
| 69 | + implementation = Implementation( |
| 70 | + HardcodedComposeColorDetector::class.java, |
| 71 | + Scope.JAVA_FILE_SCOPE, |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + private val HARDCODED_COLOR_NAMES = setOf( |
| 76 | + "Red", "Blue", "Green", "Yellow", "Cyan", "Magenta", "White", "Black", |
| 77 | + "Gray", "LightGray", "DarkGray", |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + override fun getApplicableConstructorTypes(): List<String> = listOf(COMPOSE_COLOR_CLASS) |
| 82 | + |
| 83 | + override fun getApplicableMethodNames(): List<String> = listOf("Color") + HARDCODED_COLOR_NAMES.toList() |
| 84 | + |
| 85 | + override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 86 | + val methodName = node.methodName ?: return |
| 87 | + if (methodName != "Color" && methodName != "constructor-impl") return |
| 88 | + |
| 89 | + val containingClass = method.containingClass ?: return |
| 90 | + val fqName = context.evaluator.getQualifiedName(containingClass) |
| 91 | + if (!isComposeColorClass(fqName)) return |
| 92 | + |
| 93 | + reportHardcodedColorIfLiteral(context, node) |
| 94 | + } |
| 95 | + |
| 96 | + override fun visitConstructor( |
| 97 | + context: JavaContext, |
| 98 | + node: UCallExpression, |
| 99 | + constructor: PsiMethod, |
| 100 | + ) { |
| 101 | + val containingClass = constructor.containingClass ?: return |
| 102 | + val fqName = context.evaluator.getQualifiedName(containingClass) |
| 103 | + if (fqName != COMPOSE_COLOR_CLASS) return |
| 104 | + |
| 105 | + reportHardcodedColorIfLiteral(context, node) |
| 106 | + } |
| 107 | + |
| 108 | + private fun isComposeColorClass(fqName: String?): Boolean { |
| 109 | + return fqName == COMPOSE_COLOR_CLASS || fqName == COMPOSE_COLOR_KT_CLASS |
| 110 | + } |
| 111 | + |
| 112 | + private fun reportHardcodedColorIfLiteral(context: JavaContext, node: UCallExpression) { |
| 113 | + val arguments = node.valueArguments |
| 114 | + if (arguments.isEmpty()) return |
| 115 | + |
| 116 | + val hexValue = extractHexValue(arguments.firstOrNull()) ?: return |
| 117 | + val message = getColorSuggestion(hexValue) |
| 118 | + val tokens = GeneratedColorTokenMap.COLOR_TOKEN_MAP[hexValue] |
| 119 | + val fix = buildColorLintFix(hexValue, tokens) |
| 120 | + |
| 121 | + context.report(ISSUE, context.getLocation(node), message, fix) |
| 122 | + } |
| 123 | + |
| 124 | + private fun buildColorLintFix(hexValue: String, tokens: List<String>?): LintFix? { |
| 125 | + tokens ?: return null |
| 126 | + return if (tokens.size == 1) { |
| 127 | + LintFix.create() |
| 128 | + .replace() |
| 129 | + .text("Color($hexValue)") |
| 130 | + .with(tokens[0]) |
| 131 | + .autoFix() |
| 132 | + .build() |
| 133 | + } else { |
| 134 | + val alternatives = tokens.map { tokenName -> |
| 135 | + LintFix.create() |
| 136 | + .replace() |
| 137 | + .text("Color($hexValue)") |
| 138 | + .with(tokenName) |
| 139 | + .build() |
| 140 | + } |
| 141 | + LintFix.create().alternatives(*alternatives.toTypedArray()) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private fun extractHexValue(argument: org.jetbrains.uast.UElement?): String? { |
| 146 | + if (argument is ULiteralExpression) { |
| 147 | + val value = argument.value |
| 148 | + if (value is Long || value is Int) { |
| 149 | + val hex = value.toString().toLongOrNull()?.toString(16)?.uppercase() |
| 150 | + return hex?.let { "0x${it.padStart(8, '0')}" } |
| 151 | + } |
| 152 | + } |
| 153 | + return null |
| 154 | + } |
| 155 | + |
| 156 | + override fun getApplicableReferenceNames(): List<String> = HARDCODED_COLOR_NAMES.toList() |
| 157 | + |
| 158 | + override fun visitReference( |
| 159 | + context: JavaContext, |
| 160 | + reference: UReferenceExpression, |
| 161 | + referenced: PsiElement, |
| 162 | + ) { |
| 163 | + // Check if this is a Color.Red, Color.Blue, etc. reference |
| 164 | + if (referenced !is PsiField) return |
| 165 | + |
| 166 | + val parent = reference.uastParent |
| 167 | + if (parent is UQualifiedReferenceExpression) { |
| 168 | + val containingClass = referenced.containingClass ?: return |
| 169 | + val evaluator = context.evaluator |
| 170 | + val qualifiedReference = evaluator.getQualifiedName(containingClass) |
| 171 | + |
| 172 | + if (qualifiedReference == COMPOSE_COLOR_CLASS) { |
| 173 | + val fieldName = referenced.name |
| 174 | + if (fieldName !in setOf("Unspecified", "Transparent")) { |
| 175 | + context.report( |
| 176 | + ISSUE, |
| 177 | + context.getLocation(parent), |
| 178 | + EXPLANATION, |
| 179 | + ) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments