pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.06k stars 504 forks source link

Rule to disable usage of any class from specified package.... Example disable java.time.* in order to use kotlinx.datetime instead. #2665

Closed urosjarc closed 1 month ago

urosjarc commented 1 month ago

Expected Rule behavior

Many times because the inherited nature of kotlin from JDK there are some java and kotlin packages that are using same class naming. These namings creates confusion in the project which types developer should use... Company would want to enforce the use of only packages that team leaders agree upon and pass this decision down the hierarchy.

Rule should be able to prevent developer to use any class from the specified package, for example java.time.*

paul-dingemans commented 1 month ago

Thanks for your idea.

I don't think this fits the use case for the rules provided by Ktlint. Prohibiting unwanted imports has nothing to do with the way the code in a file is formatted. It is about forbidding the usage of certain imports. Next to that the list of unwanted imports varies per situation (e.g. project, company, or something else). It would mean that the list of unwanted imports would need to be configured somewhere outside of Ktlint code.

It will however not be that difficult to develop a custom rule (in a custom ruleset) and apply that ruleset within your company. Additional benefit is that the list of unwanted imports can be coded inside the rule. Of course all projects in your company would need to use this ruleset. Please see https://pinterest.github.io/ktlint/latest/api/custom-rule-set/ for some references to create a custom rule (set).

urosjarc commented 1 month ago

Thank you for your feedback, it's much appreciated! I agree with your view.