From 8fa1ece6a3c61565a5b58e5effca82b187d82071 Mon Sep 17 00:00:00 2001 From: Denis Kumar Date: Tue, 11 May 2021 17:41:56 +0300 Subject: [PATCH] Value Parameter in Magic Number Rule (#874) * Value Parameter in Magi Number Rule ### What's done: Added new flag to handle value parameter Added this flag to config files Added tests --- diktat-analysis.yml | 2 + .../ruleset/rules/chapter3/MagicNumberRule.kt | 7 +++- .../cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 1 + .../main/resources/diktat-analysis-huawei.yml | 2 + .../chapter3/MagicNumberRuleWarnTest.kt | 39 +++++++++++++++++++ examples/maven/diktat-analysis.yml | 2 + 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index fc8c6764ee..14bfcb612c 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -312,6 +312,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt index cf6174d2a0..643362eb52 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt @@ -16,6 +16,7 @@ import com.pinterest.ktlint.core.ast.ElementType.MINUS import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RANGE +import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER import com.pinterest.ktlint.core.ast.parent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.psi.KtFunction @@ -49,7 +50,8 @@ class MagicNumberRule(configRules: List) : DiktatRule( val isHashFunction = node.parent({ it.elementType == FUN && it.isHashFun() }) != null val isConstant = node.parent({ it.elementType == PROPERTY && it.isConstant() }) != null val isPropertyDeclaration = !isConstant && node.parent({ it.elementType == PROPERTY && !it.isNodeFromCompanionObject() }) != null - val isLocalVariable = node.parent({ it.isVarProperty() && (it.psi as KtProperty).isLocal }) != null + val isLocalVariable = node.parent({ it.elementType == PROPERTY && it.isVarProperty() && (it.psi as KtProperty).isLocal }) != null + val isValueParameter = node.parent({ it.elementType == VALUE_PARAMETER }) != null val isCompanionObjectProperty = node.parent({ it.elementType == PROPERTY && it.isNodeFromCompanionObject() }) != null val isEnums = node.parent({ it.elementType == ENUM_ENTRY }) != null val isRanges = node.treeParent.run { @@ -58,7 +60,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( } val isExtensionFunctions = node.parent({ it.elementType == FUN && (it.psi as KtFunction).isExtensionDeclaration() }) != null && node.parents().find { it.elementType == PROPERTY } == null - val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isConstant, + val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isValueParameter, isConstant, isCompanionObjectProperty, isEnums, isRanges, isExtensionFunctions).zip(mapConfiguration.map { configuration.getParameter(it.key) }) if (result.any { it.first && it.first != it.second } && !isIgnoreNumber) { MAGIC_NUMBER.warn(configRules, emitWarn, isFixMode, nodeText, node.startOffset, node) @@ -98,6 +100,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( "ignoreHashCodeFunction" to true, "ignorePropertyDeclaration" to false, "ignoreLocalVariableDeclaration" to false, + "ignoreValueParameter" to true, "ignoreConstantDeclaration" to true, "ignoreCompanionObjectPropertyDeclaration" to true, "ignoreEnums" to false, diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt index e68bb1c44c..6b6359f8b5 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt @@ -29,6 +29,7 @@ import com.pinterest.ktlint.core.ast.ElementType.LBRACE import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.ElementType.OVERRIDE_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.PRIVATE_KEYWORD +import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.PROTECTED_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.PUBLIC_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index e49788b609..9354b65640 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -312,6 +312,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt index eddea55859..baffac256c 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt @@ -20,6 +20,7 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { "ignoreHashCodeFunction" to "true", "ignorePropertyDeclaration" to "true", "ignoreLocalVariableDeclaration" to "true", + "ignoreValueParameter" to "false", "ignoreConstantDeclaration" to "true", "ignoreCompanionObjectPropertyDeclaration" to "true", "ignoreEnums" to "true", @@ -142,4 +143,42 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { """.trimMargin(), rulesConfigList = rulesConfigMagicNumber ) } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check value parameter`() { + lintMethod( + """ + class TomlDecoder( + var elementsCount: Int = 100 + ) + """.trimMargin(), + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check value parameter with config`() { + lintMethod( + """ + class TomlDecoder( + var elementsCount: Int = 100 + ) + """.trimMargin(), + LintError(2, 46, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), + rulesConfigList = rulesConfigMagicNumber + ) + } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check value parameter in function with config`() { + lintMethod( + """ + fun TomlDecoder(elementsCount: Int = 100) {} + """.trimMargin(), + LintError(1, 54, ruleId, "${MAGIC_NUMBER.warnText()} 100", false), + rulesConfigList = rulesConfigMagicNumber + ) + } } diff --git a/examples/maven/diktat-analysis.yml b/examples/maven/diktat-analysis.yml index 3f953a53ed..f11ea46c37 100644 --- a/examples/maven/diktat-analysis.yml +++ b/examples/maven/diktat-analysis.yml @@ -308,6 +308,8 @@ ignorePropertyDeclaration: "false" # Is ignore local variable ignoreLocalVariableDeclaration: "false" + # Is ignore value parameter + ignoreValueParameter: "true" # Is ignore constant ignoreConstantDeclaration: "true" # Is ignore property in companion object