Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for incorrect variable name handle #646

Merged
merged 8 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.cqfn.diktat.ruleset.utils.isPascalCase
import org.cqfn.diktat.ruleset.utils.isTextLengthInRange
import org.cqfn.diktat.ruleset.utils.isUpperSnakeCase
import org.cqfn.diktat.ruleset.utils.removePrefix
import org.cqfn.diktat.ruleset.utils.search.findAllVariablesWithUsages
import org.cqfn.diktat.ruleset.utils.toLowerCamelCase
import org.cqfn.diktat.ruleset.utils.toPascalCase
import org.cqfn.diktat.ruleset.utils.toUpperSnakeCase
Expand All @@ -47,16 +48,21 @@ import com.pinterest.ktlint.core.ast.ElementType.CATCH
import com.pinterest.ktlint.core.ast.ElementType.CATCH_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_TYPE
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.isPrivate
import org.jetbrains.kotlin.psi.psiUtil.parents

/**
Expand Down Expand Up @@ -134,10 +140,12 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident
/**
* all checks for case and naming for vals/vars/constants
*/
@Suppress("SAY_NO_TO_VAR")
@Suppress("SAY_NO_TO_VAR", "TOO_LONG_FUNCTION", "ComplexMethod")
private fun checkVariableName(node: ASTNode): List<ASTNode> {
// special case for Destructuring declarations that can be treated as parameters in lambda:
var namesOfVariables = extractVariableIdentifiers(node)
// only local private properties will be autofix in order not to break code if there are usages in other files
val isFix = isFixMode && if (node.elementType == PROPERTY) (node.psi as KtProperty).run { isLocal || isPrivate() } else true
namesOfVariables
.forEach { variableName ->
// variable should not contain only one letter in it's name. This is a bad example: b512
Expand All @@ -154,14 +162,19 @@ class IdentifierNaming(private val configRules: List<RulesConfig>) : Rule("ident
// 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) {
CONSTANT_UPPERCASE.warnAndFix(configRules, emitWarn, isFixMode, variableName.text, variableName.startOffset, node) {
CONSTANT_UPPERCASE.warnAndFix(configRules, emitWarn, isFix, variableName.text, variableName.startOffset, node) {
(variableName as LeafPsiElement).replaceWithText(variableName.text.toUpperSnakeCase())
}
}
} else if (variableName.text != "_" && !variableName.text.isLowerCamelCase()) {
// variable name should be in camel case. The only exception is a list of industry standard variables like i, j, k.
VARIABLE_NAME_INCORRECT_FORMAT.warnAndFix(configRules, emitWarn, isFixMode, variableName.text, variableName.startOffset, node) {
VARIABLE_NAME_INCORRECT_FORMAT.warnAndFix(configRules, emitWarn, isFix, variableName.text, variableName.startOffset, node) {
// FixMe: cover fixes with tests
variableName
.parent({it.elementType == FILE})
?.findAllVariablesWithUsages { it.name == variableName.text }
?.flatMap { it.value.toList() }
?.forEach { (it.node.firstChildNode as LeafPsiElement).replaceWithText(variableName.text.toLowerCamelCase())}
(variableName as LeafPsiElement).replaceWithText(variableName.text.toLowerCamelCase())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,39 @@ private val loWerValue = ""
private val lower = ""
private val valNX256 = ""
private val voiceIpPort = ""

class A {
private val voiceIpPort = ""
public val valN_x2567 = ""

fun foo() {
val voiceIpPorts = ""
val upperSnakers = voiceIpPort
qww(voiceIpPorts)
}

fun goo() {
val qwe = lowerSnake
val pre = valNX256
}
}

class B {
companion object {
val QQ = 1
private val qwe = 11
}

fun foo() {
var qq = 20
while (qq < 20) {
qq = 10
}
}

class Ba {
fun goo() {
val qwe = QQ
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,39 @@ private val loWer_VAlue = ""
private val lower = ""
private val ValN_x256 = ""
private val VoiceIP_port = ""

class A {
private val VoiceIP_port = ""
public val valN_x2567 = ""

fun foo() {
val VoiceIP_ports = ""
val UPPER_SNAKERS = VoiceIP_port
qww(VoiceIP_ports)
}

fun goo() {
val qwe = lower_snake
val pre = ValN_x256
}
}

class B {
companion object {
val QQ = 1
private val QWE = 11
}

fun foo() {
var QQ = 20
while (QQ < 20) {
QQ = 10
}
}

class BA {
fun goo() {
val qwe = QQ
}
}
}