Skip to content

Commit

Permalink
Report proper code position in MaxLineLength (#5583)
Browse files Browse the repository at this point in the history
* report proper lines in MaxLineLength

* Add missing test
  • Loading branch information
BraisGabin committed Jan 18, 2023
1 parent 4d6b0f4 commit adf5d96
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Location
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.Configuration
Expand Down Expand Up @@ -61,12 +64,16 @@ class MaxLineLength(config: Config = Config.empty) : Rule(config) {
for (line in lines) {
offset += line.length
if (!isValidLine(file, offset, line)) {
val ktElement = findFirstMeaningfulKtElementInParents(file, offset, line)
if (ktElement != null) {
report(CodeSmell(issue, Entity.from(ktElement), issue.description))
} else {
report(CodeSmell(issue, Entity.from(file, offset), issue.description))
val ktElement = findFirstMeaningfulKtElementInParents(file, offset, line) ?: file
val location = Location.from(file, offset - line.length).let { location ->
Location(
source = location.source,
endSource = SourceLocation(location.source.line, line.length + 1),
text = TextLocation(offset - line.length, offset),
filePath = location.filePath,
)
}
report(CodeSmell(issue, Entity.from(ktElement, location), issue.description))
}

offset += 1 /* '\n' */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,56 @@ class MaxLineLengthSpec {

rule.visitKtFile(file)
assertThat(rule.findings).hasSize(1)
assertThat(rule.findings).hasStartSourceLocations(SourceLocation(6, 5))
assertThat(rule.findings)
.hasStartSourceLocations(SourceLocation(6, 1))
.hasEndSourceLocation(6, 109)
}
}

@Test
fun `report the correct lines on raw strings with backslash on it - issue #5314`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "30",
"excludeRawStrings" to "false",
)
)

rule.visitKtFile(
compileContentForTest(
"""
// some other content
val x = Regex(${"\"\"\""}
Text (.*?)\(in parens\) this is too long to be valid.
The regex/raw string continues down another line .
${"\"\"\""}.trimIndent())
// that is the right length
""".trimIndent()
)
)
assertThat(rule.findings).hasSize(2)
assertThat(rule.findings).hasTextLocations(40 to 97, 98 to 157)
}

@Test
fun `report the correct lines on raw strings with backslash on it 2 - issue #5314`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "30",
"excludeRawStrings" to "false",
)
)

rule.visitKtFile(
compileContentForTest(
"""
// some other content
val x = "Foo".matches(${"\"\"\""}...too long\(parens\) and some more${"\"\"\""}.toRegex())
// that is the right length
""".trimIndent()
)
)
assertThat(rule.findings).hasSize(1)
assertThat(rule.findings).hasTextLocations(22 to 96)
}
}

0 comments on commit adf5d96

Please sign in to comment.