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
Expand Up @@ -152,7 +152,7 @@ enum class Warnings(
AVOID_USING_UTILITY_CLASS(false, "6.4.1", "avoid using utility classes/objects, use extensions functions"),
OBJECT_IS_PREFERRED(true, "6.4.2", "it is better to use object for stateless classes"),
INVERSE_FUNCTION_PREFERRED(true, "5.1.4", "it is better to use inverse function"),
INLINE_CLASS_CAN_BE_USED(true, "6.1.12", "inline class can be used"),
INLINE_CLASS_CAN_BE_USED(false, "6.1.12", "inline class can be used"),
;

/**
Expand Down
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 >= ktVersion) {
handleClasses(node.psi as KtClass)
}
}
Expand All @@ -47,9 +49,8 @@ class InlineClassesRule(private val configRule: List<RulesConfig>) : Rule("inlin
if (hasValidProperties(classPsi) &&
!isExtendingClass(classPsi.node) &&
classPsi.node.getFirstChildWithType(MODIFIER_LIST)?.getChildren(null)?.all { it.elementType in goodModifiers } != false) {
INLINE_CLASS_CAN_BE_USED.warnAndFix(configRule, emitWarn, isFixMode, "class ${classPsi.name}", classPsi.node.startOffset, classPsi.node) {
// Fixme: since it's an experimental feature we shouldn't do fixer
}
// Fixme: since it's an experimental feature we shouldn't do fixer
INLINE_CLASS_CAN_BE_USED.warn(configRule, emitWarn, isFixMode, "class ${classPsi.name}", classPsi.node.startOffset, classPsi.node)
}
}

Expand All @@ -72,6 +73,7 @@ class InlineClassesRule(private val configRule: List<RulesConfig>) : Rule("inlin
?: false

companion object {
val ktVersion = KotlinVersion(1, 3)
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.2.9"))
)
private val rulesConfigListSameVersion: List<RulesConfig> = listOf(
RulesConfig(
DIKTAT_COMMON, true,
mapOf("kotlinVersion" to "1.3"))
)
private val rulesConfigListLateVersion: List<RulesConfig> = listOf(
RulesConfig(
DIKTAT_COMMON, true,
mapOf("kotlinVersion" to "1.3.1"))
)

@Test
@Tag(WarningNames.INLINE_CLASS_CAN_BE_USED)
Expand All @@ -32,7 +49,7 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
| val config = Config()
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", false)
)
}

Expand All @@ -45,7 +62,7 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
| val config = Config()
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", false)
)
}

Expand All @@ -70,7 +87,7 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
|
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", false)
)
}

Expand Down Expand Up @@ -119,7 +136,7 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
| val some = 3
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class Some", false)
)
}

Expand All @@ -144,7 +161,7 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
|
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class LocalCommandExecutor", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class LocalCommandExecutor", false)
)
}

Expand All @@ -157,7 +174,40 @@ class InlineClassesWarnTest : LintTestBase(::InlineClassesRule) {
|
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class LocalCommandExecutor", true)
LintError(1, 1, ruleId, "${INLINE_CLASS_CAN_BE_USED.warnText()} class LocalCommandExecutor", false)
)
}

@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", false),
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", false),
rulesConfigList = rulesConfigListSameVersion
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
|
| http://www.apache.org/licenses/LICENSE-2.0
""".trimMargin()
),
DIKTAT_COMMON to mapOf(
"domainName" to "org.cqfn.diktat",
"kotlinVersion" to "1.3.7"
)
)
)
Expand All @@ -127,6 +131,10 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
Assertions.assertFalse(
unfixedLintErrors.contains(LintError(line = 1, col = 1, ruleId = "diktat-ruleset:comments", detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*"))
)

Assertions.assertTrue(
unfixedLintErrors.contains(LintError(1, 1, "diktat-ruleset:inline-classes", "${Warnings.INLINE_CLASS_CAN_BE_USED.warnText()} class Some"))
)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
package org.cqfn.diktat

class Some {

val config = Config()
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.cqfn.diktat

class Some {

val config = Config()
}