Skip to content

Commit

Permalink
Closes #1847
Browse files Browse the repository at this point in the history
  • Loading branch information
diphtongue committed Dec 8, 2023
1 parent b30404e commit 303d656
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Checks that CONSTANT (treated as const val from companion object or class level) is in non UPPER_SNAKE_CASE
- name: CONSTANT_UPPERCASE
enabled: true
configuration:
exceptionConstNames: "serialVersionUID"
# Checks that enum value is in upper SNAKE_CASE or in PascalCase depending on the config. UPPER_SNAKE_CASE is the default, but can be changed by 'enumStyle' config
- name: ENUM_VALUE
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
)

private fun checkVariableName(node: ASTNode): List<ASTNode> {
val exceptionConstNames = setOf("serialVersionUID")
val configuration = ConstantUpperCaseConfiguration(
configRules.getRuleConfig(CONSTANT_UPPERCASE)?.configuration
?: emptyMap())

val exceptionNames = configuration.exceptionConstNames

// special case for Destructuring declarations that can be treated as parameters in lambda:
var namesOfVariables = extractVariableIdentifiers(node)

Expand All @@ -179,7 +184,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
// check for constant variables - check for val from companion object or on global file level
// it should be in UPPER_CASE, no need to raise this warning if it is one-letter variable
if (node.isConstant()) {
if (!variableName.text.isUpperSnakeCase() && variableName.text.length > 1 && !exceptionConstNames.contains(variableName.text)) {
if (!exceptionNames.contains(variableName.text) && !variableName.text.isUpperSnakeCase() && variableName.text.length > 1) {
CONSTANT_UPPERCASE.warnOnlyOrWarnAndFix(
configRules = configRules,
emit = emitWarn,
Expand Down Expand Up @@ -494,6 +499,10 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
} ?: Style.SNAKE_CASE
}

class ConstantUpperCaseConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
val exceptionConstNames = config["exceptionConstNames"]?.split(',') ?: emptyList()
}

class BooleanFunctionsConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
/**
* A list of functions that return boolean and are allowed to use. Input is in a form "foo, bar".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class IdentifierNamingWarnTest : LintTestBase(::IdentifierNaming) {
RulesConfig(FUNCTION_BOOLEAN_PREFIX.name, true,
mapOf("allowedPrefixes" to "equals, equivalent, foo"))
)
private val rulesConfigConstantUpperCase = listOf(
RulesConfig(CONSTANT_UPPERCASE.name, true, mapOf(
"exceptionConstNames" to "serialVersionUID"
))
)

// ======== checks for generics ========
@Test
Expand Down Expand Up @@ -205,7 +210,8 @@ class IdentifierNamingWarnTest : LintTestBase(::IdentifierNaming) {
private const val serialVersionUID: Long = -1
}
}
""".trimIndent()
""".trimIndent(),
rulesConfigList = rulesConfigConstantUpperCase
)
}

Expand Down

0 comments on commit 303d656

Please sign in to comment.