nicklockwood / SwiftFormat

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

Error thrown for missing '{' in seemingly valid code #1061

Closed christiankm closed 2 years ago

christiankm commented 2 years ago

We have 2 places in our project where SwiftFormat fails to parse the code and therefore throws errors:

christian@MacBook-Pro-tilhrende-Christian ios-spring % swiftformat --lint .
Running SwiftFormat...
(lint mode - no files will be changed.)
Reading config file at /Users/christian/Developer/ios-spring/.swiftformat
error: Expected { on line 232 in /Users/christian/Developer/ios-spring/Modules/AppCoordinator/AppCoordinator.swift.
error: Expected { on line 65 in /Users/christian/Developer/ios-spring/Modules/Recharge/RechargeWithVoucher/Model/VoucherRechargeDataHandler.swift.

We have been able to resolve these errors, and complete parsing by restructuring the code a bit, though it's not ideal. We feel it's a bug in SwiftFormat. Or perhaps some conflicting rules.

This code causes a problem:

private func handleGenericError(_ error: Error) {
        if let requestableError = error as? RequestableError,
           case let .underlying(error as NSError) = requestableError, // <--- throws error about missing '{' here
           error.code == NSURLErrorNotConnectedToInternet
        {
            print("Error handled")
        } else {
            showGenericError()
        }
    }

Please see our swift format configuration (inside folder), and an example project to reproduce the problem.

Let me know if I can help! SwiftFormat-Example-Project.zip

nicklockwood commented 2 years ago

@christiankm fixed in 0.48.18

christiankm commented 2 years ago

Thank you very much @nicklockwood!