Skip to content

Commit

Permalink
Removing customizations of KTlint as much as possible from diktat to …
Browse files Browse the repository at this point in the history
…be able to move the project in separate repo. (#4)

### What's done:
1) Mainly rules-config.json handling was changed. Now it is integrated in code and should not be passed as separate config.
2) Tests were change, readme updated.
  • Loading branch information
orchestr7 authored Jul 2, 2020
1 parent 0809274 commit 398bf1d
Show file tree
Hide file tree
Showing 27 changed files with 316 additions and 303 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ a) Use Maven:
This will also install git hooks into your local .git directory. The hooks will restrict commit messages and branch naming.

b) Use Gradle:
`gradle build`
`gradle build`

## What is rules-config.json and why should I care?
In ktlint rules can be configured via .editorconfig, but this does not give a chance to customize or enable/disable each and every rule independently.
That is why we have supported rules-config.json that can be easily changed and help in customization of your own rule set.
It has simple fields: "name" - name of the rule, "enabled" (true/false) to enable or disable that rule and "configuration" - a simple map of some extra unique configurations for the rule, for example:
```json
"configuration": {
"isCopyrightMandatory": true,
"copyrightText": "Copyright (c) Huawei Technologies Co., Ltd. 2012-2020. All rights reserved."
}
```

To install git hooks using gradle run `gradle installGitHooks`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.net.URL
/**
* @param <T> - class name parameter that will be used in calculation of classpath
</T> */
interface JsonResourceConfigReader<T> {
abstract class JsonResourceConfigReader<T> {
/**
* @param resourceFileName - related path to a file from resources
*/
Expand All @@ -28,11 +28,11 @@ interface JsonResourceConfigReader<T> {
return null
}

fun getConfigFile(resourceFileName: String): URL? {
protected open fun getConfigFile(resourceFileName: String): URL? {
return javaClass.classLoader.getResource(resourceFileName)
}

fun parseResource(file: File): T
protected abstract fun parseResource(file: File): T

companion object {
val log: Logger = LoggerFactory.getLogger(JsonResourceConfigReader::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.lang.IllegalArgumentException
import java.net.URL

interface Rule {
Expand All @@ -15,9 +16,9 @@ interface Rule {

@JsonIgnoreProperties(ignoreUnknown = true)
data class RulesConfig(
var name: String,
var enabled: Boolean,
var configuration: Map<String, String>
val name: String,
val enabled: Boolean,
val configuration: Map<String, String>
)

abstract class RuleConfiguration(protected val config: Map<String, String>)
Expand All @@ -26,7 +27,7 @@ object EmptyConfiguration: RuleConfiguration(mapOf())
/**
* class returns the list of configurations that we have read from a json: rules-config.json
*/
class RulesConfigReader : JsonResourceConfigReader<List<RulesConfig>> {
class RulesConfigReader : JsonResourceConfigReader<List<RulesConfig>>() {
companion object {
val log: Logger = LoggerFactory.getLogger(RulesConfigReader::class.java)
}
Expand Down
2 changes: 1 addition & 1 deletion rules-config.json → diktat-rules/rules-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"name": "PACKAGE_NAME_MISSING",
"enabled": true,
"configuration": {
"domainName": "org.cqfn"
"domainName": "org.cqfn.diktat"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package org.cqfn.diktat.ruleset.rules

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader

class DiktatRuleSetProvider : RuleSetProvider {
override fun get(): RuleSet = RuleSet(
"diktat-ruleset",
KdocComments(),
KdocMethods(),
KdocFormatting(),
FileNaming(),
PackageNaming(),
IdentifierNaming()
)
/**
* this constant will be used everywhere in the code to mark usage of Diktat ruleset
*/
const val DIKTAT_RULE_SET_ID = "diktat-ruleset"

class RuleSetDiktat(val rulesConfig: List<RulesConfig>, vararg rules: Rule) : RuleSet(DIKTAT_RULE_SET_ID, *rules)

fun KtLint.Params.getDiktatConfigRules(): List<RulesConfig> {
return (this.ruleSets.find { it.id == DIKTAT_RULE_SET_ID } as RuleSetDiktat).rulesConfig?: listOf()
}

class DiktatRuleSetProvider(private val jsonRulesConfig: String = "rules-config.json") : RuleSetProvider {
override fun get(): RuleSet {
return RuleSetDiktat(
RulesConfigReader().readResource(jsonRulesConfig)?: listOf(),
KdocComments(),
KdocMethods(),
KdocFormatting(),
FileNaming(),
PackageNaming(),
IdentifierNaming()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FileNaming : Rule("file-naming") {
autoCorrect: Boolean,
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
fileName = params.fileName
emitWarn = emit
isFixMode = autoCorrect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class IdentifierNaming : Rule("identifier-naming") {
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
isFixMode = autoCorrect
emitWarn = emit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class KdocComments : Rule("kdoc-comments") {
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
emitWarn = emit
isFixMode = autoCorrect

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KdocFormatting : Rule("kdoc-formatting") {
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {

confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
isFixMode = autoCorrect
emitWarn = emit
fileName = params.fileName ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class KdocMethods : Rule("kdoc-methods") {
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {

confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
isFixMode = autoCorrect
emitWarn = emit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ class PackageNaming : Rule("package-naming") {
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {

confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
isFixMode = autoCorrect
emitWarn = emit

val configuration = PackageNamingConfiguration(confiRules.getRuleConfig(PACKAGE_NAME_MISSING)?.configuration ?: mapOf())
val domainNameConfiguration = confiRules.getRuleConfig(PACKAGE_NAME_MISSING)?.configuration
if (domainNameConfiguration == null) {
log.error("Not able to find an external configuration for domain name in the configuration of" +
" ${PACKAGE_NAME_MISSING.name} check (is it missing in json config?)")
}
val configuration = PackageNamingConfiguration(domainNameConfiguration ?: mapOf())
domainName = configuration.domainName

if (node.elementType == PACKAGE_DIRECTIVE) {
Expand Down Expand Up @@ -243,6 +248,9 @@ class PackageNaming : Rule("package-naming") {
}
}

/**
* this class represents json-map configuration with the only one field (domainName) - can be used for json parsing
*/
class PackageNamingConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
val domainName by config
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
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.ruleset.rules.getDiktatConfigRules
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement

Expand All @@ -38,7 +39,7 @@ class HeaderCommentRule : Rule("header-comment") {
autoCorrect: Boolean,
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
confiRules = params.rulesConfigList!!
confiRules = params.getDiktatConfigRules()
isFixMode = autoCorrect
emitWarn = emit

Expand Down
Loading

0 comments on commit 398bf1d

Please sign in to comment.