nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

Extend preferInferredTypes rule to support if/switch expressions #1655

Closed calda closed 3 months ago

calda commented 3 months ago

The PR extends the preferInferredTypes rule to support if/switch expressions.

Previously this example was left as-is:

let foo: Foo =
    if condition {
        .init(bar)
    } else {
        .init(baaz)
    }

Now it's converted to:

let foo =
    if condition {
        Foo(bar)
    } else {
        Foo(baaz)
    }
codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 96.87500% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 95.17%. Comparing base (9ce956c) to head (ede2953).

Files Patch % Lines
Sources/Rules.swift 95.45% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1655 +/- ## =========================================== - Coverage 95.18% 95.17% -0.01% =========================================== Files 20 20 Lines 22498 22551 +53 =========================================== + Hits 21414 21464 +50 - Misses 1084 1087 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nicklockwood commented 3 months ago

The case for this is much less strong, e.g. if the type is something long like UnsafeMutablePointer<VeryLongName> or whatever then you'll now be repeating it multiple times.

I think at the very least it warrants another configuration option, but I wonder if there's maybe a sensible heuristic, like to only apply it for simple class names, or ones below a certain character count?

calda commented 3 months ago

Good thinking @nicklockwood, you're right. I added an option for this that is disabled by default.