8000 Fix formatting regression where issues printed the whole filename by arturbosch · Pull Request #2988 · detekt/detekt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix formatting regression where issues printed the whole filename #2988

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

Merged
merged 1 commit into from
Aug 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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