[uast-inspections] KTIJ-29797 support escape symbols when string literal is created

GitOrigin-RevId: a1dc2cf91d81991fac6442e57b2e5b65addeb987
This commit is contained in:
Mikhail Pyltsin
2024-05-21 19:10:18 +02:00
committed by intellij-monorepo-bot
parent e9fac5fa6d
commit 22fac7715b
3 changed files with 29 additions and 1 deletions

View File

@@ -4,6 +4,17 @@ import java.lang.RuntimeException
class StringTemplateAsArgumentFix {
private val loggerSlf4J = LoggerFactory.getLogger()
val x = 1
val y = 2
fun testWithEscape() {
loggerSlf4J.debug("{}\n{}", x, y)
loggerSlf4J.debug("{}\t{}", x, y)
loggerSlf4J.debug("{}\"{}", x, y)
loggerSlf4J.debug("{}\${}", x, y)
}
fun testLoggerSlf4J() {
val variable1 = "test"
val variable2 = 1

View File

@@ -4,6 +4,17 @@ import java.lang.RuntimeException
class StringTemplateAsArgumentFix {
private val loggerSlf4J = LoggerFactory.getLogger()
val x = 1
val y = 2
fun testWithEscape() {
loggerSlf4J.debug("$x\n$y")
loggerSlf4J.debug("$x\t$y")
loggerSlf4J.debug("$x\"$y")
loggerSlf4J.debug("$x$$y")
}
fun testLoggerSlf4J() {
val variable1 = "test"
val variable2 = 1

View File

@@ -274,10 +274,16 @@ abstract class KotlinUastElementFactory(project: Project) : UastElementFactory {
}
override fun createStringLiteralExpression(text: String, context: PsiElement?): UExpression {
val literal = psiFactory(context).createExpression(StringUtil.wrapWithDoubleQuote(text)) as KtStringTemplateExpression
val literal = psiFactory(context).createExpression(StringUtil.wrapWithDoubleQuote(text.escape())) as KtStringTemplateExpression
return KotlinStringTemplateUPolyadicExpression(literal, null)
}
private fun String.escape(): String {
val stringBuilder = StringBuilder()
StringUtil.escapeStringCharacters(this.length, this, "\"$", stringBuilder)
return stringBuilder.toString()
}
override fun createLongConstantExpression(long: Long, context: PsiElement?): UExpression? {
return when (val literalExpr = psiFactory(context).createExpression(long.toString() + "L")) {
is KtConstantExpression -> KotlinULiteralExpression(literalExpr, null)