From eecc1828732f449985fad6d8820fd97b5e5ba1b5 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 15 Sep 2020 14:35:27 +0300 Subject: [PATCH 1/7] Recommendation 4.2.2 type alias ### What's done: Added new rule --- .../cqfn/diktat/ruleset/constants/Warnings.kt | 1 + .../ruleset/rules/DiktatRuleSetProvider.kt | 1 + .../diktat/ruleset/rules/TypeAliasRule.kt | 44 +++++++++++++++++++ .../src/main/resources/diktat-analysis.yml | 7 ++- .../ruleset/chapter3/TypeAliasRuleWarnTest.kt | 24 ++++++++++ info/available-rules.md | 1 + 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt create mode 100644 diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt index fb2490da07..c58f31c4e9 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt @@ -89,6 +89,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S WRONG_DECLARATIONS_ORDER(true, "declarations of constants and enum members should be sorted alphabetically"), WRONG_MULTIPLE_MODIFIERS_ORDER(true, "sequence of modifiers is incorrect"), LOCAL_VARIABLE_EARLY_DECLARATION(false, "local variables should be declared close to the line where they are first used"), + TYPE_ALIAS(true, "variable can replace with type alias"), ; /** diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt index 8a1d4b4414..ca93e05dfa 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt @@ -49,6 +49,7 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy ::SortRule, ::StringConcatenationRule, ::LineLength, + ::TypeAliasRule, ::BlankLinesRule, ::WhiteSpaceRule, ::WhenMustHaveElseRule, diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt new file mode 100644 index 0000000000..23a8f92094 --- /dev/null +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -0,0 +1,44 @@ +package org.cqfn.diktat.ruleset.rules + +import com.pinterest.ktlint.core.Rule +import com.pinterest.ktlint.core.ast.ElementType.FILE +import com.pinterest.ktlint.core.ast.ElementType.PROPERTY +import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE +import org.cqfn.diktat.common.config.rules.RuleConfiguration +import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.common.config.rules.getRuleConfig +import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS +import org.cqfn.diktat.ruleset.utils.hasChildOfType +import org.cqfn.diktat.ruleset.utils.prettyPrint +import org.jetbrains.kotlin.com.intellij.lang.ASTNode + +class TypeAliasRule(private val configRules: List) : Rule("type-alias") { + + companion object { + const val TYPE_REFERENCE_MAX_LENGTH = 25 + } + + private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) + private var isFixMode: Boolean = false + + override fun visit(node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { + emitWarn = emit + isFixMode = autoCorrect + + val config = TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf()) + if (node.elementType == PROPERTY) + checkProperty(node, config) + } + + private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { + val typeReference = node.findChildByType(TYPE_REFERENCE) ?: return + if (typeReference.textLength > config.typeReferenceLength) + TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference, can be replace by type alias", typeReference.startOffset) + } + + class TypeAliasConfiguration(config: Map) : RuleConfiguration(config) { + val typeReferenceLength = config["typeReferenceLength"]?.toIntOrNull() ?: TYPE_REFERENCE_MAX_LENGTH + } +} \ No newline at end of file diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index c3e9b55351..6d4d0d626a 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -234,4 +234,9 @@ # Inspection that checks if local variables are declared close to the first usage site - name: LOCAL_VARIABLE_EARLY_DECLARATION enabled: true - configuration: {} \ No newline at end of file + configuration: {} +# Type aliases provide alternative names for existing types when type's reference text is longer 25 chars +- name: TYPE_ALIAS + enabled: true + configuration: + typeReferenceLength: '25' # max length of type reference \ No newline at end of file diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt new file mode 100644 index 0000000000..8675df6843 --- /dev/null +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt @@ -0,0 +1,24 @@ +package org.cqfn.diktat.ruleset.chapter3 + +import com.pinterest.ktlint.core.LintError +import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS +import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID +import org.cqfn.diktat.ruleset.rules.TypeAliasRule +import org.cqfn.diktat.util.LintTestBase +import org.junit.jupiter.api.Test + +class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { + + private val ruleId = "$DIKTAT_RULE_SET_ID:type-alias" + + @Test + fun `string concatenation - only strings`() { + lintMethod( + """ + | val b: MutableMap> + | val b = listof() + """.trimMargin(), + LintError(1,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference, can be replace by type alias", false) + ) + } +} \ No newline at end of file diff --git a/info/available-rules.md b/info/available-rules.md index 08cfc83bbf..da936f5c4e 100644 --- a/info/available-rules.md +++ b/info/available-rules.md @@ -71,3 +71,4 @@ | 3 | 3.2 | WRONG_DECLARATIONS_ORDER | Check: if order of enum values or constant property inside companion isn't correct | yes | - | - | | 3 | 3.11 | ANNOTATION_NEW_LINE | Check: warns if annotation not on a new single line | yes | - | - | | 3 | 3.2 | WRONG_MULTIPLE_MODIFIERS_ORDER | Check: if multiple modifiers sequence is in the wrong order | yes | - | - | +| 4 | 4.2.2| TYPE_ALIAS | Check: if type reference of property is longer than expected | yes | typeReferenceLength | -| From 4f5d5b6ae561acddf3b901ed8abaa8222e5d7092 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 15 Sep 2020 15:09:20 +0300 Subject: [PATCH 2/7] Recommendation 4.2.2 type alias ### What's done: Added soe tests Changed warn's message --- .../cqfn/diktat/ruleset/constants/Warnings.kt | 2 +- .../diktat/ruleset/rules/TypeAliasRule.kt | 7 +-- .../ruleset/chapter3/TypeAliasRuleWarnTest.kt | 48 ++++++++++++++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt index c58f31c4e9..821d315fca 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt @@ -89,7 +89,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S WRONG_DECLARATIONS_ORDER(true, "declarations of constants and enum members should be sorted alphabetically"), WRONG_MULTIPLE_MODIFIERS_ORDER(true, "sequence of modifiers is incorrect"), LOCAL_VARIABLE_EARLY_DECLARATION(false, "local variables should be declared close to the line where they are first used"), - TYPE_ALIAS(true, "variable can replace with type alias"), + TYPE_ALIAS(false, "variable's type reference can replace with typealias"), ; /** diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt index 23a8f92094..03d68106ab 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -1,15 +1,12 @@ package org.cqfn.diktat.ruleset.rules import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS -import org.cqfn.diktat.ruleset.utils.hasChildOfType -import org.cqfn.diktat.ruleset.utils.prettyPrint import org.jetbrains.kotlin.com.intellij.lang.ASTNode class TypeAliasRule(private val configRules: List) : Rule("type-alias") { @@ -35,10 +32,10 @@ class TypeAliasRule(private val configRules: List) : Rule("type-ali private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { val typeReference = node.findChildByType(TYPE_REFERENCE) ?: return if (typeReference.textLength > config.typeReferenceLength) - TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference, can be replace by type alias", typeReference.startOffset) + TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", typeReference.startOffset) } class TypeAliasConfiguration(config: Map) : RuleConfiguration(config) { val typeReferenceLength = config["typeReferenceLength"]?.toIntOrNull() ?: TYPE_REFERENCE_MAX_LENGTH } -} \ No newline at end of file +} diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt index 8675df6843..c0ba6f7511 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt @@ -1,6 +1,7 @@ package org.cqfn.diktat.ruleset.chapter3 import com.pinterest.ktlint.core.LintError +import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID import org.cqfn.diktat.ruleset.rules.TypeAliasRule @@ -11,6 +12,11 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { private val ruleId = "$DIKTAT_RULE_SET_ID:type-alias" + private val rulesConfigListShortType: List = listOf( + RulesConfig(TYPE_ALIAS.name, true, + mapOf("typeReferenceLength" to "4")) + ) + @Test fun `string concatenation - only strings`() { lintMethod( @@ -18,7 +24,45 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { | val b: MutableMap> | val b = listof() """.trimMargin(), - LintError(1,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference, can be replace by type alias", false) + LintError(1,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false) + ) + } + + @Test + fun `check long lambda property`() { + lintMethod( + """ + | var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) + """.trimMargin(), + LintError(1,16, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false) + ) + } + + @Test + fun `correct type length`() { + lintMethod( + """ + | var emitWarn: Int + | val b = mutableMapOf>() + | + | fun foo(): MutableMap> { + | } + | + """.trimMargin() + ) + } + + @Test + fun `correct type length but with configuration`() { + lintMethod( + """ + | var emitWarn: Int + | val b: (T) -> Boolean + | + """.trimMargin(), + LintError(2,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false), + rulesConfigList = rulesConfigListShortType ) } -} \ No newline at end of file + +} From 170b13506a46473862cfeb8ee0dfcda988d1ed51 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 15 Sep 2020 16:33:27 +0300 Subject: [PATCH 3/7] Recommendation 4.2.2 type alias ### What's done: Added handler for generic count Added tests --- .../cqfn/diktat/ruleset/constants/Warnings.kt | 2 +- .../cqfn/diktat/ruleset/rules/TypeAliasRule.kt | 16 +++++++++------- .../ruleset/chapter3/TypeAliasRuleWarnTest.kt | 15 +++++++++------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt index 821d315fca..3161e9f78e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt @@ -89,7 +89,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S WRONG_DECLARATIONS_ORDER(true, "declarations of constants and enum members should be sorted alphabetically"), WRONG_MULTIPLE_MODIFIERS_ORDER(true, "sequence of modifiers is incorrect"), LOCAL_VARIABLE_EARLY_DECLARATION(false, "local variables should be declared close to the line where they are first used"), - TYPE_ALIAS(false, "variable's type reference can replace with typealias"), + TYPE_ALIAS(false, "variable's type is too complex and should be replaced with typealias"), ; /** diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt index 03d68106ab..a4e83af1f6 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -1,12 +1,14 @@ package org.cqfn.diktat.ruleset.rules import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.ast.ElementType.PROPERTY +import com.pinterest.ktlint.core.ast.ElementType.LT import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE +import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS +import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode class TypeAliasRule(private val configRules: List) : Rule("type-alias") { @@ -24,15 +26,15 @@ class TypeAliasRule(private val configRules: List) : Rule("type-ali emitWarn = emit isFixMode = autoCorrect - val config = TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf()) - if (node.elementType == PROPERTY) - checkProperty(node, config) + if (node.elementType == TYPE_REFERENCE) { + checkProperty(node, TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf())) + } } private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { - val typeReference = node.findChildByType(TYPE_REFERENCE) ?: return - if (typeReference.textLength > config.typeReferenceLength) - TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", typeReference.startOffset) + if (node.textLength > config.typeReferenceLength) + if (node.findAllNodesWithSpecificType(LT).size > 1 || node.findAllNodesWithSpecificType(VALUE_PARAMETER).size > 1) + TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", node.startOffset) } class TypeAliasConfiguration(config: Map) : RuleConfiguration(config) { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt index c0ba6f7511..739e385dbb 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt @@ -32,9 +32,11 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { fun `check long lambda property`() { lintMethod( """ - | var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) + | var emitWarn: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + | var emitWarn: (offset: Int, (T) -> Boolean) -> Unit """.trimMargin(), - LintError(1,16, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false) + LintError(1,16, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false), + LintError(2,16, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false) ) } @@ -48,7 +50,8 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { | fun foo(): MutableMap> { | } | - """.trimMargin() + """.trimMargin(), + LintError(4,13, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false) ) } @@ -57,12 +60,12 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { lintMethod( """ | var emitWarn: Int - | val b: (T) -> Boolean + | val flag: (T) -> Boolean + | val list: List> | """.trimMargin(), - LintError(2,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false), + LintError(3,12, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false), rulesConfigList = rulesConfigListShortType ) } - } From 6b21aed52d2916ff032e6452cf86a601d03d884f Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 15 Sep 2020 19:02:17 +0300 Subject: [PATCH 4/7] Recommendation 4.2.2 type alias ### What's done: Added tags for tests --- diktat-rules/src/main/kotlin/generated/WarningNames.kt | 2 ++ .../cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/diktat-rules/src/main/kotlin/generated/WarningNames.kt b/diktat-rules/src/main/kotlin/generated/WarningNames.kt index 74d2c5e415..de04689d7d 100644 --- a/diktat-rules/src/main/kotlin/generated/WarningNames.kt +++ b/diktat-rules/src/main/kotlin/generated/WarningNames.kt @@ -147,4 +147,6 @@ object WarningNames { const val WRONG_MULTIPLE_MODIFIERS_ORDER: String = "WRONG_MULTIPLE_MODIFIERS_ORDER" const val LOCAL_VARIABLE_EARLY_DECLARATION: String = "LOCAL_VARIABLE_EARLY_DECLARATION" + + const val TYPE_ALIAS: String = "TYPE_ALIAS" } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt index 739e385dbb..9e294844d6 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/TypeAliasRuleWarnTest.kt @@ -1,11 +1,13 @@ package org.cqfn.diktat.ruleset.chapter3 import com.pinterest.ktlint.core.LintError +import generated.WarningNames import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID import org.cqfn.diktat.ruleset.rules.TypeAliasRule import org.cqfn.diktat.util.LintTestBase +import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { @@ -18,6 +20,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { ) @Test + @Tag(WarningNames.TYPE_ALIAS) fun `string concatenation - only strings`() { lintMethod( """ @@ -29,6 +32,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { } @Test + @Tag(WarningNames.TYPE_ALIAS) fun `check long lambda property`() { lintMethod( """ @@ -41,6 +45,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { } @Test + @Tag(WarningNames.TYPE_ALIAS) fun `correct type length`() { lintMethod( """ @@ -56,6 +61,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) { } @Test + @Tag(WarningNames.TYPE_ALIAS) fun `correct type length but with configuration`() { lintMethod( """ From b99430c513e76dac781cad29505092885fb82d5b Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 15 Sep 2020 19:09:12 +0300 Subject: [PATCH 5/7] Recommendation 4.2.2 type alias ### What's done: Fixed after merged --- .../main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt index a4e83af1f6..2b70f0b31c 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -34,7 +34,7 @@ class TypeAliasRule(private val configRules: List) : Rule("type-ali private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { if (node.textLength > config.typeReferenceLength) if (node.findAllNodesWithSpecificType(LT).size > 1 || node.findAllNodesWithSpecificType(VALUE_PARAMETER).size > 1) - TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", node.startOffset) + TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", node.startOffset, node) } class TypeAliasConfiguration(config: Map) : RuleConfiguration(config) { From a16ee8609593953e09e4b3ada4b0d6d90d57e699 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Wed, 16 Sep 2020 14:30:59 +0300 Subject: [PATCH 6/7] Recommendation 4.3.1 type-alias ### What's done: Added comments --- .../main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt index 2b70f0b31c..19c2eb6442 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -31,6 +31,9 @@ class TypeAliasRule(private val configRules: List) : Rule("type-ali } } + /** + * Check properties for nested generics. Count LT for generic types and VALUE_PARAMETER for functional types + */ private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { if (node.textLength > config.typeReferenceLength) if (node.findAllNodesWithSpecificType(LT).size > 1 || node.findAllNodesWithSpecificType(VALUE_PARAMETER).size > 1) From fa4912104f5218d03c8c368d49bc17ec0a7868e5 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Thu, 17 Sep 2020 17:38:13 +0300 Subject: [PATCH 7/7] Recommendation 4.2.2 type alias ### What's done: Added KDoc Added to another ymls file --- diktat-analysis.yml | 7 ++++++- .../kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt | 8 ++++++-- .../src/main/resources/diktat-analysis-huawei.yml | 7 ++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index e5ff59780d..c92be347a0 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -230,4 +230,9 @@ configuration: {} - name: LOCAL_VARIABLE_EARLY_DECLARATION enabled: true - configuration: {} \ No newline at end of file + configuration: {} +# Type aliases provide alternative names for existing types when type's reference text is longer 25 chars +- name: TYPE_ALIAS + enabled: true + configuration: + typeReferenceLength: '25' # max length of type reference \ No newline at end of file diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt index 19c2eb6442..8fd6d06cf3 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/TypeAliasRule.kt @@ -11,6 +11,10 @@ import org.cqfn.diktat.ruleset.constants.Warnings.TYPE_ALIAS import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode +/** + * This rule checks if variable has long type reference and two or more nested generics. + * Length type reference can be configured + */ class TypeAliasRule(private val configRules: List) : Rule("type-alias") { companion object { @@ -27,14 +31,14 @@ class TypeAliasRule(private val configRules: List) : Rule("type-ali isFixMode = autoCorrect if (node.elementType == TYPE_REFERENCE) { - checkProperty(node, TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf())) + checkTypeReference(node, TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf())) } } /** * Check properties for nested generics. Count LT for generic types and VALUE_PARAMETER for functional types */ - private fun checkProperty(node: ASTNode, config: TypeAliasConfiguration) { + private fun checkTypeReference(node: ASTNode, config: TypeAliasConfiguration) { if (node.textLength > config.typeReferenceLength) if (node.findAllNodesWithSpecificType(LT).size > 1 || node.findAllNodesWithSpecificType(VALUE_PARAMETER).size > 1) TYPE_ALIAS.warn(configRules, emitWarn, isFixMode, "too long type reference", node.startOffset, node) diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index 691b282e55..befbf9d4bf 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -230,4 +230,9 @@ configuration: {} - name: LOCAL_VARIABLE_EARLY_DECLARATION enabled: true - configuration: {} \ No newline at end of file + configuration: {} +# Type aliases provide alternative names for existing types when type's reference text is longer 25 chars +- name: TYPE_ALIAS + enabled: true + configuration: + typeReferenceLength: '25' # max length of type reference \ No newline at end of file