-
Notifications
You must be signed in to change notification settings - Fork 508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ktlint 1.1.0 incorrectly reports backing property name error when public-facing variable is internal #2448
Comments
This is introduced in #2346 because according to Kotlin Coding conventions:
|
To us, public API means anything that is not private. It really depends where you set the boundaries. If I have an internal class which is used in the same module it still has a public api (what it exposes to other classes within the same gradle module). |
Agree with @PaulWoitaschek here - in multi-module setup internal class/method is a public API within the module. @paul-dingemans can we reopen this issue and make the rule less strict, or configurable? |
I do understand the way you look at it. But other devs with just reason differently, and refer to the strict meaning in the Kotlin Coding conventions. As documented in #2345:
I do not think this important enough to create a configuration option for. See https://pinterest.github.io/ktlint/latest/faq/#can-a-new-toggle-be-added-to-optionally-enabledisable-format-code-in-a-particular-way |
The visibility modifier should not matter for this rule. Take this example from the official Android guidelines: private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(...): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
...
}
override fun onDestroyView() {
_binding = null
} The backing field is made non-nullable with the other field during the lifecycle of this object. IMO this is a valid use case to improve quality of code in places that use this field and to improve overall developer experience. @paul-dingemans could you reconsider this rule? Or at least can this rule be configurable for the Android folks? |
I am still getting following error on below code - "Backing property is only allowed when a matching property or function exists (standard:backing-property-naming)"
I am using "org.jlleitschuh.gradle.ktlint:12.1.0" in my Android studio
Help here to resolve this issue at my end ? and tell me what rule should is add in my .editorConfig to fix this issue ? |
Please use Ktlint CLI if you want more information about the violation. In your example the problem is that the matching property is a private and therefore not qualifies as a backing property according to the style guides.
|
Expected Behavior
property-naming
error is not reported when there is backing property used and the public-facing variable isinternal
(that's the behavior of ktlint 1.0.1)Observed Behavior
ktlint 1.1.0 reports
property-naming
error when the public-facing variable isinternal
.Steps to Reproduce
You can use following or similar code to reproduce the issue
When
internal
is removed fromelementList
, the error is gone. However, I want to be able to use backing property pattern, and hide the property from other modules at the same time.The text was updated successfully, but these errors were encountered: