Skip to content

Commit

Permalink
Don't allow invalid Source Locations (#7030)
Browse files Browse the repository at this point in the history
* Improve tests

* Don't add invalid values on SourceLocation

* Don't hide problems with coerce

* Fix wording in assertion messages

---------

Co-authored-by: Matthew Haughton <3flex@users.noreply.github.com>
  • Loading branch information
BraisGabin and 3flex committed Mar 17, 2024
1 parent 839c58b commit d08f8a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Location(

private fun lineAndColumn(element: PsiElement, range: TextRange): PsiDiagnosticUtils.LineAndColumn {
return getLineAndColumnInPsiFile(element.containingFile, range)
?: PsiDiagnosticUtils.LineAndColumn(-1, -1, null)
?: PsiDiagnosticUtils.LineAndColumn(1, 1, null)
}
}
}
Expand All @@ -70,6 +70,11 @@ class Location(
*/
@Poko
class SourceLocation(val line: Int, val column: Int) {
init {
require(line > 0) { "The source location line must be greater than 0" }
require(column > 0) { "The source location column must be greater than 0" }
}

override fun toString(): String = "$line:$column"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private fun buildEmptyEntity(): Entity = Entity(
name = "",
signature = "",
location = Location(
source = SourceLocation(0, 0),
source = SourceLocation(1, 1),
text = TextLocation(0, 0),
filePath = FilePath.fromAbsolute(Path("/"))
),
Expand Down
12 changes: 8 additions & 4 deletions detekt-psi-utils/src/main/kotlin/io/github/detekt/psi/KtFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ fun PsiFile.toFilePath(): FilePath {

// #3317 If any rule mutates the PsiElement, searching the original PsiElement may throw an exception.
fun getLineAndColumnInPsiFile(file: PsiFile, range: TextRange): PsiDiagnosticUtils.LineAndColumn? {
return runCatching {
@Suppress("ForbiddenMethodCall")
DiagnosticUtils.getLineAndColumnInPsiFile(file, range)
}.getOrNull()
return if (file.textLength == 0) {
null
} else {
runCatching {
@Suppress("ForbiddenMethodCall")
DiagnosticUtils.getLineAndColumnInPsiFile(file, range)
}.getOrNull()
}
}

private fun buildPlatformSpecificSuffixes(platforms: List<String>): List<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ import org.junit.jupiter.api.Test
class EmptyKotlinFileSpec {
private val subject = EmptyKotlinFile(Config.empty)

private val codeWithPackageStatement = """
package my.packagee
""".trimIndent()

private val codeWithFunction = """
package my.packagee
fun myFunction() {}
""".trimIndent()

@Test
fun `reports empty if file is blank`() {
val code = ""
assertThat(subject.lint(code)).hasSize(1)
assertThat(subject.lint(code))
.singleElement()
.hasSourceLocation(1, 1)
}

@Test
fun `does report file with package statement`() {
assertThat(subject.compileAndLint(codeWithPackageStatement)).hasSize(1)
val codeWithPackageStatement = """
package my.packagee
""".trimIndent()
assertThat(subject.compileAndLint(codeWithPackageStatement))
.singleElement()
.hasSourceLocation(1, 1)
}

@Test
fun `does not report file with code`() {
assertThat(subject.compileAndLint(codeWithFunction)).hasSize(0)
val code = """
package my.packagee
fun myFunction() {}
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}
}

0 comments on commit d08f8a2

Please sign in to comment.