Skip to content

Commit

Permalink
KotlinParseException on string concatenation for if expression
Browse files Browse the repository at this point in the history
### What's done:
* fixed if expression in rule
closes #1076, #1141
  • Loading branch information
Cheshiriks committed Jan 26, 2022
1 parent 237cb2c commit 7d89624
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtOperationExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.psi.KtStringTemplateExpression

/**
Expand Down Expand Up @@ -154,13 +153,14 @@ class StringConcatenationRule(configRules: List<RulesConfig>) : DiktatRule(
// =========== "a " + "b" -> "a b"
val rvalueTextNew = rvalueText?.trim('"')
return "$lvalueText$rvalueTextNew"
} else if (binaryExpressionPsi.isRvalueCallExpression() || binaryExpressionPsi.isRvalueDotQualifiedExpression() ||
binaryExpressionPsi.isRvalueOperation() || binaryExpressionPsi.isRvalueSafeQualifiedExpression()) {
} else if (binaryExpressionPsi.isRvalueCallExpression()) {
// =========== "a " + foo() -> "a ${foo()}}"
return "$lvalueText\${$rvalueText}"
} else if (binaryExpressionPsi.isRvalueReferenceExpression()) {
// =========== "a " + b -> "a $b"
return "$lvalueText$$rvalueText"
} else if (!binaryExpressionPsi.isRvalueParenthesized() && binaryExpressionPsi.isRvalueExpression()) {
return "$lvalueText\${$rvalueText}"
} else if (binaryExpressionPsi.isRvalueParenthesized()) {
val binExpression = binaryExpressionPsi.right?.children?.first()
if (binExpression is KtBinaryExpression) {
Expand Down Expand Up @@ -198,15 +198,9 @@ class StringConcatenationRule(configRules: List<RulesConfig>) : DiktatRule(
private fun KtBinaryExpression.isRvalueReferenceExpression() =
this.right is KtReferenceExpression

private fun KtBinaryExpression.isRvalueDotQualifiedExpression() =
this.right is KtDotQualifiedExpression

private fun KtBinaryExpression.isRvalueParenthesized() =
this.right is KtParenthesizedExpression

private fun KtBinaryExpression.isRvalueOperation() =
this.right is KtOperationExpression

private fun KtBinaryExpression.isLvalueDotQualifiedExpression() =
this.left is KtDotQualifiedExpression

Expand All @@ -219,6 +213,6 @@ class StringConcatenationRule(configRules: List<RulesConfig>) : DiktatRule(
private fun KtBinaryExpression.isLvalueConstantExpression() =
this.left is KtConstantExpression

private fun KtBinaryExpression.isRvalueSafeQualifiedExpression() =
this.right is KtSafeQualifiedExpression
private fun KtBinaryExpression.isRvalueExpression() =
this.right is KtExpression
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ val myTest21 = "${x} string"
val myTest22 = "string${foo()}"
val myTest23 = x.toString() + foo()
val myTest24 = foo() + "string"
val myTest25 = "String ${valueStr?.value}"
val myTest25 = "String ${valueStr?.value}"
val myTest26 = "my string ${if (true) "1" else "2"}"
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ val myTest22 = "string" + foo()
val myTest23 = x.toString() + foo()
val myTest24 = foo() + "string"
val myTest25 = "String " + valueStr?.value
val myTest26 = "my string " + if (true) "1" else "2"

0 comments on commit 7d89624

Please sign in to comment.