Skip to content

Commit

Permalink
Remove alias from Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed May 19, 2024
1 parent 60afc4d commit da372f8
Show file tree
Hide file tree
Showing 25 changed files with 4 additions and 70 deletions.
2 changes: 0 additions & 2 deletions detekt-api/api/detekt-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,10 @@ public abstract interface annotation class io/gitlab/arturbosch/detekt/api/Requi

public class io/gitlab/arturbosch/detekt/api/Rule : io/gitlab/arturbosch/detekt/api/DetektVisitor {
public fun <init> (Lio/gitlab/arturbosch/detekt/api/Config;Ljava/lang/String;)V
public final fun getAliases ()Ljava/util/Set;
public final fun getAutoCorrect ()Z
public final fun getBindingContext ()Lorg/jetbrains/kotlin/resolve/BindingContext;
public final fun getCompilerResources ()Lio/gitlab/arturbosch/detekt/api/CompilerResources;
public final fun getConfig ()Lio/gitlab/arturbosch/detekt/api/Config;
public fun getDefaultRuleIdAliases ()Ljava/util/Set;
public final fun getDescription ()Ljava/lang/String;
public fun getRuleId ()Lio/gitlab/arturbosch/detekt/api/Rule$Id;
protected fun postVisit (Lorg/jetbrains/kotlin/psi/KtFile;)V
Expand Down
15 changes: 0 additions & 15 deletions detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,9 @@ open class Rule(
*/
open val ruleId: Id get() = Id(javaClass.simpleName)

/**
* List of rule ids which can optionally be used in suppress annotations to refer to this rule.
*/
val aliases: Set<String> get() = config.valueOrDefault("aliases", defaultRuleIdAliases.toList()).toSet()

var bindingContext: BindingContext = BindingContext.EMPTY
var compilerResources: CompilerResources? = null

/**
* The default names which can be used instead of this [ruleId] to refer to this rule in suppression's.
*
* When overriding this property make sure to meet following structure for detekt-generator to pick
* it up and generate documentation for aliases:
*
* override val defaultRuleIdAliases = setOf("Name1", "Name2")
*/
open val defaultRuleIdAliases: Set<String> = emptySet()

val autoCorrect: Boolean
get() = config.valueOrDefault(Config.AUTO_CORRECT_KEY, false) &&
(config.parent?.valueOrDefault(Config.AUTO_CORRECT_KEY, true) != false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,5 @@ internal fun parseToSeverity(severity: String): Severity {
return Severity.entries.find { it.name.lowercase() == lowercase }
?: error("$severity is not a valid Severity. Allowed values are ${Severity.entries}")
}

private val Rule.aliases: Set<String> get() = config.valueOrDefault("aliases", emptyList<String>()).toSet()
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class MaximumLineLength(config: Config) : FormattingRule(

override val wrapping = MaxLineLengthRule()

override val defaultRuleIdAliases: Set<String> = setOf("MaxLineLength")

@Configuration("maximum line length")
private val maxLineLength: Int by configWithAndroidVariants(120, 100)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class CyclomaticComplexMethod(config: Config) : Rule(
"Prefer splitting up complex methods into smaller, easier to test methods."
) {

override val defaultRuleIdAliases: Set<String> = setOf("ComplexMethod")

@Configuration("The maximum allowed McCabe's Cyclomatic Complexity (MCC) for a method.")
private val allowedComplexity: Int by config(defaultValue = 14)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class SuspendFunWithCoroutineScopeReceiver(config: Config) : Rule(
"The `suspend` modifier should not be used for functions that use a CoroutinesScope as receiver. You should " +
"use suspend functions without the receiver or use plain functions and use coroutineScope { } instead."
) {

override val defaultRuleIdAliases = setOf("SuspendFunctionOnCoroutineScope")

override fun visitNamedFunction(function: KtNamedFunction) {
checkReceiver(function)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class Deprecation(config: Config) : Rule(
"Deprecated elements should not be used."
) {

override val defaultRuleIdAliases = setOf("DEPRECATION")

override fun visitElement(element: PsiElement) {
val diagnostic = hasDeprecationCompilerWarnings(element)
if (diagnostic != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class DoubleMutabilityForCollection(config: Config) : Rule(
"Consider using val or immutable collection or value types."
) {

override val defaultRuleIdAliases: Set<String> = setOf("DoubleMutability")

@Configuration("Define a list of mutable types to trigger on when defined with `var`.")
private val mutableTypes: Set<FqName> by config(defaultMutableTypes) { types ->
types.map { FqName(it) }.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class UnsafeCast(config: Config) : Rule(
config,
"Cast operator throws an exception if the cast is not possible."
) {

override val defaultRuleIdAliases: Set<String> = setOf("UNCHECKED_CAST")

override fun visitBinaryWithTypeRHSExpression(expression: KtBinaryExpressionWithTypeRHS) {
if (bindingContext.diagnostics.forElement(expression.operationReference)
.any { it.factory == Errors.CAST_NEVER_SUCCEEDS }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class ClassNaming(config: Config) : Rule(
"A class or object name should fit the naming pattern defined in the projects configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("ClassName")

@Configuration("naming pattern")
private val classPattern: Regex by config("[A-Z][a-zA-Z0-9]*") { it.toRegex() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class FunctionNameMaxLength(config: Config) : Rule(
"Function names should not be longer than the maximum set in the project configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("FunctionMaxNameLength")

@Configuration("maximum name length")
private val maximumFunctionNameLength: Int by config(DEFAULT_MAXIMUM_FUNCTION_NAME_LENGTH)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class FunctionNameMinLength(config: Config) : Rule(
"Function names should not be shorter than the minimum defined in the configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("FunctionMinNameLength")

@Configuration("minimum name length")
private val minimumFunctionNameLength: Int by config(3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class FunctionNaming(config: Config) : Rule(
"Function names should follow the naming convention set in the configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("FunctionName")

@Configuration("naming pattern")
private val functionPattern: Regex by config("[a-z][a-zA-Z0-9]*", String::toRegex)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class InvalidPackageDeclaration(config: Config) : Rule(
"Kotlin source files should be stored in the directory corresponding to its package statement."
) {

override val defaultRuleIdAliases: Set<String> = setOf("PackageDirectoryMismatch")

@Configuration("if specified this part of the package structure is ignored")
private val rootPackage: String by config("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class ObjectPropertyNaming(config: Config) : Rule(
"Property names inside objects should follow the naming convention set in the projects configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("ObjectPropertyName")

@Configuration("naming pattern")
private val constantPattern: Regex by config("[A-Za-z][_A-Za-z0-9]*") { it.toRegex() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class PackageNaming(config: Config) : Rule(
"Package names should match the naming convention set in the configuration."
) {

override val defaultRuleIdAliases: Set<String> = setOf("PackageName", "PackageDirectoryMismatch")

@Configuration("naming pattern")
private val packagePattern: Regex by config("""[a-z]+(\.[a-z][A-Za-z0-9]*)*""") { it.toRegex() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class InvalidPackageDeclarationSpec {
assertThat(findings).isEmpty()
}

@Test
fun `has correct aliases`() {
assertThat(InvalidPackageDeclaration(Config.empty).aliases)
.containsExactlyInAnyOrder("PackageDirectoryMismatch")
}

@Test
fun `should report if package declaration does not match source location`() {
val ktFile = compileForTest(Path("src/test/resources/InvalidPackageDeclarationSpec/src/bar/incorrect.kt"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ class PackageNamingSpec {
assertThat(rule.compileAndLint("package package_1")).isEmpty()
}

@Test
fun `should ignore the issue by alias suppression - PackageDirectoryMismatch`() {
assertThat(PackageNaming(Config.empty).aliases)
.containsExactlyInAnyOrder("PackageDirectoryMismatch", "PackageName")
}

@Test
fun `should find a uppercase package name`() {
assertThat(PackageNaming(Config.empty).compileAndLint("package FOO.BAR")).hasSize(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class UnusedParameter(config: Config) : Rule(
"Function parameter is unused and should be removed."
) {

override val defaultRuleIdAliases: Set<String> = setOf("UNUSED_VARIABLE", "UNUSED_PARAMETER", "unused", "UnusedPrivateMember")

@Configuration("unused parameter names matching this regex are ignored")
private val allowedNames: Regex by config("ignored|expected", String::toRegex)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class UnusedPrivateClass(config: Config) : Rule(
"Private class is unused and should be removed."
) {

override val defaultRuleIdAliases: Set<String> = setOf("unused")

override fun visit(root: KtFile) {
super.visit(root)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class UnusedPrivateMember(config: Config) : Rule(
"Private function is unused and should be removed."
) {

override val defaultRuleIdAliases: Set<String> = setOf("UNUSED_VARIABLE", "UNUSED_PARAMETER", "unused")

@Configuration("unused private function names matching this regex are ignored")
private val allowedNames: Regex by config("", String::toRegex)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class UnusedPrivateProperty(config: Config) : Rule(
"Property is unused and should be removed."
) {

override val defaultRuleIdAliases: Set<String> = setOf("UNUSED_PARAMETER", "unused", "UnusedPrivateMember")

@Configuration("unused property names matching this regex are ignored")
private val allowedNames: Regex by config(
"ignored|expected|serialVersionUID",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class UnusedVariable(config: Config) : Rule(
config,
"Variable is unused and should be removed."
) {

override val defaultRuleIdAliases: Set<String> = setOf("UNUSED_VARIABLE", "unused")

@Configuration("unused variables names matching this regex are ignored")
private val allowedNames: Regex by config(
"ignored|_",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class VarCouldBeVal(config: Config) : Rule(
"Var declaration could be val."
) {

override val defaultRuleIdAliases: Set<String> = setOf("CanBeVal")

@Configuration("Whether to ignore uninitialized lateinit vars")
private val ignoreLateinitVar: Boolean by config(defaultValue = false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ private fun List<Finding>.filterSuppressed(rule: Rule): List<Finding> {
it.entity.ktElement?.isSuppressedBy(rule.ruleId, rule.aliases, RuleSet.Id("NoARuleSetId")) == true
}
}

private val Rule.aliases: Set<String> get() = config.valueOrDefault("aliases", emptyList<String>()).toSet()

0 comments on commit da372f8

Please sign in to comment.