mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-04 08:39:27 +00:00
Add library containing detekt rules for apollo plugins
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
package org.apollo.game.plugin.detekt
|
||||
|
||||
import io.gitlab.arturbosch.detekt.api.Config
|
||||
import io.gitlab.arturbosch.detekt.api.RuleSet
|
||||
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
|
||||
import org.apollo.game.plugin.detekt.rules.DeclarationInScriptRule
|
||||
|
||||
class ApolloPluginRuleSetProvider : RuleSetProvider {
|
||||
override val ruleSetId = "apollo-plugin"
|
||||
|
||||
override fun instance(config: Config): RuleSet {
|
||||
return RuleSet(ruleSetId, listOf(
|
||||
DeclarationInScriptRule()
|
||||
))
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package org.apollo.game.plugin.detekt.rules
|
||||
|
||||
import io.gitlab.arturbosch.detekt.api.*
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtObjectDeclaration
|
||||
|
||||
class DeclarationInScriptRule : Rule() {
|
||||
override val issue = Issue(
|
||||
"DeclarationInScript",
|
||||
Severity.CodeSmell,
|
||||
"This rule reports a plugin file containing class or object declarations.",
|
||||
Debt.FIVE_MINS
|
||||
)
|
||||
|
||||
override fun visit(root: KtFile) {
|
||||
super.visit(root)
|
||||
|
||||
val script = root.script ?: return
|
||||
val declarations = script.declarations.filter { it is KtClass || it is KtObjectDeclaration }
|
||||
|
||||
declarations
|
||||
.forEach {
|
||||
report(CodeSmell(
|
||||
issue,
|
||||
Entity.from(it),
|
||||
message = "Declaration of ${it.name} should live in a top-level file, not a script"
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.apollo.game.plugin.detekt.ApolloPluginRuleSetProvider
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package org.apollo.game.plugin.detekt.rules
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.lint
|
||||
import java.nio.file.Paths
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class DeclarationInScriptRuleTest {
|
||||
val rule = DeclarationInScriptRule()
|
||||
|
||||
@Test
|
||||
fun `Finds warning in script file`() {
|
||||
val srcPath = Paths.get(this.javaClass.getResource("/testData/example.kts").toURI())
|
||||
val findings = rule.lint(srcPath)
|
||||
|
||||
assertEquals(1, findings.size)
|
||||
assertEquals("Declaration of ExampleDeclaration should live in a top-level file, not a script", findings[0].message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class ExampleDeclaration {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user