diff options
-rw-r--r-- | detekt/detekt.yml | 2 | ||||
-rw-r--r-- | detekt/src/main/kotlin/grammar/AvoidBritishSpelling.kt | 46 | ||||
-rw-r--r-- | detekt/src/main/kotlin/grammar/AvoidColour.kt | 38 | ||||
-rw-r--r-- | detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt | 9 |
4 files changed, 53 insertions, 42 deletions
diff --git a/detekt/detekt.yml b/detekt/detekt.yml index 1b7d3ab05..ac5c9de53 100644 --- a/detekt/detekt.yml +++ b/detekt/detekt.yml @@ -4,7 +4,7 @@ config: GrammarRules: active: true - AvoidColour: # custom rule to prefer color to colour + AvoidBritishSpelling: # custom rule to prefer american spellings over british ones active: true FormattingRules: diff --git a/detekt/src/main/kotlin/grammar/AvoidBritishSpelling.kt b/detekt/src/main/kotlin/grammar/AvoidBritishSpelling.kt new file mode 100644 index 000000000..6a4ea3d61 --- /dev/null +++ b/detekt/src/main/kotlin/grammar/AvoidBritishSpelling.kt @@ -0,0 +1,46 @@ +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 british spelling over the american spelling in the codebase, + * this will ignore any type annotations, i.e., `@ConfigEditorColour` will not be reported. + */ +class AvoidBritishSpelling(config: Config) : Rule(config) { + override val issue = Issue( + "AvoidBritishSpelling", + Severity.Style, + "Avoid using the word british spelling over american spelling.", + Debt.FIVE_MINS, + ) + + private val scannedWords = mapOf( + "colour" to "color", + "armour" to "armor", + ) + + 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. + + for (word in scannedWords) { + if (text.contains(word.key, ignoreCase = true)) { + report( + CodeSmell( + issue, + Entity.from(expression), + "Avoid using the word '${word.key}' in code, '${word.value}' is preferred instead.", + ), + ) + } + } + super.visitStringTemplateExpression(expression) + } +} diff --git a/detekt/src/main/kotlin/grammar/AvoidColour.kt b/detekt/src/main/kotlin/grammar/AvoidColour.kt deleted file mode 100644 index 754148d36..000000000 --- a/detekt/src/main/kotlin/grammar/AvoidColour.kt +++ /dev/null @@ -1,38 +0,0 @@ -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 index 957b20147..8001fe42b 100644 --- a/detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt +++ b/detekt/src/main/kotlin/grammar/GrammarRuleSetProvider.kt @@ -10,8 +10,11 @@ class GrammarRuleSetProvider : RuleSetProvider { override val ruleSetId: String = "GrammarRules" override fun instance(config: Config): RuleSet { - return RuleSet(ruleSetId, listOf( - AvoidColour(config) - )) + return RuleSet( + ruleSetId, + listOf( + AvoidBritishSpelling(config), + ), + ) } } |