-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create RequiresTypeResolutionRulesDoesNotRunWithoutAContext
- Loading branch information
1 parent
0264491
commit d79d501
Showing
4 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../gitlab/arturbosch/detekt/authors/RequiresTypeResolutionRulesDoesNotRunWithoutAContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.gitlab.arturbosch.detekt.authors | ||
|
||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
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.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault | ||
import org.jetbrains.kotlin.psi.KtClass | ||
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.findFunctionByName | ||
|
||
/** | ||
* A rule annotated with RequiresTypeResolution should override `visitCondition` and return false if the provided | ||
* `bindingContext` is empty. | ||
* | ||
* <compliant> | ||
* @RequiresTypeResolution | ||
* class MyRule(config: Config = Config.empty) : Rule(config) { | ||
* override fun visitCondition(file: KtFile) = | ||
* bindingContext != BindingContext.EMPTY && super.visitCondition(root) | ||
* } | ||
* } | ||
* </compliant> | ||
*/ | ||
@ActiveByDefault("1.22.0") | ||
class RequiresTypeResolutionRulesDoesNotRunWithoutAContext(config: Config = Config.empty) : Rule(config) { | ||
override val issue = Issue( | ||
javaClass.simpleName, | ||
Severity.Style, | ||
"Rules marked as RequiresTypeResolution shouldn't run without `bindingContext`.", | ||
Debt.FIVE_MINS | ||
) | ||
|
||
override fun visitClass(klass: KtClass) { | ||
if (klass.annotationEntries.map { it.text }.contains("@RequiresTypeResolution")) { | ||
val function: KtNamedFunction? = klass.findFunctionByName("visitCondition") as? KtNamedFunction | ||
|
||
if (function == null) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.atName(klass), | ||
message | ||
) | ||
) | ||
return | ||
} | ||
|
||
if (function.bodyExpression?.text != requiredImplementation) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.atName(function), | ||
message | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private const val requiredImplementation = "bindingContext != BindingContext.EMPTY && super.visitCondition(root)" | ||
private const val message = "This rule is annotated with `RequiresTypeResolution` but `visitCondition` " + | ||
"implementation is not `$requiredImplementation`" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
detekt: | ||
active: true | ||
RequiresTypeResolutionRulesDoesNotRunWithoutAContext: | ||
active: true |
80 changes: 80 additions & 0 deletions
80
...lab/arturbosch/detekt/authors/RequiresTypeResolutionRulesDoesNotRunWithoutAContextSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.gitlab.arturbosch.detekt.authors | ||
|
||
import io.gitlab.arturbosch.detekt.test.assertThat | ||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import org.junit.jupiter.api.Test | ||
|
||
class RequiresTypeResolutionRulesDoesNotRunWithoutAContextSpec { | ||
|
||
private val rule = RequiresTypeResolutionRulesDoesNotRunWithoutAContext() | ||
|
||
@Test | ||
fun `should not report no annotated classes`() { | ||
val code = """ | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
class A(config: Config = Config.empty) : Rule(config) { | ||
override val issue = error("I don't care") | ||
} | ||
""" | ||
val findings = rule.compileAndLint(code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `should report annotated classes without visitCondition()`() { | ||
val code = """ | ||
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
@RequiresTypeResolution | ||
class A(config: Config = Config.empty) : Rule(config) { | ||
override val issue = error("I don't care") | ||
} | ||
""" | ||
val findings = rule.compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `should report annotated classes with visitCondition() but no check for bindingContext`() { | ||
val code = """ | ||
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import org.jetbrains.kotlin.psi.KtFile | ||
@RequiresTypeResolution | ||
class A(config: Config = Config.empty) : Rule(config) { | ||
override val issue = error("I don't care") | ||
override fun visitCondition(root: KtFile) = super.visitCondition(root) | ||
} | ||
""" | ||
val findings = rule.compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `should not report annotated classes with visitCondition() and check for bindingContext first line`() { | ||
val code = """ | ||
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
@RequiresTypeResolution | ||
class A(config: Config = Config.empty) : Rule(config) { | ||
override val issue = error("I don't care") | ||
override fun visitCondition(root: KtFile) = | ||
bindingContext != BindingContext.EMPTY && super.visitCondition(root) | ||
} | ||
""" | ||
val findings = rule.compileAndLint(code) | ||
assertThat(findings).isEmpty() | ||
} | ||
} |