Skip to content

Commit

Permalink
Ignore properties starting with _ in backing-property-naming rule
Browse files Browse the repository at this point in the history
Name of an overridden property can only be changed by changing the name of the base property.

Closes #2748
  • Loading branch information
paul-dingemans committed Jul 23, 2024
1 parent ce05eac commit b7c3731
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.INTERNAL_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OVERRIDE_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIVATE_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROTECTED_KEYWORD
Expand Down Expand Up @@ -53,7 +54,10 @@ public class BackingPropertyNamingRule :
property
.findChildByType(IDENTIFIER)
?.takeIf { it.text.startsWith("_") }
?.let { identifier ->
?.takeUnless {
// Do not report overridden properties as they can only be changed by changing the base property
it.treeParent.hasModifier(OVERRIDE_KEYWORD)
}?.let { identifier ->
visitBackingProperty(identifier, emit)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,18 @@ class BackingPropertyNamingRuleTest {
""".trimIndent()
backingPropertyNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2748 - Given an override property with name starting with '_' then do not report a violation`() {
val code =
"""
// The property "__foo" in example below can be defined in an external dependency, which can not be changed. Even in case it is
// a (internal) dependency that can be changed, the violation should only be reported at the base property, but on the overrides
val fooBar =
object : FooBar {
override val __foo = "foo"
}
""".trimIndent()
backingPropertyNamingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit b7c3731

Please sign in to comment.