The latest stable release of SwiftLint brings support for Swift 5.6 and Xcode 13.3 to developers and organizations aiming for better code consistency. Additionally, a pre-release version integrates SwiftSyntax as a replacement of SourceKit.
SwiftLint integrates with Xcode and many other tools to make it easier to enforce Swift style and conventions and to write idiomatic Swift code. Maintained since 2015, SwiftLint currently includes over 200 rules derived by the GitHub Swift Style Guide (now archived) and Ray Wenderlich's Swift Style Guide.
As SwiftLint maintainer JP Simard remarks, one of the major benefits reported by SwiftLint users is improving consistency within a single project as well as across projects and teams. This benefits readability and allows developers to focus on what matters and avoid bad practices, he says.
SwiftLint uses SourceKit and the Swift compiler to parse Swift code, as well as to leverage some of their advanced capabilities aimed to automatically correct some rule violations. In fact, about a third of SwiftLint rules are correctable and can be transparently enforced.
Another third of the rules are configurable, meaning, you can change their default behaviour using a configuration file. For example, you can require all array declarations to include a trailing comma, or specifically forbid that. In addition to global configuration, SwiftLint also supports local overrides through specific commands embedded in the source code.
SwiftLint configurability enables its progressive adoption, which can be essential for large teams or large existing codebases. Not all of SwiftLint's rules should be taken for mandatory and Simard strongly advises for tailoring the tool to its users' needs.
You really have to rely on your thought process to know when SwiftLint is wrong or when to ignore SwiftLint. And SwiftLint will be wrong. If SwiftLint was always right, that’d be the compiler’s job. These rules would be in the compiler.
SwiftLint also allows you to define additional rules based on SourceKit syntax. The SwiftLint team has also started supporting SwiftSyntax, a more recent tool to parse, inspect, generate, and transform Swift source code.
As mentioned, after installing it, you can easily integrate SwiftLint in your Xcode project. This requires creating a new post-compilation Build Phase to run the following script:
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: swiftlint not installed"
fi
In addition to Xcode, SwiftLint provides support for AppCode, Atom, Visual Studio Code, and fastlane through their respective plugins. You can also run swiftlint
manually from the command line and use its CLI to integrate it into your CI pipeline.