-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Add ktlint
Kotlin linter
#3649
Add ktlint
Kotlin linter
#3649
Conversation
reported and fixed by `no-blank-line-before-rbrace` ktlint rule
reported and fixed by `no-consecutive-blank-lines` ktlint rule
reported and fixed by `final-newline` ktlint rule
reported and fixed by `colon-spacing` ktlint rule
reported and fixed by `comma-spacing` ktlint rule
reported and fixed by `curly-spacing` ktlint rule
reported and fixed by `keyword-spacing` ktlint rule
reported and fixed by `op-spacing` ktlint rule
reported and fixed by `paren-spacing` ktlint rule
reported and fixed by `comment-spacing` ktlint rule
reported and fixed by `parameter-list-wrapping` ktlint rule
reported and fixed by `chain-wrapping` ktlint rule
reported and fixed by `indent` ktlint rule
reported and fixed by `no-multi-spaces` ktlint rule
reported and fixed by `string-template` ktlint rule
reported and fixed by `import-ordering` ktlint rule
reported by `no-wildcard-imports` ktlint rule, manually fixed
reported and fixed by `no-unused-imports` ktlint rule
reported by `filename` ktlint rule, manually fixed
This comment has been minimized.
This comment has been minimized.
* PR workflow now always passes (green check mark), adds file annotations anyway * push workflow marks failures as before
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Next iteration: The workflow now fails again if there are lint issues, but clicking "Details" immediately opens the list of lint issues. Also, a comment is automatically created that explains what to do now. See again the demo PR. "Proposed edits" like @matkoniecz suggested seem to be too difficult to implement. Also, I did now try the Gradle plugin, but it requires manual configuration in Android Studio, so I think it's too complicated. However, Android Studio's code style settings are now checked into git, so just formatting the code inside Android Studio now fixes most lint issues. |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Mateusz Konieczny <matkoniecz@gmail.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'll summarize the discussions about specific rules in this comment, and link to the original comments from here. Then they can be hidden/collapsed to keep this thread manageable. I'll also highlight the (current) concensus: ✔ accepted, ⛔ rejected, ❔ unclear. ✔ ✔ ✔ ✔
|
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" /> | |
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" /> |
@westnordost wrote: Why are wildcard imports considered to be bad?
@smichel17 wrote: Wildcard imports make it harder to browse the source code as plain text. Explicit imports means I can search in the file to find where something is imported from.
Why are explicit imports bad? That's a rhetorical question; we've had this conversation before, so I know the answer. The point of the question is to show that "you should use better tooling" works in both directions:
A smart IDE can auto-import and collapse the imports, making them effectively equivalent to wildcards, for people using the IDE. People who opt for simple tooling do not have this option, so we should style for them by default.
✔ b622664
: Remove unused imports (no-unused-imports
)
✔ 96dea18
: Rename file to match class name (filename
)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I've finally took the time to look at this.
So-so. The rules as summarized by you are okay. I'd not like to loose the ❔stuff though, I think it makes it more readable. What I definitely don't want, and I think Mateusz mentioned it before, is some automatic lint check on GitHub, that prompts users to change their formatting when they create a PR, as seen here. |
...c/androidTest/java/de/westnordost/streetcomplete/quests/existence/CheckExistenceTitleTest.kt
Show resolved
Hide resolved
app/src/androidTest/java/de/westnordost/streetcomplete/data/osm/mapdata/RelationDaoTest.kt
Show resolved
Hide resolved
app/src/main/java/de/westnordost/streetcomplete/data/edithistory/Edit.kt
Show resolved
Hide resolved
app/src/main/java/de/westnordost/streetcomplete/data/elementfilter/ElementFilterExpression.kt
Show resolved
Hide resolved
app/src/main/java/de/westnordost/streetcomplete/data/elementfilter/OverpassQueryCreator.kt
Show resolved
Hide resolved
app/src/test/java/de/westnordost/streetcomplete/quests/AddCyclewayTest.kt
Outdated
Show resolved
Hide resolved
app/src/test/java/de/westnordost/streetcomplete/quests/AddPostboxCollectionTimesTest.kt
Outdated
Show resolved
Hide resolved
Thanks for the review @westnordost! Regarding the rules: The ones you don't like are
Why not? Do you think it's intimidating for new contributors? Or that it's a waste of time for them? I'd argue that it actually helps save time for both contributors and reviewers: When commits with lint issues are pushed to a PR, the contributor won't know at first. Then once a reviewer looks over the PR and notices a lint error, they have to make a decision whether to ask the contributor to fix it, or do it themselves now or later, or just ignore it. Only in the first case will the contributor learn that they should have formatted something in specific way; and they will only learn "why" if the reviewer has put effort into explaining that. An automatic lint check can do that immediately without the contributor having to wait for a review. And we can customize that message to make it less intimidating and more helpful. Also note that a failing check does not prevent merging (although you could require certain checks to pass in the repo settings). So in the best case, this exact lint error is avoided right from the start the next time. In the worst case, the contributor is so intimidated that they refrain from ever contributing again. I find that quite unlikely.
If you expect most PRs to be compliant, why are you against an automatic PR lint check then?
I agree that this would be the best case. Unfortunately, while there are Gradle plugins, I couldn't get them to run automatically during coding or building. They seem to require manual configuration for each user. |
Yes, both. And a waste of time for the reviewers to point to. The linter is not even about catching possible issues, but about code style (spaces after commas and so forth), it is really unimportant.
I expect them to be generally compliant with the code style because people contributing tend to copy the style the find.
Well, that sounds good. Let's do that then. Is this already on master? |
Alright, then let's not include the PR lint check for now. I'd still add the check on the master branch though, as it will only cause a red cross ❌ on a failing commit there. That's nothing a non-regular contributor is likely to see. Maybe this can be discussed again if/when there are more elaborate code smell lint checks.
No. I'll cherry-pick that from this PR and open a new PR. |
I'll close this PR now, and create separate a PR that fixes the accepted lint rules. |
Before reading on: Yes, there are an awful lot of changes in this PR. But I think it's worth it.
About ktlint
ktlint is an easy-to-use linter for Kotlin. Most of the reported issues can also be fixed automatically. It tries to implement the official Kotlin code style without having to configure anything.
It is configurable though: Rules we don't like can be disabled through
.editorconfig
.And it is extensible: We could write a custom ruleset someday that e.g. checks the property order in quest type classes (see #3526 and 2fcfd10) – with a real AST, not with hacky regexes.
How to run ktlint
ktlint
in the repo root to check all Kotlin files, orktlint --format
to fix most reported issues automatically.ktlint --apply-to-idea-project
to adjust the formatting settings of Android Studio to match whatktlint
would produce.ktlint --install-git-pre-commit-hook
orktlint --install-git-pre-push-hook
to automatically runktlint
beforegit commit
ing /git push
ing, so lint issues don't find their way into the repo at all.master
branch. That way, lint issues should be caught quite quickly.Why we should add it
A more consistent codebase is more maintainable for everyone, because there are less decisions to think about. People that are new to the project learn the code style faster, because they are not confused by existing code that actually violates it. There is less to check in code reviews, because the lint issues are caught by an automatic tool already. So it might mitigate #3573 (comment) a bit.
What the linter complains about
That is what makes this PR so huge: There are many parts in the codebase that violate the ktlint code style. I have let ktlint fix most of the issues automatically, and I have not reviewed the changes thoroughly. I did notice some ugly formatting though, e.g. here. I'll fix that later, if needed.
How to proceed
Lint:
and see what code changes the respective rule causes. Please also see this comment for discussions about specific rules.