Skip to content

Commit

Permalink
Ignore override of function in rule function-naming
Browse files Browse the repository at this point in the history
A function override should not be reported as the interface of class that defines the function might be out of scope of the project in which case the function name can not be changed. Note that the function will still be reported at the interface or class itself whenever that interface or class is defined inside the scope of the project.

Closes #2271
  • Loading branch information
paul-dingemans committed Sep 23, 2023
1 parent b135fe8 commit 52d3d16
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Fix indent of multiline object declaration inside class `indent` [#2257](https://github.com/pinterest/ktlint/issue/2257)
* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Ignore override of function in rule `function-naming` [#2271](https://github.com/pinterest/ktlint/issue/2271)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IMPORT_DIRECTIVE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OVERRIDE_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.ruleset.standard.StandardRule
import com.pinterest.ktlint.ruleset.standard.rules.internal.regExIgnoringDiacriticsAndStrokesOnLetters
Expand Down Expand Up @@ -47,7 +50,8 @@ public class FunctionNamingRule : StandardRule("function-naming") {
node.isFactoryMethod() ||
node.isTestMethod() ||
node.hasValidFunctionName() ||
node.isAnonymousFunction()
node.isAnonymousFunction() ||
node.isOverrideFunction()
}?.let {
val identifierOffset =
node
Expand Down Expand Up @@ -86,6 +90,17 @@ public class FunctionNamingRule : StandardRule("function-naming") {
?.nextCodeSibling()
?.elementType

/*
* A function override should not be reported as the interface of class that defines the function might be out of scope of the project
* in which case the function name can not be changed. Note that the function will still be reported at the interface or class itself
* whenever that interface or class is defined inside the scope of the project.
*/
private fun ASTNode.isOverrideFunction() =
findChildByType(MODIFIER_LIST)
?.children()
.orEmpty()
.any { it.elementType == OVERRIDE_KEYWORD }

private companion object {
val VALID_FUNCTION_NAME_REGEXP = "[a-z][A-Za-z\\d]*".regExIgnoringDiacriticsAndStrokesOnLetters()
val VALID_TEST_FUNCTION_NAME_REGEXP = "(`.*`)|([a-z][A-Za-z\\d_]*)".regExIgnoringDiacriticsAndStrokesOnLetters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,18 @@ class FunctionNamingRuleTest {
""".trimIndent()
functionNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2271 - Given an override fun then do not report a violation of the rule as it might not be possible to change the signature of the rule when defined outside scope of project`() {
val code =
"""
// Assume that Bar is defined outside the scope of the project. As of that the function signature can not be changed to comply
// to the function-naming rule. In case that Bar is defined inside the scope of the project, the violation will still be
// reported in the Bar class/interface itself.
class Foo : Bar {
override fun Something()
}
""".trimIndent()
functionNamingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 52d3d16

Please sign in to comment.