Skip to content

Commit

Permalink
KotlinParseException on string concatenation in fix plugin (#1183)
Browse files Browse the repository at this point in the history
### What's done:
* fixed null-safety in rule
  • Loading branch information
Cheshiriks authored Jan 24, 2022
1 parent 2011aff commit c00aab9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtOperationExpression
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 @@ -153,7 +154,8 @@ 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()) {
} else if (binaryExpressionPsi.isRvalueCallExpression() || binaryExpressionPsi.isRvalueDotQualifiedExpression() ||
binaryExpressionPsi.isRvalueOperation() || binaryExpressionPsi.isRvalueSafeQualifiedExpression()) {
// =========== "a " + foo() -> "a ${foo()}}"
return "$lvalueText\${$rvalueText}"
} else if (binaryExpressionPsi.isRvalueReferenceExpression()) {
Expand Down Expand Up @@ -216,4 +218,7 @@ class StringConcatenationRule(configRules: List<RulesConfig>) : DiktatRule(

private fun KtBinaryExpression.isLvalueConstantExpression() =
this.left is KtConstantExpression

private fun KtBinaryExpression.isRvalueSafeQualifiedExpression() =
this.right is KtSafeQualifiedExpression
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ val myTest20 = "${x}string"
val myTest21 = "${x} string"
val myTest22 = "string${foo()}"
val myTest23 = x.toString() + foo()
val myTest24 = foo() + "string"
val myTest24 = foo() + "string"
val myTest25 = "String ${valueStr?.value}"
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ val myTest21 = x.toString() + " string"
val myTest22 = "string" + foo()
val myTest23 = x.toString() + foo()
val myTest24 = foo() + "string"
val myTest25 = "String " + valueStr?.value

0 comments on commit c00aab9

Please sign in to comment.