Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type alias wrong trigger #319

Merged
merged 4 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +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.FUN
import com.pinterest.ktlint.core.ast.ElementType.LT
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_LIST
import com.pinterest.ktlint.core.ast.ElementType.TYPEALIAS
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
Expand All @@ -10,6 +15,7 @@ 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
import org.jetbrains.kotlin.psi.psiUtil.parents

/**
* This rule checks if variable has long type reference and two or more nested generics.
Expand All @@ -30,7 +36,7 @@ class TypeAliasRule(private val configRules: List<RulesConfig>) : Rule("type-ali
emitWarn = emit
isFixMode = autoCorrect

if (node.elementType == TYPE_REFERENCE) {
if (node.elementType == TYPE_REFERENCE && node.parents().map { it.elementType }.none { it == SUPER_TYPE_LIST ||it == TYPEALIAS}) {
checkTypeReference(node, TypeAliasConfiguration(configRules.getRuleConfig(TYPE_ALIAS)?.configuration ?: mapOf()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) {
| val b: MutableMap<String, MutableList<String>>
| val b = listof<Int>()
""".trimMargin(),
LintError(1,9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false)
LintError(1, 9, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false)
)
}

Expand All @@ -39,8 +39,8 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) {
| 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(2,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)
)
}

Expand All @@ -56,7 +56,7 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) {
| }
|
""".trimMargin(),
LintError(4,13, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false)
LintError(4, 13, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false)
)
}

Expand All @@ -70,7 +70,60 @@ class TypeAliasRuleWarnTest : LintTestBase(::TypeAliasRule) {
| val list: List<List<Int>>
|
""".trimMargin(),
LintError(3,12, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false),
LintError(3, 12, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false),
rulesConfigList = rulesConfigListShortType
)
}


@Test
@Tag(WarningNames.TYPE_ALIAS)
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
fun `should ignore inheritance`() {
lintMethod(
"""
| class A : JsonResourceConfigReader<List<RulesConfig>>() {
| fun foo() : JsonResourceConfigReader<List<RulesConfig>> {}
| val q: JsonResourceConfigReader<List<RulesConfig>>? = null
| fun goo() {
| class B : JsonResourceConfigReader<List<RulesConfig>> {}
| }
| }
""".trimMargin(),
LintError(2, 16, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false),
LintError(3, 11, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false)
)
}

@Test
@Tag(WarningNames.TYPE_ALIAS)
fun `check correct examle`() {
lintMethod(
"""
|typealias jsonType = JsonResourceConfigReader<List<RulesConfig>>
|class A : JsonResourceConfigReader<List<RulesConfig>>() {
|
| fun foo() : jsonType {}
| val q: jsonType? = null
| fun goo() {
| class B : JsonResourceConfigReader<List<RulesConfig>> {}
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.TYPE_ALIAS)
fun `check lazy property`() {
lintMethod(
"""
|class A {
| val q: List<Map<Int, Int>> by lazy {
| emptyList<Map<Int, Int>>()
| }
|}
""".trimMargin(),
LintError(2, 11, ruleId, "${TYPE_ALIAS.warnText()} too long type reference", false),
rulesConfigList = rulesConfigListShortType
)
}
Expand Down