mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Prototype suppressing of rules on file level
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package io.gitlab.arturbosch.detekt.api
|
||||
|
||||
import org.jetbrains.kotlin.preprocessor.typeReferenceName
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
/**
|
||||
@@ -9,7 +10,7 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
*
|
||||
* A rule is implemented using the visitor pattern and should be started using the visit(KtFile)
|
||||
* function. If calculations must be done before or after the visiting process, here are
|
||||
* two predefined (preVisit/postVisit) functions which can be overriden.
|
||||
* two predefined (preVisit/postVisit) functions which can be overriden to setup/teardown additional data.
|
||||
*
|
||||
* @author Artur Bosch
|
||||
*/
|
||||
@@ -51,12 +52,20 @@ abstract class Rule(val id: String,
|
||||
*/
|
||||
open fun visit(root: KtFile) {
|
||||
ifRuleActive {
|
||||
preVisit(root)
|
||||
root.accept(this)
|
||||
postVisit(root)
|
||||
if (!root.isSuppressed()) {
|
||||
preVisit(root)
|
||||
root.accept(this)
|
||||
postVisit(root)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtFile.isSuppressed(): Boolean {
|
||||
return annotationEntries.find { it.typeReferenceName == "SuppressWarnings" }
|
||||
?.valueArguments
|
||||
?.find { it.getArgumentExpression()?.text?.replace("\"", "") == id } != null
|
||||
}
|
||||
|
||||
/**
|
||||
* If custom configurable attributes are provided, use this method to retrieve
|
||||
* properties from the sub configuration specified by the rule id.
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.gitlab.arturbosch.detekt.api
|
||||
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.psi.PsiFileFactory
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.jetbrains.spek.api.dsl.it
|
||||
import java.io.File
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
/**
|
||||
* @author Artur Bosch
|
||||
*/
|
||||
internal class RuleTest : Spek({
|
||||
|
||||
it("rule should be suppressed") {
|
||||
val ktFile = compilerFor("SuppressedObject.kt")
|
||||
val rule = TestRule()
|
||||
rule.visit(ktFile)
|
||||
assertNotNull(rule.expected)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
fun compilerFor(resource: String) = Compiler.compileFromContent(
|
||||
File(Compiler.javaClass.getResource("/$resource").path).readText())
|
||||
|
||||
internal object Compiler {
|
||||
|
||||
private val psiFileFactory: PsiFileFactory
|
||||
|
||||
init {
|
||||
val project = KotlinCoreEnvironment.createForProduction(Disposer.newDisposable(),
|
||||
CompilerConfiguration(), EnvironmentConfigFiles.JVM_CONFIG_FILES).project
|
||||
psiFileFactory = PsiFileFactory.getInstance(project)
|
||||
}
|
||||
|
||||
fun compileFromContent(content: String): KtFile {
|
||||
return psiFileFactory.createFileFromText(KotlinLanguage.INSTANCE, content) as KtFile
|
||||
}
|
||||
}
|
||||
|
||||
class TestRule : Rule("Test") {
|
||||
var expected: String? = "Test"
|
||||
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
|
||||
expected = null
|
||||
}
|
||||
}
|
||||
11
detekt-api/src/test/resources/SuppressedObject.kt
Normal file
11
detekt-api/src/test/resources/SuppressedObject.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
@file:SuppressWarnings("Test")
|
||||
|
||||
/**
|
||||
* @author Artur Bosch
|
||||
*/
|
||||
object SuppressedObject {
|
||||
|
||||
fun stuff() {
|
||||
println("Stuff")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user