Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
### What's done:
* WIP
  • Loading branch information
kgevorkyan committed Sep 13, 2021
1 parent 56168af commit e469d96
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,22 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
node: ASTNode,
simplifiedExpr: Expression<String>,
mapOfExpressionToChar: HashMap<String, Char>) {
println("simplifiedExpr ${simplifiedExpr}")
var correctKotlinBooleanExpression = simplifiedExpr
.toString()
.replace("&", "&&")
.replace("|", "||")
.drop(1) // dropping first (
.dropLast(1) // dropping last )

if (simplifiedExpr.toString().first() == '(' && simplifiedExpr.toString().last() == ')') {
correctKotlinBooleanExpression = correctKotlinBooleanExpression
.drop(1)
.dropLast(1)
}

mapOfExpressionToChar.forEach { (key, value) ->
correctKotlinBooleanExpression = correctKotlinBooleanExpression.replace(value.toString(), key)
}
println("correct node ${correctKotlinBooleanExpression}")
node.replaceChild(node.firstChildNode, KotlinParser().createNode(correctKotlinBooleanExpression))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ class BooleanExpressionsRuleFixTest : FixTestBase("test/paragraph3/boolean_expre
fun `check distributive law fixing`() {
fixAndCompare("DistributiveLawExpected.kt", "DistributiveLawTest.kt")
}

@Test
@Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
fun `check same expressions`() {
fixAndCompare("SameExpressionsInConditionExpected.kt", "SameExpressionsInConditionTest.kt")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
if (a) {}
if (a) {}
if (a) {}

return if (node is TomlKeyValueSimple) {
decodeValue().toString().toLowerCase() != "null"
} else {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
if (a || a) {}
if (a && a) {}
if ((((a && a)))) {}

return if ((node is TomlKeyValueSimple) || (node is TomlKeyValueSimple)) {
decodeValue().toString().toLowerCase() != "null"
} else {
true
}
}

0 comments on commit e469d96

Please sign in to comment.