Introduced custom rule for detekt to exclude the deprecated imports

This commit is contained in:
MohitMali 2023-07-05 15:52:24 +05:30 committed by Kelson
parent af23cf56b7
commit 8e6c74abe8
4 changed files with 73 additions and 1 deletions

View File

@ -22,6 +22,8 @@ dependencies {
exclude(group = "com.google.guava", module = "guava")
}
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0")
// For defining custom rules in detekt
implementation("io.gitlab.arturbosch.detekt:detekt-api:1.20.0")
implementation("com.googlecode.json-simple:json-simple:1.1")
implementation(gradleApi())

View File

@ -0,0 +1,34 @@
/*
* Kiwix Android
* Copyright (c) 2023 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package plugin
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.RuleSet
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
class CustomRulesProvider : RuleSetProvider {
override val ruleSetId: String = "deprecation-rules"
override fun instance(config: Config): RuleSet = RuleSet(
ruleSetId,
listOf(
DeprecationExclusionRule()
)
)
}

View File

@ -0,0 +1,31 @@
package plugin
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import org.jetbrains.kotlin.psi.KtImportDirective
class DeprecationExclusionRule : Rule() {
override val issue = Issue(
id = "DeprecationExclusion",
severity = Severity.CodeSmell,
description = "Deprecated imports should be excluded",
debt = Debt.FIVE_MINS
)
override fun visitImportDirective(importDirective: KtImportDirective) {
val importText = importDirective.text
if (importText.contains("@Deprecated")) {
report(
CodeSmell(
issue,
Entity.from(importDirective),
"Deprecated import found: $importText"
)
)
}
}
}

View File

@ -31,6 +31,11 @@ console-reports:
# - 'FindingsReport'
- 'FileBasedFindingsReport'
deprecation-rules:
active: true
DeprecationExclusion:
active: true
comments:
active: true
excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt"
@ -295,7 +300,7 @@ performance:
potential-bugs:
active: true
Deprecation:
active: false
active: true
DuplicateCaseInWhenExpression:
active: true
EqualsAlwaysReturnsTrueOrFalse: