nicklockwood / SwiftFormat

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

Fix issue where conditional compilation blocks would be sorted incorrectly #1714

Closed calda closed 4 weeks ago

calda commented 1 month ago

This PR fixes a regression introduced by #1678 where conditional compilation blocks would be sorted incorrectly (always at the bottom).

Previously the organizeDeclarations rule supported a partial ordering, where any declaration without a defined type would just preserve its existing relative ordering. The architecture changes to the organizeDeclaration rule now require a total ordering of declarations, so every declaration must have a well defined type.

Since conditional compilation blocks can have multiple nested declarations with different types, we previously just used type: nil for these. #1678 made these a type of their own which was sorted last. Now, we instead use the type of the first declaration in the conditional compilation block. This better matches the previous behavior.

Before

class Foo {

    // MARK: Lifecycle

    init() {}

    // MARK: Internal

    func test() {}

    #if DEBUG
    init() {
        print("Debug")
    }
    #endif

}

After

class Foo {

    // MARK: Lifecycle

    init() {}

    #if DEBUG
    init() {
        print("Debug")
    }
    #endif

    // MARK: Internal

    func test() {}
}
codecov[bot] commented 1 month ago

Codecov Report

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

Project coverage is 95.15%. Comparing base (f487996) to head (de86b97).

Files Patch % Lines
Sources/FormattingHelpers.swift 87.50% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1714 +/- ## =========================================== + Coverage 95.13% 95.15% +0.02% =========================================== Files 20 20 Lines 23124 23130 +6 =========================================== + Hits 21998 22010 +12 + Misses 1126 1120 -6 ```

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