Skip to content
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

chain-method-continuation shouldn't trigger on property access with only single method call #2924

Open
FyiurAmron opened this issue Jan 13, 2025 · 0 comments

Comments

@FyiurAmron
Copy link

related to #2455 , mirroring the idea from #2455 (comment) somewhat; results from IMVHO excessive application of chain-method-continuation, coupled with the default value of https://pinterest.github.io/ktlint/1.0.0/rules/configuration-ktlint/#force-multiline-chained-methods-based-on-number-of-chain-operators

A practical example, single println (please ignore the fact it doesn't use string template : ) :

println("ver: " + libs.versions.our.lib.get())

Expected Behavior

Line is left verbatim. Length is OK, the property access via Gradle version catalog accessor it perfectly OK, and the getter for the Property<String> is unfortunately necessary due to Gradle's internals (no good toString() there) - however since we do not have a method chain (only a single explicit method is called, the rest is either accessors or fields or whatever, no () anyway), no chain-method-continuation rule should trigger - since there is no chained method nor any continuation happening)

Observed Behavior

a waterfall of tragedies follows:

  1. first, the chain rule triggers, so it gets reformatted to
println("ver: " + libs.versions.our.lib
    .get())
  1. then, the wrapping and multiline-expression-wrapping rules trigger, resulting in
println(
    "ver: " +
        libs.versions.our.lib
            .get(),
)

It's now a lot harder to read and handle - and, let's be honest - in total that's a real overkill, that could be safely prevented by a tweak to the chain-method-continuation logic :)

Steps to Reproduce

Take any expression that has >= 3 property etc. accesses (even with very short names!), and terminate with a method call.

Note that this won't happen for pure property access, nor for <= 2 property accesses like libs.versieeeeeons.ooooooooour.getLibrary()

Proposed solution

Don't treat a stream of tokens as a "chained method call" if only a single () method is called at the end.

Your Environment

ktlint ruleset 1.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant