|
| 1 | +package detekt |
| 2 | + |
| 3 | +import io.gitlab.arturbosch.detekt.api.CodeSmell |
| 4 | +import io.gitlab.arturbosch.detekt.api.Config |
| 5 | +import io.gitlab.arturbosch.detekt.api.Debt |
| 6 | +import io.gitlab.arturbosch.detekt.api.Entity |
| 7 | +import io.gitlab.arturbosch.detekt.api.Issue |
| 8 | +import io.gitlab.arturbosch.detekt.api.Rule |
| 9 | +import io.gitlab.arturbosch.detekt.api.Severity |
| 10 | +import org.jetbrains.kotlin.com.intellij.psi.PsiElement |
| 11 | +import org.jetbrains.kotlin.psi.KtCallExpression |
| 12 | +import org.jetbrains.kotlin.psi.KtExpression |
| 13 | +import org.jetbrains.kotlin.psi.KtFile |
| 14 | +import org.jetbrains.kotlin.psi.KtLambdaExpression |
| 15 | +import org.jetbrains.kotlin.psi.KtStringTemplateExpression |
| 16 | +import org.jetbrains.kotlin.psi.psiUtil.parents |
| 17 | + |
| 18 | +/** |
| 19 | + * Flags Gradle dependency declarations using raw String coordinates |
| 20 | + * (e.g., implementation("group:artifact:version") or group/name/version named params), |
| 21 | + * enforcing the use of Version Catalog (libs.*), project(), platform(libs.*), etc. |
| 22 | + */ |
| 23 | +class NoStringDependenciesInGradleRule(config: Config = Config.empty) : Rule(config) { |
| 24 | + override val issue: Issue = Issue( |
| 25 | + id = "NoStringDependenciesInGradle", |
| 26 | + severity = Severity.CodeSmell, |
| 27 | + description = "Dependencies must use version catalog (libs.*), project(), or platform(libs.*). " + |
| 28 | + "Raw String coordinates and group/name/version named params are forbidden.", |
| 29 | + debt = Debt.TEN_MINS |
| 30 | + ) |
| 31 | + |
| 32 | + // Common Gradle dependency configurations (add/remove as needed). |
| 33 | + private val knownConfigs = setOf( |
| 34 | + "api", |
| 35 | + "compileOnly", |
| 36 | + "debugImplementation", |
| 37 | + "implementation", |
| 38 | + "kapt", |
| 39 | + "ksp", |
| 40 | + "runtimeOnly", |
| 41 | + "testFixturesApi", |
| 42 | + "testFixturesCompileOnly", |
| 43 | + "testFixturesImplementation", |
| 44 | + "testFixturesRuntimeOnly", |
| 45 | + "testImplementation", |
| 46 | + "jacocoAggregation" |
| 47 | + ) |
| 48 | + |
| 49 | + // Accept both exact matches above and any custom configuration ending with these suffixes |
| 50 | + // (e.g., "myFlavorImplementation", "stagingApi"). |
| 51 | + private val configSuffixes = listOf( |
| 52 | + "Implementation", |
| 53 | + "Api", |
| 54 | + "CompileOnly", |
| 55 | + "RuntimeOnly" |
| 56 | + ) |
| 57 | + |
| 58 | + private var isGradleScript = false |
| 59 | + |
| 60 | + override fun visitKtFile(file: KtFile) { |
| 61 | + isGradleScript = file.name.endsWith(".gradle.kts") |
| 62 | + if (!isGradleScript) return |
| 63 | + super.visitKtFile(file) |
| 64 | + } |
| 65 | + |
| 66 | + override fun visitCallExpression(expression: KtCallExpression) { |
| 67 | + if (!isGradleScript) return |
| 68 | + |
| 69 | + val callee = expression.calleeExpression?.text ?: return |
| 70 | + if (!isDependencyConfigCall(callee)) { |
| 71 | + // Not a dependency configuration call, nothing to do. |
| 72 | + super.visitCallExpression(expression) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // Sanity check: optionally ensure we’re inside a `dependencies {}` scope, to reduce false positives. |
| 77 | + // If you want to be stricter, comment this out. |
| 78 | + val insideDependenciesBlock = expression.parents.any { isDependenciesBlock(it) } |
| 79 | + if (!insideDependenciesBlock) { |
| 80 | + super.visitCallExpression(expression) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // 1) If first argument is a string literal like "g:a:v", flag it unless explicitly allowed. |
| 85 | + val firstArg = expression.valueArguments.firstOrNull() |
| 86 | + val argExpr = firstArg?.getArgumentExpression() |
| 87 | + |
| 88 | + if (argExpr != null) { |
| 89 | + // Allowed forms: libs.*, platform(libs.*), enforcedPlatform(libs.*), project(":"), testFixtures(project(":")), libs.bundles.* |
| 90 | + if (isAllowedExpression(argExpr)) { |
| 91 | + super.visitCallExpression(expression) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + // Literal "group:artifact:version" without interpolation → forbidden |
| 96 | + if (argExpr is KtStringTemplateExpression && !argExpr.hasInterpolation()) { |
| 97 | + reportForbidden(expression, callee, argExpr.text) |
| 98 | + super.visitCallExpression(expression) |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // 2) Also forbid named params style: implementation(group = "...", name = "...", version = "...") |
| 104 | + val hasNamedStringParams = expression.valueArguments.any { va -> |
| 105 | + val name = va.getArgumentName()?.asName?.asString() |
| 106 | + val isNamed = name in setOf("group", "name", "version") |
| 107 | + val v = va.getArgumentExpression() |
| 108 | + val isString = v is KtStringTemplateExpression && !v.hasInterpolation() |
| 109 | + isNamed && isString |
| 110 | + } |
| 111 | + |
| 112 | + if (hasNamedStringParams) { |
| 113 | + report( |
| 114 | + CodeSmell( |
| 115 | + issue, |
| 116 | + Entity.from(expression), |
| 117 | + "Use version catalog (libs.*) or project()/platform(libs.*). " + |
| 118 | + "Do not use group/name/version string parameters." |
| 119 | + ) |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + super.visitCallExpression(expression) |
| 124 | + } |
| 125 | + |
| 126 | + private fun isDependencyConfigCall(name: String): Boolean { |
| 127 | + if (name in knownConfigs) return true |
| 128 | + return configSuffixes.any { name.endsWith(it) } |
| 129 | + } |
| 130 | + |
| 131 | + private fun reportForbidden( |
| 132 | + expression: KtCallExpression, |
| 133 | + callee: String, |
| 134 | + literal: String |
| 135 | + ) { |
| 136 | + report( |
| 137 | + CodeSmell( |
| 138 | + issue, |
| 139 | + Entity.from(expression), |
| 140 | + "Dependency coordinates must not be raw string literals. " + |
| 141 | + "Use $callee(libs.some.alias) or project(\":module\") or platform(libs.some.bom). Found: $literal" |
| 142 | + ) |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * True if the expression is one of the allowed forms: |
| 148 | + * - libs.something / libs.bundles.something |
| 149 | + * - project(":module") |
| 150 | + * - platform(libs.something) / enforcedPlatform(libs.something) |
| 151 | + * - testFixtures(project(":module")) |
| 152 | + */ |
| 153 | + private fun isAllowedExpression(expr: KtExpression): Boolean { |
| 154 | + val text = expr.text.trim() |
| 155 | + |
| 156 | + // plain catalog alias or bundle |
| 157 | + if (text.matches(Regex("""^libs(\.bundles)?\.[A-Za-z0-9_.]+$"""))) return true |
| 158 | + |
| 159 | + // project(":...") |
| 160 | + if (text.startsWith("project(")) return true |
| 161 | + |
| 162 | + // platform(libs...) or enforcedPlatform(libs...) |
| 163 | + if (text.matches(Regex("""^(enforcedPlatform|platform)\s*\(\s*libs(\.bundles)?\.[A-Za-z0-9_.]+\s*\)$"""))) return true |
| 164 | + |
| 165 | + // testFixtures(project(":...")) |
| 166 | + if (text.matches(Regex("""^testFixtures\s*\(\s*project\s*\(.*\)\s*\)$"""))) return true |
| 167 | + |
| 168 | + return false |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Try to detect a dependencies { ... } block. We check if the PSI element is |
| 173 | + * a call 'dependencies' or a lambda passed to it. |
| 174 | + */ |
| 175 | + private fun isDependenciesBlock(el: PsiElement): Boolean { |
| 176 | + // Example shapes: |
| 177 | + // dependencies { ... } |
| 178 | + // dependencies(…) { ... } |
| 179 | + return when (el) { |
| 180 | + is KtCallExpression -> el.calleeExpression?.text == "dependencies" |
| 181 | + is KtLambdaExpression -> (el.parent?.parent as? KtCallExpression) |
| 182 | + ?.calleeExpression?.text == "dependencies" |
| 183 | + |
| 184 | + else -> false |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments