Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MultilineRawStringIndentation #5245

Merged
merged 1 commit into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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