diff options
author | David Cole <40234707+DavidArthurCole@users.noreply.github.com> | 2024-09-26 03:56:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-26 09:56:44 +0200 |
commit | 092a29dd8b13c2b04b0b7c259446ab697201dd5e (patch) | |
tree | f3704e259d34bfccef083834cb9106578695a694 /detekt/src/main | |
parent | e90fd65559ef551b29de7d28f4fea3a46cc2a4e6 (diff) | |
download | skyhanni-092a29dd8b13c2b04b0b7c259446ab697201dd5e.tar.gz skyhanni-092a29dd8b13c2b04b0b7c259446ab697201dd5e.tar.bz2 skyhanni-092a29dd8b13c2b04b0b7c259446ab697201dd5e.zip |
Backend: Dekekt (#2547)
Co-authored-by: Linnea Gräf <nea@nea.moe>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Co-authored-by: Cal <cwolfson58@gmail.com>
Diffstat (limited to 'detekt/src/main')
-rw-r--r-- | detekt/src/main/kotlin/formatting/CustomCommentSpacing.kt | 51 | ||||
-rw-r--r-- | detekt/src/main/kotlin/formatting/FormattingRuleSetProvider.kt | 17 | ||||
-rw-r--r-- | detekt/src/main/kotlin/grammar/AvoidColour.kt | 38 | ||||
-rw-r--r-- | detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt | 17 | ||||
-rw-r--r-- | detekt/src/main/kotlin/root.kt | 1 |
5 files changed, 124 insertions, 0 deletions
diff --git a/detekt/src/main/kotlin/formatting/CustomCommentSpacing.kt b/detekt/src/main/kotlin/formatting/CustomCommentSpacing.kt new file mode 100644 index 000000000..aaf7896cf --- /dev/null +++ b/detekt/src/main/kotlin/formatting/CustomCommentSpacing.kt @@ -0,0 +1,51 @@ +package at.hannibal2.skyhanni.detektrules.formatting + +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 org.jetbrains.kotlin.com.intellij.psi.PsiComment + +class CustomCommentSpacing(config: Config) : Rule(config) { + override val issue = Issue( + "CustomCommentSpacing", + Severity.Style, + "Enforces custom spacing rules for comments.", + Debt.FIVE_MINS + ) + + private val allowedPatterns = listOf( + "#if", + "#else", + "#elseif", + "#endif", + "$$" + ) + + override fun visitComment(comment: PsiComment) { + if (allowedPatterns.any { comment.text.contains(it) }) { + return + } + + /** + * REGEX-TEST: // Test comment + * REGEX-TEST: /* Test comment */ + */ + val commentRegex = Regex("""^(?:\/{2}|\/\*)(?:\s.*|$)""", RegexOption.DOT_MATCHES_ALL) + if (!commentRegex.matches(comment.text)) { + report( + CodeSmell( + issue, + Entity.from(comment), + "Expected space after opening comment." + ) + ) + } + + // Fallback to super (ostensibly a no-check) + super.visitComment(comment) + } +} diff --git a/detekt/src/main/kotlin/formatting/FormattingRuleSetProvider.kt b/detekt/src/main/kotlin/formatting/FormattingRuleSetProvider.kt new file mode 100644 index 000000000..a0a969bcf --- /dev/null +++ b/detekt/src/main/kotlin/formatting/FormattingRuleSetProvider.kt @@ -0,0 +1,17 @@ +package at.hannibal2.skyhanni.detektrules.formatting + +import com.google.auto.service.AutoService +import io.gitlab.arturbosch.detekt.api.Config +import io.gitlab.arturbosch.detekt.api.RuleSet +import io.gitlab.arturbosch.detekt.api.RuleSetProvider + +@AutoService(RuleSetProvider::class) +class FormattingRuleSetProvider : RuleSetProvider { + override val ruleSetId: String = "FormattingRules" + + override fun instance(config: Config): RuleSet { + return RuleSet(ruleSetId, listOf( + CustomCommentSpacing(config) + )) + } +} diff --git a/detekt/src/main/kotlin/grammar/AvoidColour.kt b/detekt/src/main/kotlin/grammar/AvoidColour.kt new file mode 100644 index 000000000..754148d36 --- /dev/null +++ b/detekt/src/main/kotlin/grammar/AvoidColour.kt @@ -0,0 +1,38 @@ +package at.hannibal2.skyhanni.detektrules.grammar + +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 org.jetbrains.kotlin.psi.KtStringTemplateExpression + +/** + * This rule reports all usages of the word "colour" in the codebase, + * preferring the 'American' spelling "color" - this will ignore any + * type annotations, i.e., `@ConfigEditorColour` will not be reported. + */ +class AvoidColour(config: Config) : Rule(config) { + override val issue = Issue( + "AvoidColour", + Severity.Style, + "Avoid using the word 'colour' in code, prefer 'color' instead.", + Debt.FIVE_MINS + ) + + override fun visitStringTemplateExpression(expression: KtStringTemplateExpression) { + val text = expression.text // Be aware .getText() returns the entire span of this template, including variable names contained within. This should be rare enough of a problem for us to not care about it. + if (text.contains("colour", ignoreCase = true)) { + report( + CodeSmell( + issue, + Entity.from(expression), + "Avoid using the word 'colour' in code, prefer 'color' instead." + ) + ) + } + super.visitStringTemplateExpression(expression) + } +} diff --git a/detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt b/detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt new file mode 100644 index 000000000..957b20147 --- /dev/null +++ b/detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt @@ -0,0 +1,17 @@ +package at.hannibal2.skyhanni.detektrules.grammar + +import com.google.auto.service.AutoService +import io.gitlab.arturbosch.detekt.api.Config +import io.gitlab.arturbosch.detekt.api.RuleSet +import io.gitlab.arturbosch.detekt.api.RuleSetProvider + +@AutoService(RuleSetProvider::class) +class GrammarRuleSetProvider : RuleSetProvider { + override val ruleSetId: String = "GrammarRules" + + override fun instance(config: Config): RuleSet { + return RuleSet(ruleSetId, listOf( + AvoidColour(config) + )) + } +} diff --git a/detekt/src/main/kotlin/root.kt b/detekt/src/main/kotlin/root.kt new file mode 100644 index 000000000..9b95a398f --- /dev/null +++ b/detekt/src/main/kotlin/root.kt @@ -0,0 +1 @@ +package at.hannibal2.skyhanni.detektrules |