Skip to content

Commit

Permalink
Improve MultilineRawStringIndentation (#5245)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Sep 13, 2022
1 parent 1c3e62b commit 15c19aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
Expand Up @@ -67,7 +67,16 @@ class MultilineRawStringIndentation(config: Config) : Rule(config) {
val lineCount = text.lines().count()
if (lineCount <= 1) return
if (!expression.isTrimmed()) return
if (!text.matches(rawStringRegex)) return
if (!text.matches(rawStringRegex)) {
report(
CodeSmell(
issue,
Entity.from(expression),
"A multiline raw string should start with a break line and should end with another",
)
)
return
}

val lineAndColumn = getLineAndColumnInPsiFile(expression.containingFile, expression.textRange) ?: return

Expand Down Expand Up @@ -158,7 +167,7 @@ private fun message(desiredIntent: Int, currentIndent: Int): String {
return "The indentation should be $desiredIntent but it is $currentIndent."
}

private val rawStringRegex = "\"{3}\n.*\n *\"{3}".toRegex(RegexOption.DOT_MATCHES_ALL)
private val rawStringRegex = "\"{3}\n(.*\n)? *\"{3}".toRegex(RegexOption.DOT_MATCHES_ALL)

private fun String.countIndent() = this.takeWhile { it == ' ' }.count()

Expand Down
Expand Up @@ -301,38 +301,66 @@ class MultilineRawStringIndentationSpec {
}

@Test
fun `don't raise if it contains content after the opening triple quote`() {
fun `don't raise if it isEmpty`() {
val code = """
val a = ${TQ}Hello world!
How are you?
val a = $TQ
$TQ.trimIndent()
""".trimIndent()
subject.compileAndLint(code)
assertThat(subject.findings)
.isEmpty()
}
}

@Nested
inner class MissingBreakingLine {

@Test
fun `don't raise if it contains content before the closing triple quote`() {
fun `raise missing break line start`() {
val code = """
val a = ${TQ}Hello world!
Hola mundo!
$TQ.trimIndent()
""".trimIndent()
subject.compileAndLint(code)
assertThat(subject.findings)
.hasSize(1)
}

@Test
fun `raise missing break line end`() {
val code = """
val a = $TQ
Hello world!
How are you?$TQ.trimIndent()
Hola mundo!$TQ.trimIndent()
""".trimIndent()
subject.compileAndLint(code)
assertThat(subject.findings)
.isEmpty()
.hasSize(1)
}

@Test
fun `don't raise if it isEmpty`() {
fun `raise missing break line both`() {
val code = """
val a = ${TQ}Hello world!
Hola mundo!$TQ.trimIndent()
""".trimIndent()
subject.compileAndLint(code)
assertThat(subject.findings)
.hasSize(1)
}

@Test
fun `don't raise multiline raw string when correct`() {
val code = """
val a = $TQ
Hello world!
Hola mundo!
$TQ.trimIndent()
""".trimIndent()
subject.compileAndLint(code)
assertThat(subject.findings)
.isEmpty()
.hasSize(0)
}
}
}
Expand Down

0 comments on commit 15c19aa

Please sign in to comment.