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
Improve binding context management #5130
Closed
BraisGabin
wants to merge
3
commits into
create-authors-rules
from
improve-binding-context-management
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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,9 @@ | ||
plugins { | ||
id("module") | ||
} | ||
|
||
dependencies { | ||
compileOnly(projects.detektApi) | ||
testImplementation(projects.detektTest) | ||
testImplementation(libs.assertj) | ||
} |
22 changes: 22 additions & 0 deletions
22
detekt-authors/src/main/kotlin/io/gitlab/arturbosch/detekt/authors/AuthorsProvider.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,22 @@ | ||
package io.gitlab.arturbosch.detekt.authors | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.RuleSet | ||
import io.gitlab.arturbosch.detekt.api.RuleSetProvider | ||
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault | ||
|
||
/** | ||
* The authors ruleset provides rules that ensures good practices when writing detekt rules | ||
*/ | ||
@ActiveByDefault("1.22.0") | ||
class AuthorsProvider : RuleSetProvider { | ||
|
||
override val ruleSetId: String = "detekt" | ||
|
||
override fun instance(config: Config) = RuleSet( | ||
ruleSetId, | ||
listOf( | ||
RequiresTypeResolutionRulesDoesNotRunWithoutAContext(config), | ||
) | ||
) | ||
} |
79 changes: 79 additions & 0 deletions
79
.../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,79 @@ | ||
@file:Suppress("ForbiddenComment") | ||
|
||
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. | ||
* | ||
* <noncompliant> | ||
* @@RequiresTypeResolution | ||
* class MyRule(config: Config = Config.empty) : Rule(config) { | ||
* override fun visitKtFile(file: KtFile) = | ||
* if (bindingContext == BindingContext.EMPTY) return | ||
* ... | ||
* } | ||
* } | ||
* </noncompliant> | ||
* | ||
* <compliant> | ||
* @@RequiresTypeResolution | ||
* class MyRule(config: Config = Config.empty) : Rule(config) { | ||
* override fun visitCondition(root: 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`" |
1 change: 1 addition & 0 deletions
1
...hors/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider
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 @@ | ||
io.gitlab.arturbosch.detekt.authors.AuthorsProvider |
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,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() | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My 2 cents here: shouldn't we just instead have a
Rule
subclass or a parameter to theRule
constructor, or a property insideRule
that users can override?While a generic
visitCondition
works, it does not prevent from rules accidentally using TR and forgetting to add the@RequiresTypeResolution
annotation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. That is why #5148 is still needed