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

Fix ordering issue #1390

Merged
merged 11 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package org.cqfn.diktat.ruleset.rules
import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.dummy.DummyWarning
import org.cqfn.diktat.ruleset.rules.OrderedRuleSet.Companion.ordered
import org.cqfn.diktat.ruleset.rules.chapter1.FileNaming
import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming
import org.cqfn.diktat.ruleset.rules.chapter1.PackageNaming
Expand Down Expand Up @@ -81,10 +80,8 @@ import org.cqfn.diktat.ruleset.rules.chapter6.classes.SingleConstructorRule
import org.cqfn.diktat.ruleset.rules.chapter6.classes.SingleInitRule
import org.cqfn.diktat.ruleset.rules.chapter6.classes.StatelessClassesRule

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.org.jline.utils.Levenshtein
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -144,9 +141,6 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
// We don't have a way to enforce a specific order, so we should just be careful when adding new rules to this list and, when possible,
// cover new rules in smoke test as well. If a rule needs to be at a specific position in a list, please add comment explaining it (like for NewlinesRule).
val rules = listOf(
// test warning that can be used for manual testing of diktat
::DummyWarning,

// comments & documentation
::CommentsRule,
::SingleConstructorRule, // this rule can add properties to a primary constructor, so should be before KdocComments
Expand Down Expand Up @@ -231,13 +225,10 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
.map {
it.invoke(configRules)
}
val orderedRules = rules.mapIndexed { index, rule ->
if (index != 0) OrderedRule(rule, rules[index - 1]) else rule
}
return RuleSet(
DIKTAT_RULE_SET_ID,
rules = orderedRules.toTypedArray()
)
rules = rules.toTypedArray()
).ordered()
}

private fun validate(config: RulesConfig) =
Expand Down Expand Up @@ -269,43 +260,7 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS

private fun resolveConfigFileFromSystemProperty(): String? = System.getProperty(DIKTAT_CONF_PROPERTY)

/**
* This is a wrapper around Ktlint Rule which adjusts visitorModifiers to keep order with prevRule
* Added as a workaround after introducing a new logic for sorting KtLint Rules: https://github.com/pinterest/ktlint/issues/1478
*
* @property rule KtLink Rule which this class wraps
*
* @param prevRule previous KtLink Rule, the wrapped rule is called after prevRule
*/
internal class OrderedRule(val rule: Rule, prevRule: Rule) : Rule(rule.id, adjustVisitorModifiers(rule, prevRule)) {
/**
* Delegating a call of this method
*/
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
) {
rule.visit(node, autoCorrect, emit)
}
}

companion object {
private val log = LoggerFactory.getLogger(DiktatRuleSetProvider::class.java)

private fun adjustVisitorModifiers(rule: Rule, prevRule: Rule): Set<Rule.VisitorModifier> {
val visitorModifiers: Set<Rule.VisitorModifier> = rule.visitorModifiers
require(visitorModifiers.none { it is Rule.VisitorModifier.RunAfterRule }) {
"Rule ${rule.id} already contains VisitorModifier.RunAfterRule"
}
require(rule.id != prevRule.id) {
"PrevRule has same ID as rule: ${rule.id}"
}
return visitorModifiers + Rule.VisitorModifier.RunAfterRule(
ruleId = prevRule.id,
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.ruleset.constants.EmitType
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

/**
* This is a wrapper around Ktlint RuleSet which adjusts visitorModifiers for all rules to keep order with prevRule
* Added as a workaround after introducing a new logic for sorting KtLint Rules: https://github.com/pinterest/ktlint/issues/1478
*
* @param id ID of RuleSet
* @param rules rules which belongs to current RuleSet
*/
class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjustRules(id, rules = rules)) {
companion object {
private fun adjustRules(ruleSetId: String, vararg rules: Rule): Array<out Rule> {
if (rules.isEmpty()) {
return rules
}
return rules.mapIndexed { index, rule ->
if (index == 0) {
checkVisitorModifiers(rule)
rule
} else {
OrderedRule(ruleSetId, rule, rules[index - 1])
}
}.toTypedArray()
}

private fun adjustVisitorModifiers(
ruleSetId: String,
rule: Rule,
prevRule: Rule
): Set<Rule.VisitorModifier> {
val visitorModifiers: Set<Rule.VisitorModifier> = rule.visitorModifiers
checkVisitorModifiers(rule)
require(rule.id != prevRule.id) {
"PrevRule has same ID as rule: ${rule.id}"
}
return visitorModifiers + Rule.VisitorModifier.RunAfterRule(
ruleId = ruleSetId + ":" + prevRule.id,
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false
)
}

private fun checkVisitorModifiers(rule: Rule) {
require(rule.visitorModifiers.none { it is Rule.VisitorModifier.RunAfterRule }) {
"Rule ${rule.id} contains VisitorModifier.RunAfterRule"
}
}

/**
* @return a rule to which a logic is delegated
*/
internal fun Rule.delegatee(): Rule = if (this is OrderedRule) this.rule else this

/**
* @return RuleSet with ordered rules
*/
fun RuleSet.ordered(): OrderedRuleSet = OrderedRuleSet(id = id, rules = rules)

/**
* @property rule wraps this rule to keep order
*/
private class OrderedRule(
ruleSetId: String,
val rule: Rule,
prevRule: Rule
) : Rule(rule.id, adjustVisitorModifiers(ruleSetId, rule, prevRule)) {
/**
* Delegating a call of this method
*/
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
) {
rule.visit(node, autoCorrect, emit)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FileNaming(configRules: List<RulesConfig>) : DiktatRule(

companion object {
// FixMe: should be moved to properties
const val NAME_ID = "aag-file-naming"
const val NAME_ID = "file-naming"
val validExtensions = listOf(".kt", ".kts")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
companion object {
const val MAX_IDENTIFIER_LENGTH = 64
const val MIN_IDENTIFIER_LENGTH = 2
const val NAME_ID = "aai-identifier-naming"
const val NAME_ID = "identifier-naming"

// FixMe: this should be moved to properties
val oneCharIdentifiers = setOf("i", "j", "k", "x", "y", "z")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private val log = LoggerFactory.getLogger(PackageNaming::class.java)
const val NAME_ID = "aah-package-naming"
const val NAME_ID = "package-naming"

/**
* Directory which is considered the start of sources file tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("MaxLineLength")
companion object {
private val logger = LoggerFactory.getLogger(CommentsRule::class.java)
const val NAME_ID = "aaa-comments"
const val NAME_ID = "comments"
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 @@ -275,7 +275,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
companion object {
private val log = LoggerFactory.getLogger(HeaderCommentRule::class.java)
const val CURR_YEAR_PATTERN = ";@currYear;"
const val NAME_ID = "zcp-header-comment"
const val NAME_ID = "header-comment"
val hyphenRegex = Regex("""\d+-\d+""")
val afterCopyrightRegex = Regex("""((©|\([cC]\))+ *\d+)""")
val curYear = LocalDate.now().year
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,6 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
companion object {
private const val APPROPRIATE_COMMENT_SPACES = 1
private const val MAX_SPACES = 1
const val NAME_ID = "aaf-kdoc-comments-codeblocks-formatting"
const val NAME_ID = "kdoc-comments-codeblocks-formatting"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
private fun isTopLevelFunctionStandard(node: ASTNode): Boolean = node.elementType == FUN && node.isStandardMethod()

companion object {
const val NAME_ID = "aac-kdoc-comments"
const val NAME_ID = "kdoc-comments"
private val statementsToDocument = TokenSet.create(CLASS, FUN, PROPERTY)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class KdocFormatting(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aae-kdoc-formatting"
const val NAME_ID = "kdoc-formatting"
val dateFormats: List<DateTimeFormatter> = listOf("yyyy-dd-mm", "yy-dd-mm", "yyyy-mm-dd", "yy-mm-dd", "yyyy.mm.dd", "yyyy.dd.mm")
.map {
DateTimeFormatter.ofPattern(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aad-kdoc-methods"
const val NAME_ID = "kdoc-methods"
private val expressionBodyTypes = setOf(CALL_EXPRESSION, REFERENCE_EXPRESSION)
private val allExpressionBodyTypes = setOf(
DOT_QUALIFIED_EXPRESSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ class AnnotationNewLineRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "abo-annotation-new-line"
const val NAME_ID = "annotation-new-line"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,6 @@ class BlockStructureBraces(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "zcn-block-structure"
const val NAME_ID = "block-structure"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}
companion object {
private const val INDENT_STEP = 4
const val NAME_ID = "aam-races-rule"
const val NAME_ID = "races-rule"
private val scopeFunctions = listOf("let", "run", "apply", "also")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class ClassLikeStructuresOrderRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aak-class-like-structures"
const val NAME_ID = "class-like-structures"
private val childrenTypes = listOf(PROPERTY, CLASS, CLASS_INITIALIZER, SECONDARY_CONSTRUCTOR, FUN, OBJECT_DECLARATION, ENUM_ENTRY)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ class CollapseIfStatementsRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private const val DEFAULT_NESTED_LEVEL = 2
const val NAME_ID = "abu-collapse-if"
const val NAME_ID = "collapse-if"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ class ConsecutiveSpacesRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private const val MAX_SPACES = 1
const val NAME_ID = "zco-too-many-spaces"
const val NAME_ID = "too-many-spaces"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aan-empty-block-structure"
const val NAME_ID = "empty-block-structure"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class EnumsSeparated(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "abq-enum-separated"
const val NAME_ID = "enum-separated"
private val simpleValue = listOf(IDENTIFIER, WHITE_SPACE, COMMA, SEMICOLON)
private val simpleEnum = listOf(ENUM_ENTRY, WHITE_SPACE, LBRACE, RBRACE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
*/
companion object {
private const val MAX_LENGTH = 120L
const val NAME_ID = "abv-line-length"
const val NAME_ID = "line-length"
private const val STRING_PART_OFFSET = 4
private val propertyList = listOf(INTEGER_CONSTANT, LITERAL_STRING_TEMPLATE_ENTRY, FLOAT_CONSTANT,
CHARACTER_CONSTANT, REFERENCE_EXPRESSION, BOOLEAN_CONSTANT, LONG_STRING_TEMPLATE_ENTRY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ class LongNumericalValuesSeparatedRule(configRules: List<RulesConfig>) : DiktatR
companion object {
private const val DELIMITER_LENGTH: Int = 3
private const val MAX_NUMBER_LENGTH: Int = 3
const val NAME_ID = "abm-long-numerical-values"
const val NAME_ID = "long-numerical-values"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
const val IGNORE_TEST = true
const val NAME_ID = "aca-magic-number"
const val NAME_ID = "magic-number"
val ignoreNumbersList = listOf("-1", "1", "0", "2", "0U", "1U", "2U", "-1L", "0L", "1L", "2L", "0UL", "1UL", "2UL")
val mapConfiguration = mapOf(
"ignoreHashCodeFunction" to true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MultipleModifiersSequence(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aar-multiple-modifiers"
const val NAME_ID = "multiple-modifiers"
private val modifierOrder = listOf(KtTokens.PUBLIC_KEYWORD, KtTokens.INTERNAL_KEYWORD, KtTokens.PROTECTED_KEYWORD,
KtTokens.PRIVATE_KEYWORD, KtTokens.EXPECT_KEYWORD, KtTokens.ACTUAL_KEYWORD, KtTokens.FINAL_KEYWORD,
KtTokens.OPEN_KEYWORD, KtTokens.ABSTRACT_KEYWORD, KtTokens.SEALED_KEYWORD, KtTokens.CONST_KEYWORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class NullableTypeRule(configRules: List<RulesConfig>) : DiktatRule(
)

companion object {
const val NAME_ID = "acg-nullable-type"
const val NAME_ID = "nullable-type"
private val allowExpression = listOf("emptyList", "emptySequence", "emptyArray", "emptyMap", "emptySet",
"listOf", "mapOf", "arrayOf", "sequenceOf", "setOf")
}
Expand Down
Loading