Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/npe_long_line_rule
Browse files Browse the repository at this point in the history
  • Loading branch information
kgevorkyan authored Jul 16, 2021
2 parents ea4ca15 + 502baa7 commit b1e2c97
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 105 deletions.
1 change: 0 additions & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
configuration:
# number of lines
maxSize: '2000'
ignoreFolders: ''
# Checks that file does not contain commented out code
- name: COMMENTED_OUT_CODE
enabled: true
Expand Down
1 change: 0 additions & 1 deletion diktat-common/src/test/resources/test-rules-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
enabled: true
configuration:
maxSize: '2000'
ignoreFolders: ''
- name: COMMENTED_OUT_CODE
enabled: true
- name: FILE_CONTAINS_ONLY_COMMENTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
val filePathParts = fileName.splitPathToDirs()

return if (!filePathParts.contains(PACKAGE_PATH_ANCHOR)) {
log.error("Not able to determine a path to a scanned file or src directory cannot be found in it's path." +
" Will not be able to determine correct package name. It can happen due to missing <src> directory in the path")
log.error("Not able to determine a path to a scanned file or \"$PACKAGE_PATH_ANCHOR\" directory cannot be found in it's path." +
" Will not be able to determine correct package name. It can happen due to missing <$PACKAGE_PATH_ANCHOR> directory in the path")
emptyList()
} else {
// creating a real package name:
Expand Down Expand Up @@ -276,7 +276,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
/**
* Directory which is considered the start of sources file tree
*/
const val PACKAGE_PATH_ANCHOR = "src"
const val PACKAGE_PATH_ANCHOR = SRC_DIRECTORY_NAME

/**
* Symbol that is used to separate parts in package name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings.FILE_IS_TOO_LONG
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.SRC_DIRECTORY_NAME
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.isGradleScript
import org.cqfn.diktat.ruleset.utils.splitPathToDirs

import com.pinterest.ktlint.core.ast.ElementType
Expand All @@ -27,16 +29,7 @@ class FileSize(configRules: List<RulesConfig>) : DiktatRule(

override fun logic(node: ASTNode) {
if (node.elementType == ElementType.FILE) {
val filePathParts = node.getFilePath().splitPathToDirs()
if (SRC_PATH !in filePathParts) {
log.error("$SRC_PATH directory is not found in file path ${node.getFilePath()}")
} else {
if (configuration.ignoreFolders.none {
filePathParts.containsAll(it.splitPathToDirs())
}) {
checkFileSize(node, configuration.maxSize)
}
}
checkFileSize(node, configuration.maxSize)
}
}

Expand All @@ -58,18 +51,9 @@ class FileSize(configRules: List<RulesConfig>) : DiktatRule(
* Maximum allowed number of lines in a file
*/
val maxSize = config["maxSize"]?.toLongOrNull() ?: MAX_SIZE

/**
* List of folders, files from which are ignored during the check. For example, for tests.
*/
val ignoreFolders = config["ignoreFolders"]?.replace("\\s+".toRegex(), "")?.split(IGNORE_FOLDERS_SEPARATOR) ?: ignoreFolder
}

companion object {
private val log = LoggerFactory.getLogger(FileSize::class.java)
const val IGNORE_FOLDERS_SEPARATOR = ","
const val MAX_SIZE = 2000L
const val SRC_PATH = "src"
private val ignoreFolder: List<String> = emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Utility methods to work with file paths.
*/

package org.cqfn.diktat.ruleset.utils

internal const val SRC_DIRECTORY_NAME = "src"

/**
* Splits [this] string by file path separator
*
* @return list of path parts
*/
fun String.splitPathToDirs(): List<String> =
this.replace("\\", "/")
.replace("//", "/")
.split("/")

/**
* Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts'
*
* @return true if this is a kotlin script file name, false otherwise
*/
fun String.isKotlinScript() = endsWith(".kts")

/**
* Checks if [this] String is a name of a gradle kotlin script file by checking whether file extension equals 'gradle.kts'
*
* @return true if this is a gradle kotlin script file name, false otherwise
*/
fun String.isGradleScript() = endsWith("gradle.kts")
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ fun String.containsOneLetterOrZero(): Boolean {
return count == 1 || count == 0
}

/**
* Splits [this] string by file path separator
*
* @return list of path parts
*/
fun String.splitPathToDirs(): List<String> =
this.replace("\\", "/")
.replace("//", "/")
.split("/")

/**
* method checks that string has prefix like:
* mFunction, kLength or M_VAR
Expand Down Expand Up @@ -105,17 +95,3 @@ fun String.removePrefix(): String {
}
return this
}

/**
* Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts'
*
* @return true if this is a kotlin script file name, false otherwise
*/
fun String.isKotlinScript() = endsWith(".kts")

/**
* Checks if [this] String is a name of a gradle kotlin script file by checking whether file extension equals 'gradle.kts'
*
* @return true if this is a gradle kotlin script file name, false otherwise
*/
fun String.isGradleScript() = endsWith("gradle.kts")
1 change: 0 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
configuration:
# number of lines
maxSize: '2000'
ignoreFolders: ''
# Checks that file does not contain commented out code
- name: COMMENTED_OUT_CODE
enabled: true
Expand Down
1 change: 0 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
configuration:
# number of lines
maxSize: '2000'
ignoreFolders: ''
# Checks that file does not contain commented out code
- name: COMMENTED_OUT_CODE
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,10 @@ class FileSizeWarnTest : LintTestBase(::FileSize) {
RulesConfig(Warnings.FILE_IS_TOO_LONG.name, true,
mapOf("maxSize" to "10"))
)
private val rulesConfigListIgnore: List<RulesConfig> = listOf(
RulesConfig(Warnings.FILE_IS_TOO_LONG.name, true,
mapOf("maxSize" to "5", "ignoreFolders" to "main"))
)
private val rulesConfigListEmpty: List<RulesConfig> = listOf(
RulesConfig(Warnings.FILE_IS_TOO_LONG.name, true,
emptyMap())
)
private val rulesConfigListOnlyIgnore: List<RulesConfig> = listOf(
RulesConfig(Warnings.FILE_IS_TOO_LONG.name, true,
mapOf("ignoreFolders" to "A"))
)
private val rulesConfigListTwoIgnoreFolders: List<RulesConfig> = listOf(
RulesConfig(Warnings.FILE_IS_TOO_LONG.name, true,
mapOf("maxSize" to "8", "ignoreFolders" to "A, B"))
)

@Test
@Tag(WarningNames.FILE_IS_TOO_LONG)
fun `file larger then max with ignore`() {
val path = javaClass.classLoader.getResource("test/paragraph3/src/main/FileSizeLarger.kt")
val file = File(path!!.file)
lintMethod(file.readText(), fileName = file.absolutePath, rulesConfigList = rulesConfigListIgnore)
}

@Test
@Tag(WarningNames.FILE_IS_TOO_LONG)
Expand Down Expand Up @@ -91,42 +71,10 @@ class FileSizeWarnTest : LintTestBase(::FileSize) {
fileName = file.absolutePath, rulesConfigList = rulesConfigListLarge)
}

@Test
@Tag(WarningNames.FILE_IS_TOO_LONG)
fun `config has only ignoreFolders`() {
val path = javaClass.classLoader.getResource("test/paragraph3/src/main/A/FileSize2000.kt")
val file = File(path!!.file)
lintMethod(file.readText(), fileName = file.absolutePath, rulesConfigList = rulesConfigListOnlyIgnore)
}

private fun generate2000lines(): Int {
val path = javaClass.classLoader.getResource("test/paragraph3/src/main/A/FileSize2000.kt")
val file = File(path!!.file)
file.writeText("//hello \n".repeat(2000))
return 2000
}

@Test
@Tag(WarningNames.FILE_IS_TOO_LONG)
@Suppress("SAY_NO_TO_VAR")
fun `ignoring two out of three folders`() {
var path = javaClass.classLoader.getResource("test/paragraph3/src/main/A/FileSizeA.kt")
var file = File(path!!.file)
lintMethod(file.readText(), fileName = file.absolutePath, rulesConfigList = rulesConfigListTwoIgnoreFolders)
path = javaClass.classLoader.getResource("test/paragraph3/src/main/B/FileSizeB.kt")
file = File(path!!.file)
lintMethod(file.readText(), fileName = file.absolutePath, rulesConfigList = rulesConfigListTwoIgnoreFolders)
path = javaClass.classLoader.getResource("test/paragraph3/src/main/C/FileSizeC.kt")
file = File(path!!.file)
val size = SIZE
lintMethod(file.readText(),
LintError(1, 1, "$DIKTAT_RULE_SET_ID:file-size",
"${Warnings.FILE_IS_TOO_LONG.warnText()} $size", false),
fileName = file.absolutePath,
rulesConfigList = rulesConfigListTwoIgnoreFolders)
}

companion object {
private const val SIZE = 10
}
}
2 changes: 1 addition & 1 deletion info/available-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
| 2 | 2.4.1 | FIRST_COMMENT_NO_BLANK_LINE | Check: warns if there is a new line before first comment<br>Fix: deletes a new line | yes | no | - |
| 2 | 2.4.1 | IF_ELSE_COMMENTS | Check: warns if there is a comment not in the else block<br>Fix: adds the comment to the first line in else block | yes | no | - |
| 2 | 2.2.1 | HEADER_NOT_BEFORE_PACKAGE | Check: warns if header KDoc if file is located not before package directive<br>Fix: moves this KDoc | yes | no | |
| 3 | 3.1.1 | FILE_IS_TOO_LONG | Check: warns if file has too many lines<br>Fix: no | no | maxSize<br> ignoreFolders | - |
| 3 | 3.1.1 | FILE_IS_TOO_LONG | Check: warns if file has too many lines<br>Fix: no | no | maxSize | - |
| 3 | 3.1.2 | FILE_CONTAINS_ONLY_COMMENTS | Check: warns if file contains only comments, imports and package directive. | no | no | - |
| 3 | 3.1.2 | FILE_INCORRECT_BLOCKS_ORDER | Check: warns if general order of code parts is wrong.<br>Fix: rearranges them. | yes | no | handle other elements that could be present before package directive (other comments) |
| 3 | 3.1.2 | FILE_NO_BLANK_LINE_BETWEEN_BLOCKS | Check: warns if there is not exactly one blank line between code parts.<br>Fix: leaves single empty line | yes | no | - |
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.5.20</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlinx.serialization.version>1.2.1</kotlinx.serialization.version>
<kotlinx.serialization.version>1.2.2</kotlinx.serialization.version>
<ktlint.version>0.39.0</ktlint.version>
<junit.version>5.7.2</junit.version>
<guava.version>30.1.1-jre</guava.version>
Expand All @@ -53,7 +53,7 @@
<diktat-check.version>1.0.0-rc.2</diktat-check.version>
<kotlinpoet.version>1.9.0</kotlinpoet.version>
<detekt.version>1.17.1</detekt.version>
<dokka.version>1.4.32</dokka.version>
<dokka.version>1.5.0</dokka.version>
<jacoco.version>0.8.7</jacoco.version>
<maven-plugin-tools.version>3.6.1</maven-plugin-tools.version>
<jbool.version>1.23</jbool.version>
Expand Down

0 comments on commit b1e2c97

Please sign in to comment.