Skip to content

Commit

Permalink
Used BINARY_EXPRESSION
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Sep 14, 2022
1 parent e8dc825 commit a16a21b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
* Note: mapping is String to Char(and Char to Char) actually, but will keep it as String for simplicity
*/
internal inner class ExpressionsReplacement {
private val expressionToToken: HashMap<String, String> = LinkedHashMap()
private val expressionToToken: HashMap<String, Pair<String, String>> = LinkedHashMap()
private val tokenToOrderedToken: HashMap<String, String> = HashMap()

/**
* TokenMapper for first call ExprParser which remembers the order of expression.
*/
val orderedTokenMapper: TokenMapper<String> = TokenMapper { name -> getLetter(tokenToOrderedToken, name) }
val orderedTokenMapper: TokenMapper<String> = TokenMapper { name -> getLetter(tokenToOrderedToken, name) { it } }

/**
* Returns <tt>true</tt> if this object contains no replacements.
Expand All @@ -207,7 +207,13 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
val expressionText = expressionAstNode.textWithoutComments()
// support case when `boolean_expression` matches to `!boolean_expression`
val expression = if (expressionText.startsWith('!')) expressionText.substring(1) else expressionText
getLetter(expressionToToken, expression)
getLetter(expressionToToken, expression) { letter ->
letter to if (expressionAstNode.elementType == BINARY_EXPRESSION) {
"($expression)"
} else {
expression
}
}
}

/**
Expand All @@ -222,7 +228,7 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
expressionToToken.keys
.sortedByDescending { it.length }
.forEach { refExpr ->
resultExpression = resultExpression.replace(refExpr, expressionToToken[refExpr]!!)
resultExpression = resultExpression.replace(refExpr, expressionToToken.getValue(refExpr).first)
}
return resultExpression
}
Expand All @@ -241,16 +247,21 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
}
resultExpression = resultExpression.format(args = tokenToOrderedToken.keys.toTypedArray())
// restore expression
expressionToToken.values.forEachIndexed { index, value ->
expressionToToken.values.forEachIndexed { index, (value, _) ->
resultExpression = resultExpression.replace(value, "%${index + 1}\$s")
}
resultExpression = resultExpression.format(args = expressionToToken.keys.toTypedArray())
resultExpression = resultExpression.format(args = expressionToToken.values.map { it.second }.toTypedArray())
return resultExpression
}

private fun getLetter(letters: HashMap<String, String>, key: String) = letters
private fun <T> getLetter(
letters: HashMap<String, T>,
key: String,
valueBuilder: (String) -> T
) = letters
.computeIfAbsent(key) {
('A'.code + letters.size).toChar().toString()
val letter = ('A'.code + letters.size).toChar().toString()
valueBuilder(letter)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package test.paragraph3.boolean_expressions

fun F.foo() {
if (this.valueParameters[i].getFunctionName() != other.valueParameters[i].getFunctionName() || this.valueParameters[i].getFunctionType() != other.valueParameters[i].getFunctionType()
fun F.foo1() {
if (!(this.valueParameters[i].getFunctionName() == other.valueParameters[i].getFunctionName()) || !(this.valueParameters[i].getFunctionType() != other.valueParameters[i].getFunctionType())
) {
return false
}
}

fun F.foo2() {
if (!(this.valueParameters[i].getFunctionName() > other.valueParameters[i].getFunctionName()) || !(this.valueParameters[i].getFunctionType() < other.valueParameters[i].getFunctionType())
) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package test.paragraph3.boolean_expressions

fun F.foo() {
fun F.foo1() {
if (!(this.valueParameters[i].getFunctionName() == other.valueParameters[i].getFunctionName() &&
this.valueParameters[i].getFunctionType() == other.valueParameters[i].getFunctionType())
this.valueParameters[i].getFunctionType() != other.valueParameters[i].getFunctionType())
) {
return false
}
}

fun F.foo2() {
if (!(this.valueParameters[i].getFunctionName() > other.valueParameters[i].getFunctionName() &&
this.valueParameters[i].getFunctionType() < other.valueParameters[i].getFunctionType())
) {
return false
}
Expand Down

0 comments on commit a16a21b

Please sign in to comment.