Skip to content

Commit

Permalink
Fix ordering issue
Browse files Browse the repository at this point in the history
- added ruleSetId to Rule.VisitorModifier.RunAfterRule
- extracted ordering logic to OrderedRuleSet
- removed all prefixes in rule_id
  • Loading branch information
nulls committed Jun 22, 2022
1 parent a3f3f4d commit 9f927c2
Show file tree
Hide file tree
Showing 73 changed files with 103 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode
* @param id ID of RuleSet
* @param rules rules which belongs to current RuleSet
*/
class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = rules) {

private val orderedIterator: Iterator<Rule> = adjustRules(id, listOf(super.iterator())).iterator()

/**
* @return ordered iterator of rules
*/
override fun iterator(): Iterator<Rule> {
return orderedIterator
}
class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjustRules(id, rules = rules)) {

companion object {
private fun adjustRules(ruleSetId: String, rules: Sequence<Rule>): Sequence<Rule> {
return rules.take(1) +
rules.zipWithNext { prevRule, rule -> OrderedRule(ruleSetId, rule, prevRule) }
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 class OrderedRule(ruleSetId: String, val rule: Rule, prevRule: Rule) : Rule(rule.id, adjustVisitorModifiers(ruleSetId, rule, prevRule)) {
Expand All @@ -44,9 +44,7 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = rules

private fun adjustVisitorModifiers(ruleSetId: String, 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"
}
checkVisitorModifiers(rule)
require(rule.id != prevRule.id) {
"PrevRule has same ID as rule: ${rule.id}"
}
Expand All @@ -57,7 +55,16 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = rules
)
}

fun Rule.delegatee(): Rule = if (this is OrderedRule) this.rule else this
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
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ class RangeConventionalRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "abj-range"
const val NAME_ID = "range"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SingleLineStatementsRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aaq-statement"
const val NAME_ID = "statement"
private val semicolonToken = TokenSet.create(SEMICOLON)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ class SortRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "abp-sort-rule"
const val NAME_ID = "sort-rule"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,6 @@ class StringConcatenationRule(configRules: List<RulesConfig>) : DiktatRule(
this.right is KtExpression

companion object {
const val NAME_ID = "abr-string-concatenation"
const val NAME_ID = "string-concatenation"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ class StringTemplateFormatRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "abs-string-template-format"
const val NAME_ID = "string-template-format"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private val log = LoggerFactory.getLogger(TrailingCommaRule::class.java)
const val NAME_ID = "abh-trailing-comma"
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 @@ -164,6 +164,6 @@ class WhenMustHaveElseRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aal-no-else-in-when"
const val NAME_ID = "no-else-in-when"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ class BlankLinesRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "acd-blank-lines"
const val NAME_ID = "blank-lines"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ class FileSize(configRules: List<RulesConfig>) : DiktatRule(

companion object {
const val MAX_SIZE = 2000L
const val NAME_ID = "ace-file-size"
const val NAME_ID = "file-size"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,6 @@ class FileStructureRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "zcq-file-structure"
const val NAME_ID = "file-structure"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
companion object {
private val log = LoggerFactory.getLogger(NewlinesRule::class.java)
const val MAX_CALLS_IN_ONE_LINE = 3
const val NAME_ID = "zcr-newlines"
const val NAME_ID = "newlines"

// fixme: these token sets can be not full, need to add new once as corresponding cases are discovered.
// error is raised if these operators are prepended by newline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class TopLevelOrderRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
const val NAME_ID = "aap-top-level-order"
const val NAME_ID = "top-level-order"

/**
* List of children that should be sort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private val log = LoggerFactory.getLogger(CompactInitialization::class.java)
const val NAME_ID = "zcs-horizontal-whitespace"
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
private val keywordsWithSpaceAfter = TokenSet.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class LocalVariablesRule(configRules: List<RulesConfig>) : DiktatRule(
) = "<$name> is declared on line <$declared> and is used for the first time on line <$used>"

companion object {
const val NAME_ID = "abc-local-variables"
const val NAME_ID = "local-variables"
private val functionInitializers = listOf(
"emptyList", "emptySet", "emptyMap", "emptyArray", "emptySequence",
"listOf", "setOf", "mapOf", "arrayOf", "arrayListOf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ class ImmutableValNoVarRule(configRules: List<RulesConfig>) : DiktatRule(
}
}
companion object {
const val NAME_ID = "aci-no-var-rule"
const val NAME_ID = "no-var-rule"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,6 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
referenceExpression.elementType == REFERENCE_EXPRESSION && referenceExpression.firstChildNode.text == "require"

companion object {
const val NAME_ID = "ach-null-checks"
const val NAME_ID = "null-checks"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,6 @@ class SmartCastRule(configRules: List<RulesConfig>) : DiktatRule(
class IsExpressions(val identifier: String, val type: String)

companion object {
const val NAME_ID = "abd-smart-cast-rule"
const val NAME_ID = "smart-cast-rule"
}
}
Loading

0 comments on commit 9f927c2

Please sign in to comment.