Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8940fb6
commit 5156cca
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...s-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrimMultilineRawString.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.gitlab.arturbosch.detekt.rules.style | ||
|
||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import org.jetbrains.kotlin.psi.KtCallExpression | ||
import org.jetbrains.kotlin.psi.KtExpression | ||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression | ||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver | ||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis | ||
|
||
/** | ||
* All the Raw strings that have more than one line should be followed by `trimMargin()` or `trimIndent()`. | ||
* | ||
* <noncompliant> | ||
* """ | ||
* Hello World! | ||
* How are you? | ||
* """ | ||
* </noncompliant> | ||
* | ||
* <compliant> | ||
* """ | ||
* | Hello World! | ||
* | How are you? | ||
* """.trimMargin() | ||
* | ||
* """ | ||
* Hello World! | ||
* How are you? | ||
* """.trimIndent() | ||
* | ||
* """Hello World! How are you?""" | ||
* </compliant> | ||
*/ | ||
class TrimMultilineRawString(val config: Config) : Rule(config) { | ||
override val issue = Issue( | ||
javaClass.simpleName, | ||
Severity.Style, | ||
"Multiline raw strings should be followed by `trimMargin()` or `trimIndent()`.", | ||
Debt.FIVE_MINS | ||
) | ||
|
||
override fun visitStringTemplateExpression(expression: KtStringTemplateExpression) { | ||
super.visitStringTemplateExpression(expression) | ||
|
||
if (expression.text.lines().count() == 1) return | ||
|
||
val nextCall = expression.getQualifiedExpressionForSelectorOrThis() | ||
.getQualifiedExpressionForReceiver() | ||
?.selectorExpression | ||
?.asKtCallExpression() | ||
?.calleeExpression | ||
?.text | ||
|
||
if (nextCall !in trimFunctions) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.from(expression), | ||
"Multiline raw strings should be followd by `trimMargin()` or `trimIndent()`", | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun KtExpression.asKtCallExpression(): KtCallExpression? = this as? KtCallExpression | ||
|
||
private val trimFunctions = listOf("trimIndent", "trimMargin") |
87 changes: 87 additions & 0 deletions
87
...yle/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrimMultilineRawStringSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
@file:Suppress("StringTemplate") | ||
|
||
package io.gitlab.arturbosch.detekt.rules.style | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class TrimMultilineRawStringSpec { | ||
val subject = TrimMultilineRawString(Config.empty) | ||
|
||
@Test | ||
fun `raises multiline raw strings without trim`() { | ||
val code = """ | ||
val a = ${TQ} | ||
Hello world! | ||
${TQ} | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `raises multiline raw strings with lenght`() { | ||
val code = """ | ||
val a = ${TQ} | ||
Hello world! | ||
${TQ}.length | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `doesn't raise multiline raw strings without trimIndent`() { | ||
val code = """ | ||
val a = ${TQ} | ||
Hello world! | ||
${TQ}.trimIndent() | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `doesn't raise multiline raw strings without trimMargin`() { | ||
val code = """ | ||
val a = ${TQ} | ||
|Hello world! | ||
${TQ}.trimMargin() | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `doesn't raise multiline raw strings without trimMargin with parameter`() { | ||
val code = """ | ||
val a = ${TQ} | ||
>Hello world! | ||
${TQ}.trimMargin(">") | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `don't raise one line raw strings`() { | ||
val code = """ | ||
val a = ${TQ}Hello world!${TQ} | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `doesn't raise if it is not a raw string`() { | ||
val code = """ | ||
val a = "Hello world!" | ||
""" | ||
subject.compileAndLint(code) | ||
assertThat(subject.findings).isEmpty() | ||
} | ||
} | ||
|
||
private const val TQ = "\"\"\"" |