Skip to content

Commit

Permalink
Remove multi rule FileParsingRule (#5193)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
  • Loading branch information
marschwar and Markus Schwarz committed Aug 7, 2022
1 parent ee7949d commit cdd2d71
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 86 deletions.

This file was deleted.

@@ -0,0 +1,7 @@
package io.gitlab.arturbosch.detekt.rules.style

import org.jetbrains.kotlin.psi.KtFile

internal data class KtFileContent(val file: KtFile, val content: Sequence<String>)

internal fun KtFile.toFileContent() = KtFileContent(this, text.splitToSequence("\n"))
Expand Up @@ -48,7 +48,12 @@ class MaxLineLength(config: Config = Config.empty) : Rule(config) {
@Configuration("if comment statements should be ignored")
private val excludeRawStrings: Boolean by config(true)

fun visit(element: KtFileContent) {
override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
visit(file.toFileContent())
}

private fun visit(element: KtFileContent) {
var offset = 0
val lines = element.content
val file = element.file
Expand Down
Expand Up @@ -10,9 +10,7 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.rules.isPartOf
import io.gitlab.arturbosch.detekt.rules.isPartOfString
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtStringTemplateEntryWithExpression
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType

/**
* This rule reports if tabs are used in Kotlin files.
Expand All @@ -30,9 +28,10 @@ class NoTabs(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

fun findTabs(file: KtFile) {
file.forEachDescendantOfType<PsiWhiteSpace> {
if (it.isTab()) report(CodeSmell(issue, Entity.from(it), "Tab character is in use."))
override fun visitWhiteSpace(space: PsiWhiteSpace) {
super.visitWhiteSpace(space)
if (space.isTab()) {
report(CodeSmell(issue, Entity.from(space), "Tab character is in use."))
}
}

Expand Down
Expand Up @@ -33,7 +33,9 @@ class StyleGuideProvider : DefaultRuleSetProvider {
TrimMultilineRawString(config),
NewLineAtEndOfFile(config),
WildcardImport(config),
FileParsingRule(config),
MaxLineLength(config),
TrailingWhitespace(config),
NoTabs(config),
EqualsOnSignatureLine(config),
EqualsNullCall(config),
ForbiddenComment(config),
Expand Down
Expand Up @@ -23,7 +23,12 @@ class TrailingWhitespace(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

fun visit(fileContent: KtFileContent) {
override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
visit(file.toFileContent())
}

private fun visit(fileContent: KtFileContent) {
var offset = 0
fileContent.content.forEachIndexed { index, line ->
offset += line.length
Expand Down
Expand Up @@ -20,38 +20,36 @@ class MaxLineLengthSpec {
@Nested
inner class `a kt file with some long lines` {
private val file = compileForTest(Case.MaxLineLength.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)

@Test
fun `should report no errors when maxLineLength is set to 200`() {
val rule = MaxLineLength(TestConfig(mapOf(MAX_LINE_LENGTH to "200")))

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).isEmpty()
}

@Test
fun `should report all errors with default maxLineLength`() {
val rule = MaxLineLength()

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(3)
}

@Test
fun `should report all errors with default maxLineLength including raw strings`() {
val rule = MaxLineLength(TestConfig("excludeRawStrings" to false))

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(7)
}

@Test
fun `should report meaningful signature for all violations`() {
val rule = MaxLineLength()

rule.visit(fileContent)
rule.visitKtFile(file)
val locations = rule.findings.map { it.signature.substringAfterLast('$') }
doAssert(locations).allSatisfy { doAssert(it).isNotBlank() }
}
Expand All @@ -60,14 +58,12 @@ class MaxLineLengthSpec {
@Nested
inner class `a kt file with long but suppressed lines` {
private val file = compileForTest(Case.MaxLineLengthSuppressed.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)

@Test
fun `should not report as lines are suppressed`() {
val rule = MaxLineLength()

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).isEmpty()
}
}
Expand All @@ -84,8 +80,6 @@ class MaxLineLengthSpec {
"""

private val file = compileContentForTest(code)
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)

@Test
fun `should not report the package statement and import statements by default`() {
Expand All @@ -97,7 +91,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).isEmpty()
}

Expand All @@ -113,7 +107,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(2)
}

Expand All @@ -127,24 +121,22 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).isEmpty()
}
}

@Nested
inner class `a kt file with a long package name, long import statements, a long line and long comments` {
private val file = compileForTest(Case.MaxLineLengthWithLongComments.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)

@Test
fun `should report the package statement, import statements, line and comments by default`() {
val rule = MaxLineLength(
TestConfig(MAX_LINE_LENGTH to "60")
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(8)
}

Expand All @@ -159,7 +151,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(8)
}

Expand All @@ -174,7 +166,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(5)
}
}
Expand All @@ -192,16 +184,14 @@ class MaxLineLengthSpec {
""".trimIndent()

private val file = compileContentForTest(code)
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)

@Test
fun `should only the function line by default`() {
val rule = MaxLineLength(
TestConfig(MAX_LINE_LENGTH to "60")
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(1)
}

Expand All @@ -215,7 +205,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(3)
}

Expand All @@ -229,7 +219,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(1)
}

Expand All @@ -243,7 +233,7 @@ class MaxLineLengthSpec {
)
)

rule.visit(fileContent)
rule.visitKtFile(file)
assertThat(rule.findings).hasSize(1)
assertThat(rule.findings).hasStartSourceLocations(SourceLocation(6, 5))
}
Expand Down
Expand Up @@ -6,20 +6,19 @@ import io.gitlab.arturbosch.detekt.test.assertThat
import org.junit.jupiter.api.Test

class NoTabsSpec {
private val subject = NoTabs()

@Test
fun `should flag a line that contains a tab`() {
val subject = NoTabs()
val file = compileForTest(Case.NoTabsPositive.path())
subject.findTabs(file)
subject.visitFile(file)
assertThat(subject.findings).hasSize(5)
}

@Test
fun `should not flag a line that does not contain a tab`() {
val subject = NoTabs()
val file = compileForTest(Case.NoTabsNegative.path())
subject.findTabs(file)
subject.visitFile(file)
assertThat(subject.findings).isEmpty()
}
}
@@ -1,45 +1,58 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.github.detekt.test.utils.compileContentForTest
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.test.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD

@TestInstance(PER_METHOD)
class TrailingWhitespaceSpec {

private val subject = TrailingWhitespace()

private fun compileAndLintWithoutTrim(code: String): List<Finding> {
val ktFile = compileContentForTest(code)
subject.visitKtFile(ktFile)
return subject.findings
}

@Nested
@TestInstance(PER_METHOD)
inner class `positive cases` {

@Test
fun `reports a line just with a whitespace`() {
val rule = TrailingWhitespace()
rule.visit(" ".toKtFileContent())
assertThat(rule.findings).hasTextLocations(0 to 1)
val code = " "
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).hasTextLocations(0 to 1)
}

@Test
fun `reports a commented line with a whitespace at the end`() {
val rule = TrailingWhitespace()
rule.visit("// A comment ".toKtFileContent())
assertThat(rule.findings).hasTextLocations(12 to 13)
val code = "// A comment "
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).hasTextLocations(12 to 13)
}

@Test
fun `reports a class declaration with a whitespace at the end`() {
val rule = TrailingWhitespace()
rule.visit(" class TrailingWhitespacePositive { \n }".toKtFileContent())
assertThat(rule.findings).hasTextLocations(36 to 37)
val code = " class TrailingWhitespacePositive { \n }"
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).hasTextLocations(36 to 37)
}

@Test
fun `reports a print statement with a tab at the end`() {
val rule = TrailingWhitespace()
rule.visit("\t\tprintln(\"A message\")\t".toKtFileContent())
assertThat(rule.findings).hasTextLocations(22 to 23)
val code = "\t\tprintln(\"A message\")\t"
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).hasTextLocations(22 to 23)
}
}

@Nested
@TestInstance(PER_METHOD)
inner class `negative cases` {

@Test
Expand All @@ -53,9 +66,8 @@ class TrailingWhitespaceSpec {
}
}
""".trimIndent()
val rule = TrailingWhitespace()
rule.visit(code.toKtFileContent())
assertThat(rule.findings).isEmpty()
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -67,15 +79,8 @@ class TrailingWhitespaceSpec {
Should ignore indent on the previous line
""${'"'}
""".trim()
val rule = TrailingWhitespace()
rule.visit(code.toKtFileContent())
assertThat(rule.findings).isEmpty()
val findings = compileAndLintWithoutTrim(code)
assertThat(findings).isEmpty()
}
}
}

private fun String.toKtFileContent(): KtFileContent {
val file = compileContentForTest(this)
val lines = file.text.splitToSequence("\n")
return KtFileContent(file, lines)
}

0 comments on commit cdd2d71

Please sign in to comment.