-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rule 5.2.2 parameters size ### What's done: Implemented rule Added tests and info into documentations
- Loading branch information
Showing
9 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/FunctionArgumentsSize.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType | ||
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER | ||
import org.cqfn.diktat.common.config.rules.RuleConfiguration | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.common.config.rules.getRuleConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings.TOO_MANY_PARAMETERS | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
|
||
class FunctionArgumentsSize(private val configRules: List<RulesConfig>) : Rule("argument-size") { | ||
|
||
companion object { | ||
const val maxDefaultParameterSize = 5L | ||
} | ||
|
||
private val configuration: FunctionArgumentsSizeConfiguration by lazy { | ||
FunctionArgumentsSizeConfiguration(configRules.getRuleConfig(TOO_MANY_PARAMETERS)?.configuration ?: mapOf()) | ||
} | ||
|
||
private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) | ||
private var isFixMode: Boolean = false | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.elementType == ElementType.FUN) { | ||
checkFun(node, configuration.maxParameterSize) | ||
} | ||
} | ||
|
||
@Suppress("UnsafeCallOnNullableType") | ||
private fun checkFun(node: ASTNode, maxParameterSize: Long) { | ||
val parameterListSize = (node.psi as KtFunction).valueParameters.size | ||
if (parameterListSize > maxParameterSize){ | ||
TOO_MANY_PARAMETERS.warn(configRules, emitWarn, isFixMode, | ||
"${node.findChildByType(IDENTIFIER)!!.text} has $parameterListSize, but allowed $maxParameterSize", node.startOffset, node) | ||
} | ||
} | ||
|
||
class FunctionArgumentsSizeConfiguration(config: Map<String, String>) : RuleConfiguration(config) { | ||
val maxParameterSize = config["maxParameterListSize"]?.toLongOrNull()?: maxDefaultParameterSize | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...t-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/FunctionArgumentsSizeWarnTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.cqfn.diktat.ruleset.chapter5 | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.Warnings.TOO_MANY_PARAMETERS | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.FunctionArgumentsSize | ||
import org.cqfn.diktat.util.LintTestBase | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class FunctionArgumentsSizeWarnTest: LintTestBase(::FunctionArgumentsSize) { | ||
|
||
private val ruleId = "$DIKTAT_RULE_SET_ID:argument-size" | ||
|
||
private val rulesConfigList: List<RulesConfig> = listOf( | ||
RulesConfig(TOO_MANY_PARAMETERS.name, true, | ||
mapOf("maxParameterListSize" to "1")) | ||
) | ||
|
||
@Test | ||
@Tag(WarningNames.TOO_MANY_PARAMETERS) | ||
fun `check simple function`() { | ||
lintMethod( | ||
""" | ||
|fun foo(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {} | ||
|fun foo(a: Int, b: Int, c: Int, d: Int, e: Int, myLambda: () -> Unit) {} | ||
|fun foo(a: Int, b: Int, c: Int, d: Int, myLambda: () -> Unit) {} | ||
|fun foo(a: Int, b: Int, c: Int, d: Int, e: Int, myLambda: () -> Unit) = 10 | ||
|abstract fun foo(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) | ||
""".trimMargin(), | ||
LintError(1, 1, ruleId, "${TOO_MANY_PARAMETERS.warnText()} foo has 6, but allowed 5", false), | ||
LintError(2, 1, ruleId, "${TOO_MANY_PARAMETERS.warnText()} foo has 6, but allowed 5", false), | ||
LintError(4, 1, ruleId, "${TOO_MANY_PARAMETERS.warnText()} foo has 6, but allowed 5", false), | ||
LintError(5, 1, ruleId, "${TOO_MANY_PARAMETERS.warnText()} foo has 6, but allowed 5", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.TOO_MANY_PARAMETERS) | ||
fun `check function with config`() { | ||
lintMethod( | ||
""" | ||
|fun foo(a: Int, b: Int) {} | ||
""".trimMargin(), | ||
LintError(1, 1, ruleId, "${TOO_MANY_PARAMETERS.warnText()} foo has 2, but allowed 1", false), | ||
rulesConfigList = rulesConfigList | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6434c2a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: