Skip to content

Commit

Permalink
Entity.KtElement is not longer nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed May 8, 2024
1 parent e79ba7f commit 726c394
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 36 deletions.
1 change: 1 addition & 0 deletions detekt-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {

testImplementation(projects.detektTest)
testImplementation(libs.assertj)
testFixturesImplementation(projects.detektTestUtils)
}

detekt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Entity(
val name: String,
val signature: String,
val location: Location,
val ktElement: KtElement?
val ktElement: KtElement
) : Compactable {

override fun compact(): String = "[$name] at ${location.compact()}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.api

import io.github.detekt.test.utils.internal.FakeKtElement
import io.gitlab.arturbosch.detekt.test.location
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand All @@ -20,16 +21,15 @@ class CodeSmellSpec {
text = TextLocation(0, 0),
path = Path("/").absolute().resolve("Users/tester/detekt/TestFile.kt"),
),
ktElement = null
ktElement = FakeKtElement()
),
message = "TestMessage"
)

assertThat(codeSmell.toString()).isEqualTo(
"CodeSmell(entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"path=${codeSmell.location.path}), ktElement=null), message=TestMessage, " +
"references=[])"
"location=Location(source=1:1, endSource=1:1, text=0:0, path=${codeSmell.location.path})," +
"ktElement=FakeKtElement), message=TestMessage, references=[])"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CorrectableCodeSmellSpec {
"CorrectableCodeSmell(autoCorrectEnabled=true, " +
"entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"path=${codeSmell.location.path}), ktElement=null), " +
"path=${codeSmell.location.path}), ktElement=FakeKtElement), " +
"message=TestMessage, references=[])"
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.test

import io.github.detekt.test.utils.internal.FakeKtElement
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Location
Expand Down Expand Up @@ -86,7 +87,7 @@ fun createIssueForRelativePath(
fun createEntity(
signature: String = "TestEntitySignature",
location: Location = createLocation(),
ktElement: KtElement? = null,
ktElement: KtElement = FakeKtElement(),
) = Entity(
name = "TestEntity",
signature = signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal class Analyzer(
return (correctableRules + otherRules).flatMap { (ruleInfo, rule) ->
rule.visitFile(file, bindingContext, compilerResources)
.filterNot {
it.entity.ktElement?.isSuppressedBy(ruleInfo.id, rule.aliases, ruleInfo.ruleSetId) == true
it.entity.ktElement.isSuppressedBy(ruleInfo.id, rule.aliases, ruleInfo.ruleSetId)
}
.filterSuppressedFindings(rule, bindingContext)
.map { it.toIssue(ruleInfo, rule.computeSeverity()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ internal fun annotationSuppressorFactory(rule: Rule, bindingContext: BindingCont
return if (annotations.isNotEmpty()) {
Suppressor { finding ->
val element = finding.entity.ktElement
element != null &&
element.isAnnotatedWith(AnnotationExcluder(element.containingKtFile, annotations, bindingContext))
element.isAnnotatedWith(AnnotationExcluder(element.containingKtFile, annotations, bindingContext))
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ internal fun functionSuppressorFactory(rule: Rule, bindingContext: BindingContex
.map(FunctionMatcher::fromFunctionSignature)
return if (functionMatchers.isNotEmpty()) {
Suppressor { finding ->
val element = finding.entity.ktElement
element != null && functionSuppressor(element, bindingContext, functionMatchers)
functionSuppressor(finding.entity.ktElement, bindingContext, functionMatchers)
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.core.suppressors

import io.github.detekt.test.utils.internal.FakeKtElement
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Finding
Expand All @@ -17,7 +18,7 @@ private fun buildEmptyEntity(): Entity = Entity(
name = "",
signature = "",
location = createLocation(""),
ktElement = null,
ktElement = FakeKtElement(),
)

internal fun buildRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,9 @@ class HtmlOutputReport : BuiltInOutputReport, OutputReport() {
span("message") { text(issue.message) }
}

val psiFile = issue.entity.ktElement?.containingFile
if (psiFile != null) {
val lineSequence = psiFile.text.splitToSequence('\n')
snippetCode(issue.ruleInfo.id, lineSequence, issue.location.source, issue.location.text.length())
}
val psiFile = issue.entity.ktElement.containingFile
val lineSequence = psiFile.text.splitToSequence('\n')
snippetCode(issue.ruleInfo.id, lineSequence, issue.location.source, issue.location.text.length())
}

private fun getComplexityMetrics(detektion: Detektion): List<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,9 @@ private fun MarkdownContent.renderIssue(issue: Issue, basePath: Path?): String {
""
}

val psiFile = issue.entity.ktElement?.containingFile
val snippet = if (psiFile != null) {
val lineSequence = psiFile.text.splitToSequence('\n')
snippetCode(lineSequence, issue.location.source)
} else {
""
}
val psiFile = issue.entity.ktElement.containingFile
val lineSequence = psiFile.text.splitToSequence('\n')
val snippet = snippetCode(lineSequence, issue.location.source)

return "$location\n$message\n$snippet"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class VarCouldBeValSpec(val env: KotlinCoreEnvironment) {

assertThat(lint).hasSize(1)
with(lint[0].entity) {
assertThat(ktElement?.text).isEqualTo("var shadowed = 1")
assertThat(ktElement.text).isEqualTo("var shadowed = 1")
}
}
}
Expand Down Expand Up @@ -307,7 +307,7 @@ class VarCouldBeValSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
with(subject.compileAndLintWithContext(env, code)[0]) {
assertThat(entity.ktElement?.text).isEqualTo("var myVar = value")
assertThat(entity.ktElement.text).isEqualTo("var myVar = value")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MandatoryBracesLoopsSpec {
val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings[0].entity.ktElement?.text).isEqualTo("println(i)")
assertThat(findings[0].entity.ktElement.text).isEqualTo("println(i)")
}

@Test
Expand Down Expand Up @@ -234,7 +234,7 @@ class MandatoryBracesLoopsSpec {
val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings[0].entity.ktElement?.text).isEqualTo("println()")
assertThat(findings[0].entity.ktElement.text).isEqualTo("println()")
}

@Test
Expand Down Expand Up @@ -307,7 +307,7 @@ class MandatoryBracesLoopsSpec {
val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings[0].entity.ktElement?.text).isEqualTo("println()")
assertThat(findings[0].entity.ktElement.text).isEqualTo("println()")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,8 @@ class FakeKtElement(private val psiFile: PsiFile = FakePsiFile("")) : KtElement
override fun textMatches(p0: PsiElement): Boolean = false

override fun textToCharArray(): CharArray = "".toCharArray()

override fun toString(): String {
return "FakeKtElement"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ class FindingsAssert(actual: List<Finding>) :
return this
} else {
failWithMessage("Expected ${expected.size} findings but was 0")
// This should never execute. `failWithMessage` always throws an exception but the kotlin compiled
// doesn't know that. So this line below helps it.
error("This should never execute, if you find this please open an issue with a reproducer")
}
}
val code = requireNotNull(finding?.entity?.ktElement?.run { containingKtFile.text }) {
"Finding expected to provide a KtElement."
}
val code = finding.entity.ktElement.containingKtFile.text

val textLocations = expected.map { snippet ->
val index = code.indexOf(snippet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,5 @@ fun Rule.compileAndLintWithContext(
fun Rule.lint(ktFile: KtFile): List<Finding> = visitFile(ktFile).filterSuppressed(this)

private fun List<Finding>.filterSuppressed(rule: Rule): List<Finding> {
return filterNot {
it.entity.ktElement?.isSuppressedBy(rule.ruleId, rule.aliases, RuleSet.Id("NoARuleSetId")) == true
}
return filterNot { it.entity.ktElement.isSuppressedBy(rule.ruleId, rule.aliases, RuleSet.Id("NoARuleSetId")) }
}

0 comments on commit 726c394

Please sign in to comment.