nicklockwood / SwiftFormat

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

Update conditionalAssignment rule to also simplify if/switch expressions that don't immediately follow property declaration #1643

Closed calda closed 3 months ago

calda commented 3 months ago

This PR updates the conditionalAssignment rule to also simplify if/switch expressions that don't immediately follow property declarations.

Previously the conditionalAssignment rule only handled if/switch expressions that followed a property declaration:

// Before
let property: Foo
if condition {
  property = Foo("foo")
} else {
  property = Foo("bar")
}

// After
let property =
  if condition {
    Foo("foo")
  } else {
    Foo("bar")
  }

but did nothing in other cases like this:

if condition {
  property = Foo("foo")
} else {
  property = Foo("bar")
}

Now we apply the simplification to this case as well:

// Before
if condition {
  property = Foo("foo")
} else {
  property = Foo("bar")
}

// After
property =
  if condition {
    Foo("foo")
  } else {
    Foo("bar")
  }
codecov[bot] commented 3 months ago

Codecov Report

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

Project coverage is 95.16%. Comparing base (a0e5a3a) to head (1d88a4d).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1643 +/- ## =========================================== + Coverage 95.14% 95.16% +0.02% =========================================== Files 20 20 Lines 22374 22418 +44 =========================================== + Hits 21287 21335 +48 + Misses 1087 1083 -4 ```

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

nicklockwood commented 3 months ago

Thanks!