From 8e6c74abe83e594dcd98acb488db77043472bbd0 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Wed, 5 Jul 2023 15:52:24 +0530 Subject: [PATCH] Introduced custom rule for detekt to exclude the deprecated imports --- buildSrc/build.gradle.kts | 2 ++ .../main/kotlin/plugin/CustomRuleProvider.kt | 34 +++++++++++++++++++ .../plugin/DeprecationExclusionRules.kt | 31 +++++++++++++++++ config/detekt/detekt.yml | 7 +++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt create mode 100644 buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 33a40c49b..2443ee809 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -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()) diff --git a/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt b/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt new file mode 100644 index 000000000..6027d791c --- /dev/null +++ b/buildSrc/src/main/kotlin/plugin/CustomRuleProvider.kt @@ -0,0 +1,34 @@ +/* + * Kiwix Android + * Copyright (c) 2023 Kiwix + * 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 . + * + */ + +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() + ) + ) +} diff --git a/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt new file mode 100644 index 000000000..da12cbbe2 --- /dev/null +++ b/buildSrc/src/main/kotlin/plugin/DeprecationExclusionRules.kt @@ -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" + ) + ) + } + } +} diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index ec68df645..be95c17da 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -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: