Skip to content

Commit

Permalink
StringShouldBeRawString: Ignore replaceIndent and prependIndent (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atulgpt committed May 27, 2023
1 parent 466c5af commit a53e520
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
import org.jetbrains.kotlin.psi2ir.deparenthesize

Expand Down Expand Up @@ -81,6 +83,17 @@ class StringShouldBeRawString(config: Config) : Rule(config) {

override fun visitStringTemplateExpression(expression: KtStringTemplateExpression) {
super.visitStringTemplateExpression(expression)

val callExpression = expression.getParentOfType<KtCallExpression>(strict = true)

if (callExpression?.calleeExpression?.text in listOfAllowedMethod) {
return
}

if (expression.text.matches(regexForOnlyQuotes)) {
return
}

val expressionParent = expression.getParentExpressionAfterParenthesis()
val rootElement = expression.getRootExpression()
if (
Expand Down Expand Up @@ -153,5 +166,10 @@ class StringShouldBeRawString(config: Config) : Rule(config) {

companion object {
private val REGEX_FOR_ESCAPE_CHARS = """\\[t"\\n]""".toRegex()
private val listOfAllowedMethod = listOf(
"replaceIndent",
"prependIndent",
)
private val regexForOnlyQuotes = """"(?:\\")*"""".toRegex()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,70 @@ class StringShouldBeRawStringSpec {
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report when replaceIndent is used - #6145`() {
val code = """
fun test() {
val x = ""${'"'}
...
""${'"'}.replaceIndent("\t\t\t\t")
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(MAX_ESCAPED_CHARACTER_COUNT to 0))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report when replaceIndent is used but report other expression inside it`() {
val code = """
fun test() {
val x = ""${'"'}
...
""${'"'}.replaceIndent("\t\t\t\t".also {
"\t a \n"
}
)
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(MAX_ESCAPED_CHARACTER_COUNT to 0))
subject.compileAndLint(code)
assertThat(subject.findings).hasSize(1)
assertThat(subject.findings[0]).hasSourceLocation(5, 13)
}

@Test
fun `does not report when prependIndent is used - #6145`() {
val code = """
fun test() {
val x = ""${'"'}
...
""${'"'}.trimIndent()
val usage = ""${'"'}
...
${'$'}{x.prependIndent("\t\t\t\t")}
""${'"'}
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(MAX_ESCAPED_CHARACTER_COUNT to 0))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

@Test
fun `does not report when only quotes is present - #6145`() {
val code = """
fun test() {
val triplet = "\"\"\""
val sextuple = "\"\"\"\"\"\""
val quadrupleQuintuplePair = "\"\"\"\"" + "\"\"\"\"\""
}
""".trimIndent()
val subject = StringShouldBeRawString(TestConfig(MAX_ESCAPED_CHARACTER_COUNT to 0))
subject.compileAndLint(code)
assertThat(subject.findings).isEmpty()
}

companion object {
private const val MAX_ESCAPED_CHARACTER_COUNT = "maxEscapedCharacterCount"
private const val IGNORED_CHARACTERS = "ignoredCharacters"
Expand Down

0 comments on commit a53e520

Please sign in to comment.