From 741b16e5f568aee3f9f85100893d7129f1cf45c0 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sun, 14 May 2023 20:54:35 +0200 Subject: [PATCH] Do not flag a (potential) mutable extension property in case the getter is annotated or prefixed with a modifier Closes #2024 --- CHANGELOG.md | 2 ++ .../ruleset/standard/rules/PropertyNamingRule.kt | 6 ++---- .../standard/rules/PropertyNamingRuleTest.kt | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b971418b14..428006c0db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt index 9942f64ae7..d3d4a00131 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt @@ -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 @@ -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) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt index 0081183fe8..99768a1aab 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt @@ -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 =