Skip to content

Commit

Permalink
Do not flag a (potential) mutable extension property in case the gett…
Browse files Browse the repository at this point in the history
…er is annotated or prefixed with a modifier

Closes #2024
  • Loading branch information
paul-dingemans committed May 14, 2023
1 parent 48ffc6e commit 741b16e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

* Do not flag a (potential) mutable extension property in case the getter is annotated or prefixed with a modifier `property-naming` ([#2024](https://github.com/pinterest/ktlint/issues/2024))

### Changed

## [0.49.1] - 2023-05-12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pinterest.ktlint.ruleset.standard.rules
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONST_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FILE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.GET_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_DECLARATION
Expand Down Expand Up @@ -102,10 +103,7 @@ public class PropertyNamingRule :
}
}

private fun ASTNode.hasCustomGetter() =
findChildByType(PROPERTY_ACCESSOR)
?.firstChildNode
?.text == "get"
private fun ASTNode.hasCustomGetter() = findChildByType(PROPERTY_ACCESSOR)?.findChildByType(GET_KEYWORD) != null

private fun ASTNode.hasConstModifier() = hasModifier(CONST_KEYWORD)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ class PropertyNamingRuleTest {
propertyNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a top level property extension function having a custom get function and not in screaming case notation then do not emit`() {
val code =
"""
val fooBar1: Any
get() = foobar() // Lint can not check whether data is immutable
val fooBar2
inline get() = foobar() // Lint can not check whether data is immutable
val fooBar3
@Bar get() = foobar() // Lint can not check whether data is immutable
""".trimIndent()
propertyNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a backing val property name having a custom get function and not in screaming case notation then do not emit`() {
val code =
Expand Down

0 comments on commit 741b16e

Please sign in to comment.