Skip to content

Commit

Permalink
Moved Diktat's implementation of KtLint's RuleSetProvider (#1654)
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls authored Apr 3, 2023
1 parent 298f3a9 commit 08836e7
Show file tree
Hide file tree
Showing 32 changed files with 38 additions and 101 deletions.
2 changes: 0 additions & 2 deletions diktat-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ dependencies {
api(libs.kaml)
implementation(libs.apache.commons.cli)
implementation(libs.kotlin.logging)
// ktlint-core is needed only for `initKtLintKLogger` method
implementation(libs.ktlint.core)
testImplementation(libs.junit.jupiter)
testImplementation(libs.assertj.core)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cqfn.diktat.common.config.reader

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import java.io.IOException
Expand Down Expand Up @@ -36,7 +35,7 @@ open class ApplicationProperties(propertiesFileName: String) {
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(ApplicationProperties::class)
private val log = KotlinLogging.logger {}
private const val EXIT_STATUS_MISSING_PROPERTIES = 4
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cqfn.diktat.common.config.reader

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import java.io.BufferedReader
Expand Down Expand Up @@ -57,6 +56,6 @@ abstract class JsonResourceConfigReader<T> {
protected abstract fun parseResource(fileStream: BufferedReader): T

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(JsonResourceConfigReader::class)
private val log = KotlinLogging.logger {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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 org.cqfn.diktat.common.utils.loggerWithKtlintConfig

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
Expand Down Expand Up @@ -106,7 +105,7 @@ open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResour
}

companion object {
internal val log: KLogger = KotlinLogging.loggerWithKtlintConfig(RulesConfigReader::class)
internal val log: KLogger = KotlinLogging.logger {}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.isRuleEnabled
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.utils.getFilePath

Expand Down Expand Up @@ -91,6 +90,6 @@ abstract class DiktatRule(
abstract fun logic(node: ASTNode)

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(DiktatRule::class)
private val log = KotlinLogging.logger {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.chapter1.FileNaming
import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming
Expand Down Expand Up @@ -281,6 +280,6 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = DIKTAT_ANALYS
private fun resolveConfigFileFromSystemProperty(): String? = System.getProperty(DIKTAT_CONF_PROPERTY)

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(DiktatRuleSetProvider::class)
private val log = KotlinLogging.logger {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter1
import org.cqfn.diktat.common.config.rules.CommonConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.INCORRECT_PACKAGE_SEPARATOR
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH
Expand Down Expand Up @@ -282,7 +281,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(PackageNaming::class)
private val log = KotlinLogging.logger {}
const val NAME_ID = "package-naming"

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cqfn.diktat.ruleset.rules.chapter2.comments

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.ListOfPairs
import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
import org.cqfn.diktat.ruleset.rules.DiktatRule
Expand Down Expand Up @@ -174,8 +173,8 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(

@Suppress("MaxLineLength")
companion object {
private val logger = KotlinLogging.loggerWithKtlintConfig(CommentsRule::class)
const val NAME_ID = "comments"
private val logger = KotlinLogging.logger {}
private val importKeywordWithSpace = "${KtTokens.IMPORT_KEYWORD.value} "
private val packageKeywordWithSpace = "${KtTokens.PACKAGE_KEYWORD.value} "
private val importOrPackage = """($importKeywordWithSpace|$packageKeywordWithSpace)""".toRegex()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter2.comments
import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_NOT_BEFORE_PACKAGE
Expand Down Expand Up @@ -274,7 +273,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(HeaderCommentRule::class)
private val log = KotlinLogging.logger {}
const val CURR_YEAR_PATTERN = ";@currYear;"
const val NAME_ID = "header-comment"
val hyphenRegex = Regex("""\d+-\d+""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.TRAILING_COMMA
import org.cqfn.diktat.ruleset.rules.DiktatRule

Expand Down Expand Up @@ -134,7 +133,7 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(TrailingCommaRule::class)
private val log = KotlinLogging.logger {}
const val NAME_ID = "trailing-comma"
val ktVersion = KotlinVersion(1, 4)
val whenChildrenTypes = listOf(WHEN_CONDITION_WITH_EXPRESSION, WHEN_CONDITION_IS_PATTERN, WHEN_CONDITION_IN_RANGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.NONE
Expand Down Expand Up @@ -686,7 +685,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(IndentationRule::class)
private val log = KotlinLogging.logger {}
const val NAME_ID = "indentation"
private val increasingTokens: Set<IElementType> = linkedSetOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START)
private val decreasingTokens: Set<IElementType> = linkedSetOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files
import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.ListOfList
import org.cqfn.diktat.ruleset.constants.Warnings.COMPLEX_EXPRESSION
import org.cqfn.diktat.ruleset.constants.Warnings.REDUNDANT_SEMICOLON
Expand Down Expand Up @@ -711,7 +710,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(NewlinesRule::class)
private val log = KotlinLogging.logger {}
const val MAX_CALLS_IN_ONE_LINE = 3
const val NAME_ID = "newlines"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_WHITESPACE
Expand Down Expand Up @@ -459,7 +458,7 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(WhiteSpaceRule::class)
private val log = KotlinLogging.logger {}
const val NAME_ID = "horizontal-whitespace"

private const val NUM_PARENTS_FOR_LAMBDA = 3 // this is the number of parent nodes needed to check if this node is lambda from argument list
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cqfn.diktat.ruleset.rules.chapter6.classes

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.COMPACT_OBJECT_INITIALIZATION
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.KotlinParser
Expand Down Expand Up @@ -212,7 +211,7 @@ class CompactInitialization(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = KotlinLogging.loggerWithKtlintConfig(CompactInitialization::class)
private val log = KotlinLogging.logger {}
const val NAME_ID = "class-compact-initialization"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

package org.cqfn.diktat.ruleset.utils

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.KtLint.ExperimentalParams
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.RuleSetProvider
import mu.KotlinLogging
import org.intellij.lang.annotations.Language

@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
private val log = KotlinLogging.logger {}

@Suppress("TYPE_ALIAS")
val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

package org.cqfn.diktat.ruleset.utils

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import com.google.common.base.CaseFormat
import mu.KotlinLogging

import java.util.Locale

@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
private val log = KotlinLogging.logger {}

/**
* Available cases to name enum members
Expand Down
2 changes: 2 additions & 0 deletions diktat-ruleset/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {
tasks.named<ShadowJar>("shadowJar") {
archiveBaseName.set("diktat")
archiveClassifier.set("")
// need to relocate serialization from kaml to avoid conflicts with KtLint
relocate("kotlinx.serialization", "com.saveourtool.kotlinx_serialization")
}

// disable default jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.cqfn.diktat.ruleset.rules
import org.cqfn.diktat.ktlint.KtLintRuleSetProviderWrapper.Companion.toKtLint
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.Logger

/**
* [RuleSetProvider] that provides diKTat ruleset.
Expand All @@ -13,5 +16,10 @@ import com.pinterest.ktlint.core.RuleSetProvider
* This class is registered in [resources/META-INF/services/com.pinterest.ktlint.core.RuleSetProvider]
*/
class DiktatRuleSetProviderSpi : RuleSetProvider {
init {
// Need to init KtLint logger to set log level from CLI
KotlinLogging.logger(Logger.ROOT_LOGGER_NAME).initKtLintKLogger()
}

override fun get(): RuleSet = DiktatRuleSetProvider().toKtLint().get()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cqfn.diktat.ruleset.smoke

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.test.framework.processing.TestComparatorUnit
import org.cqfn.diktat.test.framework.util.checkForkedJavaHome
import org.cqfn.diktat.test.framework.util.deleteIfExistsRecursively
Expand Down Expand Up @@ -129,8 +128,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
}

companion object {
@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val logger = KotlinLogging.loggerWithKtlintConfig { }
private val logger = KotlinLogging.logger {}
private const val BASE_DIRECTORY = "src/test/resources/test/smoke"
private const val SAVE_VERSION: String = "0.3.4"
private const val WINDOWS_TEMP_DIRECTORY = ".save-cli"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package org.cqfn.diktat.ruleset.smoke

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.test.framework.util.retry
import mu.KotlinLogging
import org.assertj.core.api.Assertions.fail
Expand All @@ -18,8 +17,7 @@ internal const val DIKTAT_FAT_JAR_GLOB = "diktat-*.jar"
internal const val KTLINT_FAT_JAR = "ktlint"
internal const val KTLINT_VERSION = "0.46.1"

@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val logger = KotlinLogging.loggerWithKtlintConfig { }
private val logger = KotlinLogging.logger {}

/**
* Downloads the file from a remote URL, retrying if necessary.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cqfn.diktat.test.framework.common

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import java.io.IOException
Expand Down Expand Up @@ -37,7 +36,6 @@ class LocalCommandExecutor internal constructor(private val command: String) {
}

companion object {
@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
private val log = KotlinLogging.logger {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.cqfn.diktat.test.framework.config

import org.cqfn.diktat.common.cli.CliArgument
import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import org.apache.commons.cli.CommandLine
Expand Down Expand Up @@ -95,7 +94,6 @@ class TestArgumentsReader(
}

companion object {
@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
private val log = KotlinLogging.logger {}
}
}
Loading

0 comments on commit 08836e7

Please sign in to comment.