nicklockwood / SwiftFormat

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

Update conditionalAssignment rule to support more complex lvalues #1647

Closed calda closed 6 months ago

calda commented 6 months ago

This PR updates the conditionalAssignment rule to support more complex lvalues. Previously it required that the value being assigned was just a simple identifier like foo =. Now it supports more complex lvalues like foo?.bar =, foo["bar"] =, etc.

For example, this sample code was left as-is before:

switch condition {
case true:
    property?.foo!.bar["baaz"] = Foo("foo")
case false:
    property?.foo!.bar["baaz"] = Foo("bar")
}

Now it is updated as expected:

property?.foo!.bar["baaz"] =
    switch condition {
    case true:
        Foo("foo")
    case false:
        Foo("bar")
    }

I also added a new --condassignment after-property to let consumers disable the new functionality added in https://github.com/nicklockwood/SwiftFormat/pull/1643. After running the updated rule on our codebase, I found too many of the changes weird and unusual. On the other hand I found the previous behavior (only applying the new syntax after a new property declaration) reasonable almost all of the time.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 95.16%. Comparing base (9ce956c) to head (a11c38d).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1647 +/- ## =========================================== - Coverage 95.18% 95.16% -0.03% =========================================== Files 20 20 Lines 22498 22522 +24 =========================================== + Hits 21414 21432 +18 - Misses 1084 1090 +6 ```

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