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

Kotlin version #730

Merged
merged 17 commits into from
Feb 1, 2021
2 changes: 2 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
domainName: org.cqfn.diktat
# testDirs: test
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
disabledChapters: ""
testDirs: test
kotlinVersion: "1.4.10"
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
# Checks that the Class/Enum/Interface name does not match Pascal case
- name: CLASS_NAME_INCORRECT
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.cqfn.diktat.common.config.rules

import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
import org.cqfn.diktat.common.config.rules.RulesConfigReader.Companion.log

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
Expand Down Expand Up @@ -123,6 +124,16 @@ data class CommonConfiguration(private val configuration: Map<String, String>?)
configuration?.get("disabledChapters")
}

/**
* Get version of kotlin from configuration
*/
val kotlinVersion: KotlinVersion by lazy {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
configuration?.get("kotlinVersion")?.kotlinVersion() ?: run {
log.error("Kotlin version not specified. Will be use current version")
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
KotlinVersion.CURRENT
}
}

/**
* False if configuration has been read from config file, true if defaults are used
*/
Expand Down Expand Up @@ -155,3 +166,20 @@ fun List<RulesConfig>.isRuleEnabled(rule: Rule): Boolean {
val ruleMatched = getRuleConfig(rule)
return ruleMatched?.enabled ?: true
}

/**
* Parse string into KotlinVersion
*
* @return KotlinVersion from configuration
*/
fun String.kotlinVersion(): KotlinVersion {
require(this.contains("^(\\d+\\.)(\\d+)\\.?(\\d+)?$".toRegex())) {
"Kotlin version format is incorrect"
}
val versions = this.split(".").map { it.toInt() }
return if (versions.size == 2) {
KotlinVersion(versions[0], versions[1])
} else {
KotlinVersion(versions[0], versions[1], versions[2])
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.ruleset.rules.chapter6.classes

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.constants.Warnings.INLINE_CLASS_CAN_BE_USED
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
Expand Down Expand Up @@ -37,7 +38,8 @@ class InlineClassesRule(private val configRule: List<RulesConfig>) : Rule("inlin
emitWarn = emit
isFixMode = autoCorrect

if (node.elementType == CLASS) {
val configuration by configRule.getCommonConfiguration()
if (node.elementType == CLASS && configuration.kotlinVersion.isAtLeast(ktVersion.major, ktVersion.minor, ktVersion.patch)) {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
handleClasses(node.psi as KtClass)
}
}
Expand Down Expand Up @@ -72,6 +74,7 @@ class InlineClassesRule(private val configRule: List<RulesConfig>) : Rule("inlin
?: false

companion object {
val ktVersion = KotlinVersion(1, 4, 10)
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
val goodModifiers = listOf(PUBLIC_KEYWORD, PRIVATE_KEYWORD, FINAL_KEYWORD, PROTECTED_KEYWORD, INTERNAL_KEYWORD)
}
}
2 changes: 2 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
domainName: com.huawei
# testDirs: test
disabledChapters: ""
testDirs: test
kotlinVersion: "1.4.10"
# Checks that the Class/Enum/Interface name does not match Pascal case
- name: CLASS_NAME_INCORRECT
enabled: true
Expand Down
1 change: 1 addition & 0 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
domainName: your.name.here
testDirs: test
disabledChapters: ""
kotlinVersion: "1.4.10"
# Checks that the Class/Enum/Interface name does not match Pascal case
- name: CLASS_NAME_INCORRECT
enabled: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cqfn.diktat.ruleset.chapter6

import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.INLINE_CLASS_CAN_BE_USED
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter6.classes.InlineClassesRule
Expand All @@ -12,6 +14,21 @@ import org.junit.jupiter.api.Test

class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
private val ruleId = "$DIKTAT_RULE_SET_ID:inline-classes"
private val rulesConfigListEarlierVersion: List<RulesConfig> = listOf(
RulesConfig(
DIKTAT_COMMON, true,
mapOf("kotlinVersion" to "1.4.9"))
)
private val rulesConfigListSameVersion: List<RulesConfig> = listOf(
RulesConfig(
DIKTAT_COMMON, true,
mapOf("kotlinVersion" to "1.4.10"))
)
private val rulesConfigListLateVersion: List<RulesConfig> = listOf(
RulesConfig(
DIKTAT_COMMON, true,
mapOf("kotlinVersion" to "1.4.11"))
)

@Test
@Tag(WarningNames.INLINE_CLASS_CAN_BE_USED)
Expand Down Expand Up @@ -160,4 +177,37 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class LocalCommandExecutor", true)
)
}

@Test
@Tag(WarningNames.INLINE_CLASS_CAN_BE_USED)
fun `check kotlin version`() {
lintMethod(
"""
|class Some {
| val config = Config()
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true),
rulesConfigList = rulesConfigListLateVersion
)

lintMethod(
"""
|class Some {
| val config = Config()
|}
""".trimMargin(),
rulesConfigList = rulesConfigListEarlierVersion
)

lintMethod(
"""
|class Some {
| val config = Config()
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true),
rulesConfigList = rulesConfigListSameVersion
)
}
}