Is there a quality swift analogue for javascript's prettier?

I did some googling and am finding a bunch of highly configurable tools -- and configurability is 100% what I don't want ... I'm looking for the swift formatting tool that is most philosophically similar to javascript's prettier (https://prettier.io) -- (nearly) no config options, just good defaults.

I don't want to ever spend any time discussing how the code formatter should be configured or resolving configuration differences across developer machines -- or at least take steps which actively keep such disagreements to an absolute minimum ...

Its my experience with similar tools in various languages (prettier, rufo vs rubocop for ruby) that NOT having configuration options is a massive engineering win -- so I tend to look for the maximally opinionated code formatter immediately when adopting tools for a new language ecosystem as they tend to produce the best developer experiences (by a large margin).

As I understand it, not allowing extensive configurability options allows the formatter to make much better decisions overall with much higher reliability as appropriate heuristics can be built to handle all manner of scenarios that would otherwise be impossible to solve for when also trying to satisfy all constraints associated with all possible configuration options.

I won't attempt to adjudicate a comparison of them, but the two most commonly used tools that I've seen are swiftformat and swiftlint (there's a third, swift-format, although I don't know of folks tending to use that one over the previous two). Both provide integrations to "auto-fix", a large selection of customizable choices, and not-unreasonable defaults.

I leave any "best of..." comparison for you to judge for your own use case. Both swift format and SwiftLint provide notable value "out of the box".

1 Like

SwiftLint is not primarily a formatter. It’s formatting is largely limited to correcting the issues it finds as a linter. SwiftFormat is really the best out there at the moment.

2 Likes

It should be noted that swift-format is the official one and will eventually be included in the Swift toolchain when it is more complete.

2 Likes

In Xcode:

Cmd-A
Ctrl-I

Is this not good enough?

Thank you for the replies!

SwiftFormat/SwiftLint seem to expose quite a few configuration knobs ... I'm not sure specifically what all those knobs are for -- but I'd be worried that some of those would be pretty unlikely to compose well if actually customized (in terms of producing code scenarios that look obviously poor after formatting) -- and just having all those options can mean more effort required to correctly synchronize the team onto the same rulesets ... -- https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md

It should be noted that swift-format is the official one and will > eventually be included in the Swift toolchain when it is more complete.

swift-format seems promising with only a small set of config options -- https://github.com/apple/swift-format/blob/main/Documentation/Configuration.md. Its not super obvious what the project goals are though -- I hope the philosophy driving that effort is solidified enough that it won't just grow an infinite set of poorly justified config knobs over time ... I hope that project is able to rally consensus as controversy arises rather than just growing config knobs on every disagreement ... Anybody know how complete/not complete swift-format is - is it considered ready to use?

Cmd-A
Ctrl-I

Is this not good enough?

IMO -- definitely not. All that does is adjust the leading spaces on the line, doesn't do anything to correct or normalize formatting choices made within the line ... Good formatter allows you to type whatever is easiest to type and then just auto-format that it into a 'good enough or best' format for readability while also minimizing diff noise and helping to reduce non-semantic version control conflicts ...

3 Likes

If you don’t care about configuration, don’t use it. All of the tools have reasonable defaults.

4 Likes

Hey! Do you have any sources about swift-format being included in Swift toolchain? Since I failed to find any sources.

It was the whole point of SE‐0250 and much work was done toward that goal (which you can see from PRs such as this one). Whether or not that is still the plan years later is a question you would have to ask its development team. @allevato, @ahoppen?

swift-format is included in the open source Swift toolchains that can be downloaded from Swift.org - Download Swift. It is currently not included in Xcode.

2 Likes

As of Xcode 16, swift-format is included: GitHub - swiftlang/swift-format: Formatting technology for Swift source code

1 Like