From 69ddcee36c84897b8af9d3fb462271fd7fee9990 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 12 Jan 2021 13:08:17 +0300 Subject: [PATCH 01/10] Warning for domain name ### What's done: Created new warning, tests and documentations --- diktat-analysis.yml | 3 ++ .../common/config/rules/RulesConfigReader.kt | 4 +-- .../src/main/kotlin/generated/WarningNames.kt | 2 ++ .../cqfn/diktat/ruleset/constants/Warnings.kt | 1 + .../diktat/ruleset/rules/PackageNaming.kt | 19 +++++++------ .../ruleset/rules/files/FileStructureRule.kt | 22 +++++++++------ .../main/resources/diktat-analysis-huawei.yml | 3 ++ .../src/main/resources/diktat-analysis.yml | 3 ++ .../ruleset/chapter1/PackageNamingWarnTest.kt | 28 +++++++++++++++++++ .../ruleset/chapter3/FileStructureRuleTest.kt | 18 ++++++++++++ info/available-rules.md | 1 + info/guide/guide-chapter-1.md | 2 ++ 12 files changed, 87 insertions(+), 19 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index d6f4e18929..31aaf9dfee 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -43,6 +43,9 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true +# Checks that domain name in DIKTAT_COMMON missing +- name: MISSING_DOMAIN_NAME + enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index 376305439e..7a271b8483 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -112,8 +112,8 @@ data class CommonConfiguration(private val configuration: Map?) /** * Start of package name, which shoould be common, e.g. org.example.myproject */ - val domainName: String by lazy { - (configuration ?: emptyMap()).getOrDefault("domainName", "") + val domainName: String? by lazy { + (configuration ?: emptyMap())["domainName"] } /** diff --git a/diktat-rules/src/main/kotlin/generated/WarningNames.kt b/diktat-rules/src/main/kotlin/generated/WarningNames.kt index 10842cf258..205266bc7b 100644 --- a/diktat-rules/src/main/kotlin/generated/WarningNames.kt +++ b/diktat-rules/src/main/kotlin/generated/WarningNames.kt @@ -5,6 +5,8 @@ package generated import kotlin.String public object WarningNames { + public const val MISSING_DOMAIN_NAME: String = "MISSING_DOMAIN_NAME" + public const val PACKAGE_NAME_MISSING: String = "PACKAGE_NAME_MISSING" public const val PACKAGE_NAME_INCORRECT_CASE: String = "PACKAGE_NAME_INCORRECT_CASE" 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 156d662dae..3ed82dba00 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 @@ -22,6 +22,7 @@ enum class Warnings( val ruleId: String, private val warn: String) : Rule { // ======== chapter 1 ======== + MISSING_DOMAIN_NAME(false, "1.2.1", "missing domain name in config file"), PACKAGE_NAME_MISSING(true, "1.2.1", "no package name declared in a file"), PACKAGE_NAME_INCORRECT_CASE(true, "1.2.1", "package name should be completely in a lower case"), PACKAGE_NAME_INCORRECT_PREFIX(true, "1.2.1", "package name should start from company's domain"), diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index 7be9e7c55f..8689e49c82 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -4,6 +4,7 @@ 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.INCORRECT_PACKAGE_SEPARATOR +import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_DOMAIN_NAME import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX @@ -37,7 +38,7 @@ import java.util.concurrent.atomic.AtomicInteger class PackageNaming(private val configRules: List) : Rule("package-naming") { private var isFixMode: Boolean = false private lateinit var emitWarn: EmitType - private lateinit var domainName: String + private var domainName: String? = null override fun visit(node: ASTNode, autoCorrect: Boolean, @@ -48,13 +49,15 @@ class PackageNaming(private val configRules: List) : Rule("package- val configuration by configRules.getCommonConfiguration() domainName = configuration.also { if (it.isDefault && visitorCounter.incrementAndGet() == 1) { - log.error("Not able to find an external configuration for domain name in the common" + - " configuration (is it missing in yml config?)") + log.error("Not able to find configuration file") } } .domainName - - if (node.elementType == PACKAGE_DIRECTIVE) { + if (domainName == null) { + if (visitorCounter.get() == 1) + MISSING_DOMAIN_NAME.warn(configRules, emitWarn, isFixMode, "No domain name", + node.startOffset, node) + } else if (node.elementType == PACKAGE_DIRECTIVE) { val filePath = node.getRootNode().getFilePath() // calculating package name based on the directory where the file is placed val realPackageName = calculateRealPackageName(filePath) @@ -113,7 +116,7 @@ class PackageNaming(private val configRules: List) : Rule("package- val fileSubDir = filePathParts.subList(filePathParts.lastIndexOf(PACKAGE_PATH_ANCHOR), filePathParts.size - 1) .dropWhile { languageDirNames.contains(it) } // no need to add DOMAIN_NAME to the package name if it is already in path - val domainPrefix = if (!fileSubDir.joinToString(PACKAGE_SEPARATOR).startsWith(domainName)) domainName.split(PACKAGE_SEPARATOR) else emptyList() + val domainPrefix = if (!fileSubDir.joinToString(PACKAGE_SEPARATOR).startsWith(domainName!!)) domainName!!.split(PACKAGE_SEPARATOR) else emptyList() domainPrefix + fileSubDir } } @@ -130,7 +133,7 @@ class PackageNaming(private val configRules: List) : Rule("package- // package name should start from a company's domain name if (wordsInPackageName.isNotEmpty() && !isDomainMatches(wordsInPackageName)) { - PACKAGE_NAME_INCORRECT_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, domainName, + PACKAGE_NAME_INCORRECT_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, domainName!!, wordsInPackageName[0].startOffset, wordsInPackageName[0]) { val oldPackageName = wordsInPackageName.joinToString(PACKAGE_SEPARATOR) { it.text } val newPackageName = "$domainName$PACKAGE_SEPARATOR$oldPackageName" @@ -185,7 +188,7 @@ class PackageNaming(private val configRules: List) : Rule("package- * function simply checks that package name starts with a proper domain name */ private fun isDomainMatches(packageNameParts: List): Boolean { - val packageNamePrefix = domainName.split(PACKAGE_SEPARATOR) + val packageNamePrefix = domainName!!.split(PACKAGE_SEPARATOR) if (packageNameParts.size < packageNamePrefix.size) { return false } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt index 25a85b8819..40e09e8be2 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt @@ -263,20 +263,24 @@ class FileStructureRule(private val configRules: List) : Rule("file else -> (headerKdoc ?: copyrightComment) to firstCodeNode } - @Suppress("TYPE_ALIAS") + @Suppress("TYPE_ALIAS", "UnsafeCallOnNullableType") private fun regroupImports(imports: List): List> { val (android, notAndroid) = imports.partition { it.isStandard(StandardPlatforms.ANDROID) } - val (ownDomain, tmp) = notAndroid.partition { import -> - import - .importPath - ?.fqName - ?.pathSegments() - ?.zip(domainName.split(PACKAGE_SEPARATOR).map(Name::identifier)) - ?.all { it.first == it.second } - ?: false + val (ownDomain, tmp) = if (domainName == null) { + Pair(emptyList(), notAndroid) + } else { + notAndroid.partition { import -> + import + .importPath + ?.fqName + ?.pathSegments() + ?.zip(domainName!!.split(PACKAGE_SEPARATOR).map(Name::identifier)) + ?.all { it.first == it.second } + ?: false + } } val (others, javaAndKotlin) = tmp.partition { diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index 2681f665db..c344f2e008 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -43,6 +43,9 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true +# Checks that domain name in DIKTAT_COMMON missing +- name: MISSING_DOMAIN_NAME + enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index cf353c0ce0..3e98155a65 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -42,6 +42,9 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true +# Checks that domain name in DIKTAT_COMMON missing +- name: MISSING_DOMAIN_NAME + enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index 2a3924d558..e56ffb4bfe 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.chapter1 import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.Warnings.INCORRECT_PACKAGE_SEPARATOR +import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_DOMAIN_NAME import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX @@ -22,6 +23,10 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { private val rulesConfigList: List = listOf( RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "org.cqfn.diktat")) ) + private val rulesConfigListWithoutDomainName: List = emptyList() + private val rulesConfigListEmptyDomainName: List = listOf( + RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "")) + ) @Test @Tag(WarningNames.PACKAGE_NAME_MISSING) @@ -229,4 +234,27 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { rulesConfigList = rulesConfigList ) } + + @Test + @Tag(WarningNames.PACKAGE_NAME_INCORRECT_PATH) + fun `should warn if there is not domain name or it empty`() { + lintMethod( + """ + |package org.cqfn.diktat + """.trimMargin(), + LintError(1, 1, ruleId, "${MISSING_DOMAIN_NAME.warnText()} No domain name", false), + fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", + rulesConfigList = rulesConfigListWithoutDomainName + ) + + lintMethod( + """ + |package org.cqfn.diktat + """.trimMargin(), + LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PREFIX.warnText()} ", true), + LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PATH.warnText()} myProjectMain.kotlin.org.cqfn.diktat.example", true), + fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", + rulesConfigList = rulesConfigListEmptyDomainName + ) + } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt index 480c269d8e..d480449c74 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt @@ -25,6 +25,7 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { RulesConfig(FILE_WILDCARD_IMPORTS.name, true, mapOf("allowedWildcards" to "org.cqfn.diktat.example.*, org.cqfn.diktat.ruleset.constants.Warnings.*")) ) + private val rulesConfigListWithoutDomainName: List = emptyList() @Test @Tag(WarningNames.FILE_CONTAINS_ONLY_COMMENTS) @@ -226,4 +227,21 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { LintError(4, 1, ruleId, "${FILE_INCORRECT_BLOCKS_ORDER.warnText()} @file:Annotation", true) ) } + + @Test + @Tag(WarningNames.FILE_INCORRECT_BLOCKS_ORDER) + fun `test with no domain name in config`() { + lintMethod( + """ + |package org.cqfn.diktat.example + | + |import org.cqfn.diktat.example.Foo + |import com.pinterest.ktlint.core.LintError + | + |class Example + """.trimMargin(), + LintError(3, 1, ruleId, "${FILE_UNORDERED_IMPORTS.warnText()} import com.pinterest.ktlint.core.LintError...", true), + rulesConfigList = rulesConfigListWithoutDomainName + ) + } } diff --git a/info/available-rules.md b/info/available-rules.md index 085763b398..d3ed22300f 100644 --- a/info/available-rules.md +++ b/info/available-rules.md @@ -1,6 +1,7 @@ | Chap | Standard | Rule name | Description | Fix | Config | FixMe | | ----- | ---------- | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | --- | ------ | ----- | | 1 | 1.1.1 | CONFUSING_IDENTIFIER_NAMING | Check: warns if identifier has inappropriate name (See table of rule 1.2 part 6). | no | no | no | +| 1 | 1.2.1 | MISSING_DOMAIN_NAME | Check: warns if there is no domain name in configuration file | no | no | no | | 1 | 1.2.1 | PACKAGE_NAME_MISSING | Check: warns if package name is missing in a file
Fix: automatically adds package directive with the name that starts from the domain name (in example - com.huawei) and contains the real directory | yes | no | Domain name should not be hardcoded. It should be moved to extra configuration.Recursively fix all imports in project.Fix the directory where the code is stored.Make this check isolated from domain name addition| | 1 | 1.2.1 | PACKAGE_NAME_INCORRECT_CASE | Check: warns if package name is in incorrect (non-lower) case
Fix: automatically update the case in package name | yes | no | Recursively update all imports in the project.| | 1 | 1.2.1 | PACKAGE_NAME_INCORRECT_PREFIX | Check: warns if package name does not start with the company's domain
Fix: automatically update the prefix in the package name | yes | no | Fix the directory where the code is stored.Recursively update all imports in the project.Domain name should not be hardcoded. It should be moved to extra configuration.| diff --git a/info/guide/guide-chapter-1.md b/info/guide/guide-chapter-1.md index ae8237d95d..1e445ca31f 100644 --- a/info/guide/guide-chapter-1.md +++ b/info/guide/guide-chapter-1.md @@ -84,6 +84,8 @@ Package names are all written in lowercase, and consecutive words are concatenat package your.company.domain.mobilecontrol.views ``` +**Note** If there isn't domain name in config file, then package verification will not be carried out. + ### 1.3 Classes, enumerations, interfaces This section describes the general rules for naming classes, enumerations, and interfaces. From cfdfbc03a17d4a9a9de5b7e36580fa8735c02463 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 12 Jan 2021 14:07:55 +0300 Subject: [PATCH 02/10] Warning for domain name ### What's done: Fixed according our code-style --- .../cqfn/diktat/ruleset/rules/PackageNaming.kt | 10 +++++++--- .../ruleset/rules/files/FileStructureRule.kt | 18 ++++++++---------- .../ruleset/chapter1/PackageNamingWarnTest.kt | 14 +++++++------- .../ruleset/chapter3/FileStructureRuleTest.kt | 6 +++--- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index 8689e49c82..a604497d5e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -37,8 +37,8 @@ import java.util.concurrent.atomic.AtomicInteger @Suppress("ForbiddenComment") class PackageNaming(private val configRules: List) : Rule("package-naming") { private var isFixMode: Boolean = false - private lateinit var emitWarn: EmitType private var domainName: String? = null + private lateinit var emitWarn: EmitType override fun visit(node: ASTNode, autoCorrect: Boolean, @@ -54,9 +54,10 @@ class PackageNaming(private val configRules: List) : Rule("package- } .domainName if (domainName == null) { - if (visitorCounter.get() == 1) + if (visitorCounter.get() == 1) { MISSING_DOMAIN_NAME.warn(configRules, emitWarn, isFixMode, "No domain name", - node.startOffset, node) + node.startOffset, node) + } } else if (node.elementType == PACKAGE_DIRECTIVE) { val filePath = node.getRootNode().getFilePath() // calculating package name based on the directory where the file is placed @@ -101,6 +102,7 @@ class PackageNaming(private val configRules: List) : Rule("package- * * @return list with words that are parts of package name like [org, diktat, name] */ + @Suppress("UnsafeCallOnNullableType") private fun calculateRealPackageName(fileName: String): List { val filePathParts = fileName.splitPathToDirs() @@ -121,6 +123,7 @@ class PackageNaming(private val configRules: List) : Rule("package- } } + @Suppress("UnsafeCallOnNullableType") private fun checkPackageName(wordsInPackageName: List, packageDirectiveNode: ASTNode) { // all words should be in a lower case (lower case letters/digits/underscore) wordsInPackageName @@ -187,6 +190,7 @@ class PackageNaming(private val configRules: List) : Rule("package- /** * function simply checks that package name starts with a proper domain name */ + @Suppress("UnsafeCallOnNullableType") private fun isDomainMatches(packageNameParts: List): Boolean { val packageNamePrefix = domainName!!.split(PACKAGE_SEPARATOR) if (packageNameParts.size < packageNamePrefix.size) { diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt index 40e09e8be2..25ee5940e4 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt @@ -269,19 +269,17 @@ class FileStructureRule(private val configRules: List) : Rule("file it.isStandard(StandardPlatforms.ANDROID) } - val (ownDomain, tmp) = if (domainName == null) { - Pair(emptyList(), notAndroid) - } else { + val (ownDomain, tmp) =domainName?.let { notAndroid.partition { import -> import - .importPath - ?.fqName - ?.pathSegments() - ?.zip(domainName!!.split(PACKAGE_SEPARATOR).map(Name::identifier)) - ?.all { it.first == it.second } - ?: false + .importPath + ?.fqName + ?.pathSegments() + ?.zip(domainName!!.split(PACKAGE_SEPARATOR).map(Name::identifier)) + ?.all { it.first == it.second } + ?: false } - } + } ?: Pair(emptyList(), notAndroid) val (others, javaAndKotlin) = tmp.partition { !it.isStandard(StandardPlatforms.JAVA) && !it.isStandard(StandardPlatforms.KOTLIN) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index e56ffb4bfe..e76d83a6c6 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -242,19 +242,19 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { """ |package org.cqfn.diktat """.trimMargin(), - LintError(1, 1, ruleId, "${MISSING_DOMAIN_NAME.warnText()} No domain name", false), - fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", - rulesConfigList = rulesConfigListWithoutDomainName + LintError(1, 1, ruleId, "${MISSING_DOMAIN_NAME.warnText()} No domain name", false), + fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", + rulesConfigList = rulesConfigListWithoutDomainName ) lintMethod( """ |package org.cqfn.diktat """.trimMargin(), - LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PREFIX.warnText()} ", true), - LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PATH.warnText()} myProjectMain.kotlin.org.cqfn.diktat.example", true), - fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", - rulesConfigList = rulesConfigListEmptyDomainName + LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PREFIX.warnText()} ", true), + LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PATH.warnText()} myProjectMain.kotlin.org.cqfn.diktat.example", true), + fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", + rulesConfigList = rulesConfigListEmptyDomainName ) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt index d480449c74..a20dbda4b7 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt @@ -232,7 +232,7 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { @Tag(WarningNames.FILE_INCORRECT_BLOCKS_ORDER) fun `test with no domain name in config`() { lintMethod( - """ + """ |package org.cqfn.diktat.example | |import org.cqfn.diktat.example.Foo @@ -240,8 +240,8 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { | |class Example """.trimMargin(), - LintError(3, 1, ruleId, "${FILE_UNORDERED_IMPORTS.warnText()} import com.pinterest.ktlint.core.LintError...", true), - rulesConfigList = rulesConfigListWithoutDomainName + LintError(3, 1, ruleId, "${FILE_UNORDERED_IMPORTS.warnText()} import com.pinterest.ktlint.core.LintError...", true), + rulesConfigList = rulesConfigListWithoutDomainName ) } } From b4e212beacb6bfcc923ac594409f9c50d5db815b Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 12 Jan 2021 14:19:24 +0300 Subject: [PATCH 03/10] Warning for domain name ### What's done: Fixed according our code-style --- .../org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt | 2 +- .../org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt index 25ee5940e4..a272f77e82 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt @@ -269,7 +269,7 @@ class FileStructureRule(private val configRules: List) : Rule("file it.isStandard(StandardPlatforms.ANDROID) } - val (ownDomain, tmp) =domainName?.let { + val (ownDomain, tmp) = domainName?.let { notAndroid.partition { import -> import .importPath diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index e76d83a6c6..16801fa36e 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -239,7 +239,7 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { @Tag(WarningNames.PACKAGE_NAME_INCORRECT_PATH) fun `should warn if there is not domain name or it empty`() { lintMethod( - """ + """ |package org.cqfn.diktat """.trimMargin(), LintError(1, 1, ruleId, "${MISSING_DOMAIN_NAME.warnText()} No domain name", false), @@ -248,7 +248,7 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { ) lintMethod( - """ + """ |package org.cqfn.diktat """.trimMargin(), LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PREFIX.warnText()} ", true), From 14546591a9b767c7071632ca1dbc366736435565 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Wed, 13 Jan 2021 11:03:33 +0300 Subject: [PATCH 04/10] Warning for domain name ### What's done: Fixed after review --- diktat-analysis.yml | 3 - .../common/config/rules/RulesConfigReader.kt | 2 +- .../cqfn/diktat/ruleset/constants/Warnings.kt | 1 - .../diktat/ruleset/rules/PackageNaming.kt | 58 +++++++++---------- .../main/resources/diktat-analysis-huawei.yml | 3 - .../src/main/resources/diktat-analysis.yml | 3 - .../ruleset/chapter1/PackageNamingWarnTest.kt | 12 +--- info/available-rules.md | 1 - info/guide/guide-chapter-1.md | 2 - 9 files changed, 29 insertions(+), 56 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index 31aaf9dfee..d6f4e18929 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -43,9 +43,6 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true -# Checks that domain name in DIKTAT_COMMON missing -- name: MISSING_DOMAIN_NAME - enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index 7a271b8483..c2d2b51be4 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -113,7 +113,7 @@ data class CommonConfiguration(private val configuration: Map?) * Start of package name, which shoould be common, e.g. org.example.myproject */ val domainName: String? by lazy { - (configuration ?: emptyMap())["domainName"] + configuration?.get("domainName") } /** 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 3ed82dba00..156d662dae 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 @@ -22,7 +22,6 @@ enum class Warnings( val ruleId: String, private val warn: String) : Rule { // ======== chapter 1 ======== - MISSING_DOMAIN_NAME(false, "1.2.1", "missing domain name in config file"), PACKAGE_NAME_MISSING(true, "1.2.1", "no package name declared in a file"), PACKAGE_NAME_INCORRECT_CASE(true, "1.2.1", "package name should be completely in a lower case"), PACKAGE_NAME_INCORRECT_PREFIX(true, "1.2.1", "package name should start from company's domain"), diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index a604497d5e..5ac553e502 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -4,7 +4,6 @@ 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.INCORRECT_PACKAGE_SEPARATOR -import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_DOMAIN_NAME import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX @@ -37,8 +36,8 @@ import java.util.concurrent.atomic.AtomicInteger @Suppress("ForbiddenComment") class PackageNaming(private val configRules: List) : Rule("package-naming") { private var isFixMode: Boolean = false - private var domainName: String? = null private lateinit var emitWarn: EmitType + private lateinit var domainName: String override fun visit(node: ASTNode, autoCorrect: Boolean, @@ -47,36 +46,36 @@ class PackageNaming(private val configRules: List) : Rule("package- emitWarn = emit val configuration by configRules.getCommonConfiguration() - domainName = configuration.also { + configuration.also { if (it.isDefault && visitorCounter.incrementAndGet() == 1) { log.error("Not able to find configuration file") } } - .domainName - if (domainName == null) { - if (visitorCounter.get() == 1) { - MISSING_DOMAIN_NAME.warn(configRules, emitWarn, isFixMode, "No domain name", - node.startOffset, node) - } - } else if (node.elementType == PACKAGE_DIRECTIVE) { - val filePath = node.getRootNode().getFilePath() - // calculating package name based on the directory where the file is placed - val realPackageName = calculateRealPackageName(filePath) + .domainName?.let { + domainName = it + if (node.elementType == PACKAGE_DIRECTIVE) { + val filePath = node.getRootNode().getFilePath() + // calculating package name based on the directory where the file is placed + val realPackageName = calculateRealPackageName(filePath) - // if node isLeaf - this means that there is no package name declared - if (node.isLeaf() && !filePath.isKotlinScript()) { - warnAndFixMissingPackageName(node, realPackageName, filePath) - return - } + // if node isLeaf - this means that there is no package name declared + if (node.isLeaf() && !filePath.isKotlinScript()) { + warnAndFixMissingPackageName(node, realPackageName, filePath) + return + } - // getting all identifiers from existing package name into the list like [org, diktat, project] - val wordsInPackageName = node.findAllNodesWithSpecificType(IDENTIFIER) + // getting all identifiers from existing package name into the list like [org, diktat, project] + val wordsInPackageName = node.findAllNodesWithSpecificType(IDENTIFIER) - // no need to check that packageIdentifiers is empty, because in this case parsing will fail - checkPackageName(wordsInPackageName, node) - // fix in checkFilePathMatchesWithPackageName is much more aggressive than fixes in checkPackageName, they can conflict - checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) - } + // no need to check that packageIdentifiers is empty, because in this case parsing will fail + checkPackageName(wordsInPackageName, node) + // fix in checkFilePathMatchesWithPackageName is much more aggressive than fixes in checkPackageName, they can conflict + checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) + } + } ?: if (visitorCounter.get() == 1) { + log.error("Not able to find an external configuration for domain" + + " name in the common configuration (is it missing in yml config?)") + } } /** @@ -102,7 +101,6 @@ class PackageNaming(private val configRules: List) : Rule("package- * * @return list with words that are parts of package name like [org, diktat, name] */ - @Suppress("UnsafeCallOnNullableType") private fun calculateRealPackageName(fileName: String): List { val filePathParts = fileName.splitPathToDirs() @@ -118,12 +116,11 @@ class PackageNaming(private val configRules: List) : Rule("package- val fileSubDir = filePathParts.subList(filePathParts.lastIndexOf(PACKAGE_PATH_ANCHOR), filePathParts.size - 1) .dropWhile { languageDirNames.contains(it) } // no need to add DOMAIN_NAME to the package name if it is already in path - val domainPrefix = if (!fileSubDir.joinToString(PACKAGE_SEPARATOR).startsWith(domainName!!)) domainName!!.split(PACKAGE_SEPARATOR) else emptyList() + val domainPrefix = if (!fileSubDir.joinToString(PACKAGE_SEPARATOR).startsWith(domainName)) domainName.split(PACKAGE_SEPARATOR) else emptyList() domainPrefix + fileSubDir } } - @Suppress("UnsafeCallOnNullableType") private fun checkPackageName(wordsInPackageName: List, packageDirectiveNode: ASTNode) { // all words should be in a lower case (lower case letters/digits/underscore) wordsInPackageName @@ -136,7 +133,7 @@ class PackageNaming(private val configRules: List) : Rule("package- // package name should start from a company's domain name if (wordsInPackageName.isNotEmpty() && !isDomainMatches(wordsInPackageName)) { - PACKAGE_NAME_INCORRECT_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, domainName!!, + PACKAGE_NAME_INCORRECT_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, domainName, wordsInPackageName[0].startOffset, wordsInPackageName[0]) { val oldPackageName = wordsInPackageName.joinToString(PACKAGE_SEPARATOR) { it.text } val newPackageName = "$domainName$PACKAGE_SEPARATOR$oldPackageName" @@ -190,9 +187,8 @@ class PackageNaming(private val configRules: List) : Rule("package- /** * function simply checks that package name starts with a proper domain name */ - @Suppress("UnsafeCallOnNullableType") private fun isDomainMatches(packageNameParts: List): Boolean { - val packageNamePrefix = domainName!!.split(PACKAGE_SEPARATOR) + val packageNamePrefix = domainName.split(PACKAGE_SEPARATOR) if (packageNameParts.size < packageNamePrefix.size) { return false } diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index c344f2e008..2681f665db 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -43,9 +43,6 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true -# Checks that domain name in DIKTAT_COMMON missing -- name: MISSING_DOMAIN_NAME - enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index 3e98155a65..cf353c0ce0 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -42,9 +42,6 @@ # Checks that the object matches PascalCase - name: OBJECT_NAME_INCORRECT enabled: true -# Checks that domain name in DIKTAT_COMMON missing -- name: MISSING_DOMAIN_NAME - enabled: true # Checks that package name is in correct (lower) case - name: PACKAGE_NAME_INCORRECT_CASE enabled: true diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index 16801fa36e..8a88a1e8ca 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -2,7 +2,6 @@ package org.cqfn.diktat.ruleset.chapter1 import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.ruleset.constants.Warnings.INCORRECT_PACKAGE_SEPARATOR -import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_DOMAIN_NAME import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX @@ -237,16 +236,7 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { @Test @Tag(WarningNames.PACKAGE_NAME_INCORRECT_PATH) - fun `should warn if there is not domain name or it empty`() { - lintMethod( - """ - |package org.cqfn.diktat - """.trimMargin(), - LintError(1, 1, ruleId, "${MISSING_DOMAIN_NAME.warnText()} No domain name", false), - fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", - rulesConfigList = rulesConfigListWithoutDomainName - ) - + fun `should warn if there is empty domain name`() { lintMethod( """ |package org.cqfn.diktat diff --git a/info/available-rules.md b/info/available-rules.md index d3ed22300f..085763b398 100644 --- a/info/available-rules.md +++ b/info/available-rules.md @@ -1,7 +1,6 @@ | Chap | Standard | Rule name | Description | Fix | Config | FixMe | | ----- | ---------- | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | --- | ------ | ----- | | 1 | 1.1.1 | CONFUSING_IDENTIFIER_NAMING | Check: warns if identifier has inappropriate name (See table of rule 1.2 part 6). | no | no | no | -| 1 | 1.2.1 | MISSING_DOMAIN_NAME | Check: warns if there is no domain name in configuration file | no | no | no | | 1 | 1.2.1 | PACKAGE_NAME_MISSING | Check: warns if package name is missing in a file
Fix: automatically adds package directive with the name that starts from the domain name (in example - com.huawei) and contains the real directory | yes | no | Domain name should not be hardcoded. It should be moved to extra configuration.Recursively fix all imports in project.Fix the directory where the code is stored.Make this check isolated from domain name addition| | 1 | 1.2.1 | PACKAGE_NAME_INCORRECT_CASE | Check: warns if package name is in incorrect (non-lower) case
Fix: automatically update the case in package name | yes | no | Recursively update all imports in the project.| | 1 | 1.2.1 | PACKAGE_NAME_INCORRECT_PREFIX | Check: warns if package name does not start with the company's domain
Fix: automatically update the prefix in the package name | yes | no | Fix the directory where the code is stored.Recursively update all imports in the project.Domain name should not be hardcoded. It should be moved to extra configuration.| diff --git a/info/guide/guide-chapter-1.md b/info/guide/guide-chapter-1.md index 1e445ca31f..ae8237d95d 100644 --- a/info/guide/guide-chapter-1.md +++ b/info/guide/guide-chapter-1.md @@ -84,8 +84,6 @@ Package names are all written in lowercase, and consecutive words are concatenat package your.company.domain.mobilecontrol.views ``` -**Note** If there isn't domain name in config file, then package verification will not be carried out. - ### 1.3 Classes, enumerations, interfaces This section describes the general rules for naming classes, enumerations, and interfaces. From 2f68efd290c7df1fbac04250677bde7afadcee5a Mon Sep 17 00:00:00 2001 From: kentr0w Date: Wed, 13 Jan 2021 11:12:59 +0300 Subject: [PATCH 05/10] Warning for domain name ### What's done: Fixed according our code-style --- .../kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index 5ac553e502..efbf96a42c 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -73,9 +73,9 @@ class PackageNaming(private val configRules: List) : Rule("package- checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) } } ?: if (visitorCounter.get() == 1) { - log.error("Not able to find an external configuration for domain" + - " name in the common configuration (is it missing in yml config?)") - } + log.error("Not able to find an external configuration for domain" + + " name in the common configuration (is it missing in yml config?)") + } } /** From 01e2e8f9d8a3693cbb5fb23545b3188437b67324 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Thu, 14 Jan 2021 13:53:50 +0300 Subject: [PATCH 06/10] Warning for domain name ### What's done: Fixed after review --- diktat-rules/src/main/kotlin/generated/WarningNames.kt | 2 -- .../cqfn/diktat/ruleset/rules/files/FileStructureRule.kt | 2 +- .../cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt | 5 ++--- .../cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt | 8 +++++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/diktat-rules/src/main/kotlin/generated/WarningNames.kt b/diktat-rules/src/main/kotlin/generated/WarningNames.kt index 205266bc7b..10842cf258 100644 --- a/diktat-rules/src/main/kotlin/generated/WarningNames.kt +++ b/diktat-rules/src/main/kotlin/generated/WarningNames.kt @@ -5,8 +5,6 @@ package generated import kotlin.String public object WarningNames { - public const val MISSING_DOMAIN_NAME: String = "MISSING_DOMAIN_NAME" - public const val PACKAGE_NAME_MISSING: String = "PACKAGE_NAME_MISSING" public const val PACKAGE_NAME_INCORRECT_CASE: String = "PACKAGE_NAME_INCORRECT_CASE" diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt index a272f77e82..c28a72b364 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/FileStructureRule.kt @@ -275,7 +275,7 @@ class FileStructureRule(private val configRules: List) : Rule("file .importPath ?.fqName ?.pathSegments() - ?.zip(domainName!!.split(PACKAGE_SEPARATOR).map(Name::identifier)) + ?.zip(it.split(PACKAGE_SEPARATOR).map(Name::identifier)) ?.all { it.first == it.second } ?: false } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index 8a88a1e8ca..cfcad912e9 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -22,7 +22,6 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { private val rulesConfigList: List = listOf( RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "org.cqfn.diktat")) ) - private val rulesConfigListWithoutDomainName: List = emptyList() private val rulesConfigListEmptyDomainName: List = listOf( RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "")) ) @@ -242,8 +241,8 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { |package org.cqfn.diktat """.trimMargin(), LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PREFIX.warnText()} ", true), - LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PATH.warnText()} myProjectMain.kotlin.org.cqfn.diktat.example", true), - fileName = "/home/testu/project/src/myProjectMain/kotlin/org/cqfn/diktat/example/Example.kt", + LintError(1, 9, ruleId, "${PACKAGE_NAME_INCORRECT_PATH.warnText()} org.cqfn.diktat.example", true), + fileName = "/home/testu/project/src/main/kotlin/org/cqfn/diktat/example/Example.kt", rulesConfigList = rulesConfigListEmptyDomainName ) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt index a20dbda4b7..c90ca3b26d 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt @@ -25,7 +25,9 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { RulesConfig(FILE_WILDCARD_IMPORTS.name, true, mapOf("allowedWildcards" to "org.cqfn.diktat.example.*, org.cqfn.diktat.ruleset.constants.Warnings.*")) ) - private val rulesConfigListWithoutDomainName: List = emptyList() + private val rulesConfigListEmptyDomainName: List = listOf( + RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "")) + ) @Test @Tag(WarningNames.FILE_CONTAINS_ONLY_COMMENTS) @@ -230,7 +232,7 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { @Test @Tag(WarningNames.FILE_INCORRECT_BLOCKS_ORDER) - fun `test with no domain name in config`() { + fun `test with empty domain name in config`() { lintMethod( """ |package org.cqfn.diktat.example @@ -241,7 +243,7 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { |class Example """.trimMargin(), LintError(3, 1, ruleId, "${FILE_UNORDERED_IMPORTS.warnText()} import com.pinterest.ktlint.core.LintError...", true), - rulesConfigList = rulesConfigListWithoutDomainName + rulesConfigList = rulesConfigListEmptyDomainName ) } } From 63003e0cedb4aa9f5a2592021cd467c39aa0eb37 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Thu, 14 Jan 2021 15:32:37 +0300 Subject: [PATCH 07/10] Warning for domain name ### What's done: Fixed according our code-style --- .../org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt index c90ca3b26d..e433e308df 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/FileStructureRuleTest.kt @@ -26,7 +26,7 @@ class FileStructureRuleTest : LintTestBase(::FileStructureRule) { mapOf("allowedWildcards" to "org.cqfn.diktat.example.*, org.cqfn.diktat.ruleset.constants.Warnings.*")) ) private val rulesConfigListEmptyDomainName: List = listOf( - RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "")) + RulesConfig("DIKTAT_COMMON", true, mapOf("domainName" to "")) ) @Test From ed1463503f2f9c30aa1d59bd4b0899f3b2b34fa7 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 18 Jan 2021 12:19:53 +0300 Subject: [PATCH 08/10] Warning for domain name ### What's done: Fixed after review --- .../diktat/ruleset/rules/PackageNaming.kt | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index efbf96a42c..98d53105dc 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -46,35 +46,30 @@ class PackageNaming(private val configRules: List) : Rule("package- emitWarn = emit val configuration by configRules.getCommonConfiguration() - configuration.also { - if (it.isDefault && visitorCounter.incrementAndGet() == 1) { - log.error("Not able to find configuration file") - } - } - .domainName?.let { - domainName = it - if (node.elementType == PACKAGE_DIRECTIVE) { - val filePath = node.getRootNode().getFilePath() - // calculating package name based on the directory where the file is placed - val realPackageName = calculateRealPackageName(filePath) + configuration.domainName?.let { + domainName = it + if (node.elementType == PACKAGE_DIRECTIVE) { + val filePath = node.getRootNode().getFilePath() + // calculating package name based on the directory where the file is placed + val realPackageName = calculateRealPackageName(filePath) - // if node isLeaf - this means that there is no package name declared - if (node.isLeaf() && !filePath.isKotlinScript()) { - warnAndFixMissingPackageName(node, realPackageName, filePath) - return - } + // if node isLeaf - this means that there is no package name declared + if (node.isLeaf() && !filePath.isKotlinScript()) { + warnAndFixMissingPackageName(node, realPackageName, filePath) + return + } - // getting all identifiers from existing package name into the list like [org, diktat, project] - val wordsInPackageName = node.findAllNodesWithSpecificType(IDENTIFIER) + // getting all identifiers from existing package name into the list like [org, diktat, project] + val wordsInPackageName = node.findAllNodesWithSpecificType(IDENTIFIER) - // no need to check that packageIdentifiers is empty, because in this case parsing will fail - checkPackageName(wordsInPackageName, node) - // fix in checkFilePathMatchesWithPackageName is much more aggressive than fixes in checkPackageName, they can conflict - checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) - } - } ?: if (visitorCounter.get() == 1) { - log.error("Not able to find an external configuration for domain" + - " name in the common configuration (is it missing in yml config?)") + // no need to check that packageIdentifiers is empty, because in this case parsing will fail + checkPackageName(wordsInPackageName, node) + // fix in checkFilePathMatchesWithPackageName is much more aggressive than fixes in checkPackageName, they can conflict + checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) + } + } ?: if (visitorCounter.incrementAndGet() == 1) { + log.error("Not able to find an external configuration for domain" + + " name in the common configuration (is it missing in yml config?)") } } From 647bcd5f6cf8cf6ea30f49485a8aa117d566f85a Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 18 Jan 2021 12:35:50 +0300 Subject: [PATCH 09/10] Warning for domain name ### What's done: Fixed according our code-style --- .../kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index 98d53105dc..2e83c0f651 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -68,8 +68,8 @@ class PackageNaming(private val configRules: List) : Rule("package- checkFilePathMatchesWithPackageName(wordsInPackageName, realPackageName, node) } } ?: if (visitorCounter.incrementAndGet() == 1) { - log.error("Not able to find an external configuration for domain" + - " name in the common configuration (is it missing in yml config?)") + log.error("Not able to find an external configuration for domain" + + " name in the common configuration (is it missing in yml config?)") } } From 9eadc6b41cf52901aa1e46d3e870e3db8016fdd9 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 18 Jan 2021 12:45:06 +0300 Subject: [PATCH 10/10] Warning for domain name ### What's done: Fixed according our code-style --- .../main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt index 2e83c0f651..a21e6fdd27 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/PackageNaming.kt @@ -33,7 +33,7 @@ import java.util.concurrent.atomic.AtomicInteger * need to support autofixing of directories in the same way as package is named. For example if we have package name: * package a.b.c.D -> then class D should be placed in a/b/c/ directories */ -@Suppress("ForbiddenComment") +@Suppress("ForbiddenComment", "TOO_MANY_LINES_IN_LAMBDA") class PackageNaming(private val configRules: List) : Rule("package-naming") { private var isFixMode: Boolean = false private lateinit var emitWarn: EmitType