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

Remove multi rule FileParsingRule #5193

Merged
merged 1 commit into from Aug 7, 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

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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ for internal

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)
}