ktlint: ktlint 1.1.0 doesn't respect disabled discouraged-comment-location rule in .editorconfig
We don’t want to enforce comments location in our project because we like the following code style:
val object = Object(
someParam1 = 10000, // comment here about why
someParam2 = 100, // comment here about why
sameParam3 = emptyList(), // comment here about why
)
It’s compact and nice to work with, but discouraged by ktlint.
To disable the A comment in a 'value_argument_list' is only allowed when placed on a separate line (cannot be auto-corrected)
warnings for such usages, our .editorconfig
file looks the following way:
root = true
[*]
ktlint_code_style = intellij_idea
ktlint_standard_discouraged-comment-location = disabled
It works for ktlint 1.0.1, but ignored when we try to update ktlint to 1.1.0.
Additional context
We are using org.jlleitschuh.gradle.ktlint:12.0.3 gradle plugin configured the following way:
ktlint {
version.set(libs.versions.ktlint)
coloredOutput.set(true)
android.set(false)
ignoreFailures.set(false)
filter {
exclude("**/generated/**")
exclude { element -> element.file.path.contains("generated/") }
}
}
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Comments: 18 (1 by maintainers)
Commits related to this issue
- Loosen dependencies on `value-argument-comment`, `value-parameter-comment`, `type-argument-comment`, and `type-parameter-comment` rules to be disabled Run `argument-list-wrapping`, `class-signature` ... — committed to pinterest/ktlint by paul-dingemans 6 months ago
- Loosen dependencies on `value-argument-comment`, `value-parameter-comment`, `type-argument-comment`, and `type-parameter-comment` rules to be disabled (#2466) Run `argument-list-wrapping`, `class-sig... — committed to pinterest/ktlint by paul-dingemans 6 months ago
- Loosen dependencies on `value-argument-comment`, `value-parameter-comment`, `type-argument-comment`, and `type-parameter-comment` rules to be disabled (#2466) Run `argument-list-wrapping`, `class-sig... — committed to Goooler/ktlint by paul-dingemans 6 months ago
Looks like a new rule was added for this -
value-argument-comment
Unfortunately disabling it results in:
At this rate I’ll have most ktlint rules disabled by 2025 😞
@Spikhalskiy I see, that you failed to understand. You referring again to “technically from a language perspective” and this is exactly what is wrong - technical limitations should not be an argument when we are taking care about readability. Readability is for human and if technical limitations hinder human needs, then technical limitations should be changed, not the human needs. The ultimate goal of linter is to make it easier for human to work with the code, not for computer or compiler. And now instead of moving to this goal linter becoming a tool with which I have to fight (of suppress) to have readable code.
@paul-dingemans Explanation why these rules are introduced seems reasonable, but it really sad that technical limitation and incorrect behavior of some tools leads to decreasing of code readability. I think linter is suppose to make code more comprehensible and not vice versa.
I think this matches with what I suggested:
Nice things sometimes come at cost. But I do hear your argument that refactoring hundreds of lines of code is not an option for you.
It is technical complex and most likely it will results in lots of other problems when trying to fix this. You are free to submit a PR which handles comments in value argument lists correctly.
This is not a relevant argument. Even if ktlint could handle comments correctly, the problem inside IntelliJ IDEA still exists. The IntelliJ IDEA example was simple to illustrate that even in Jetbrains products this kind of code can lead to unpredictable results.
Next best thing that I can think of, is that the rules that currently have a dependency on any of the comment rules just simply ignore elements containing unexpected comments. But this will also lead to unpredictable results as devs will not understand why
is not being wrapped, while it does work for:
Sorry for not clearly communicating that the
discouraged-comment-location
has been splitted into separate rules:type-argument-comment
type-parameter-comment
value-argument-comment
value-parameter-comment
Reason for this is to provide better insights why certain rules are blocked.
With ktlint
1.0.1
, disablingdiscouraged-comment-location
was only possible when also disabling:chain-method-continuation
class-signature
if-else-wrapping
From a user perspective it makes what the relation is between those rules.With ktlint
1.1.0
, disablingvalue-argument-comment
is only possible when also disabling:argument-list-wrapping
chain-method-continuation
Thechain-method-continuation
should not have been blocked. From a functional perspective, and from the unit test perspective, there seems no reason to block it.Intuitively the relation between
argument-list-wrapping
andvalue-argument-comment
is not too bad even in case you might not agree with disallowing comments in and after value arguments. It is a common pattern to use. But syntaxtically it is not correct as the comment is place after the semi colon while it refers to the element before the comment. Not only ktlint has troubles to process that correctly but also IntelliJ IDEA does not deal with it correctly as can be seen in screenshots below:EDIT As documented in a comment below the same happens when the comments are on separate lines as well.
We start with this code:
Open ‘Change signature’ and select ‘sameParam3’:
Now move ‘sameParam3’ one position up and press ‘Refactor’:
The resulting code now looks as follows:
Please note that the comments are no longer related to the original parameter.
EDIT As documented in a comment below the same happens when the comments are on separate lines as well.
Yes indeed, this should have been documented more clearly. I will fix it.