Skip to content

Commit

Permalink
Fix formatting regression where issues printed the whole filename - C…
Browse files Browse the repository at this point in the history
…loses #2985 (#2988)
  • Loading branch information
arturbosch committed Aug 19, 2020
1 parent 8664cf6 commit 19ed18e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.formatting

import com.pinterest.ktlint.core.EditorConfig
import com.pinterest.ktlint.core.KtLint
import io.github.detekt.psi.fileName
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.CorrectableCodeSmell
import io.gitlab.arturbosch.detekt.api.Debt
Expand All @@ -15,8 +16,6 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.lang.FileASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.endOffset

Expand Down Expand Up @@ -63,7 +62,7 @@ abstract class FormattingRule(config: Config) : Rule(config) {
SourceLocation(line, column),
TextLocation(node.startOffset, node.psi.endOffset),
"($line, $column)",
root.originalFilePath() ?: root.containingFile.name
root.fileName
)

// Nodes reported by 'NoConsecutiveBlankLines' are dangling whitespace nodes which means they have
Expand All @@ -74,14 +73,11 @@ abstract class FormattingRule(config: Config) : Rule(config) {
.takeIf { it.isNotEmpty() }
?.plus(".")
?: ""
val entity = Entity("", "", "$packageName${root.name}:$line", location, root)
val entity = Entity("", "", "$packageName${root.fileName}:$line", location, root)
report(CorrectableCodeSmell(issue, entity, message, autoCorrectEnabled = autoCorrect))
}
}

private fun ruleShouldOnlyRunOnFileNode(node: ASTNode) =
wrapping is com.pinterest.ktlint.core.Rule.Modifier.RestrictToRoot && node !is FileASTNode

private fun PsiElement.originalFilePath() =
(this.containingFile.viewProvider.virtualFile as? LightVirtualFile)?.originalFile?.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.gitlab.arturbosch.detekt.test.TestConfig
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.nio.file.Paths

class MaximumLineLengthSpec : Spek({

Expand All @@ -17,12 +18,24 @@ class MaximumLineLengthSpec : Spek({

describe("a single function") {

val code = "fun f() { /* 123456789012345678901234567890 */ }"
val code = """
package home.test
fun f() { /* 123456789012345678901234567890 */ }
""".trimIndent()

it("reports line which exceeds the threshold") {
assertThat(subject.lint(code)).hasSize(1)
}

it("reports issues with the filename and package as signature") {
val finding = subject.lint(
code,
Paths.get("home", "test", "Test.kt").toString()
).first()

assertThat(finding.entity.signature).isEqualTo("home.test.Test.kt:2")
}

it("does not report line which does not exceed the threshold") {
val config = TestConfig(MaximumLineLength.MAX_LINE_LENGTH to code.length)
assertThat(MaximumLineLength(config).lint(code)).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
import java.io.File
import java.nio.file.Paths

fun FormattingRule.lint(content: String): List<Finding> {
val root = compileContentForTest(content)
fun FormattingRule.lint(content: String, fileName: String = "Test.kt"): List<Finding> {
val root = compileContentForTest(content, fileName)
this.visit(root)
root.node.visit { node -> this.apply(node) }
return this.findings
Expand Down

0 comments on commit 19ed18e

Please sign in to comment.