DEV Community

Kevin Furjan
Kevin Furjan

Posted on • Originally published at kevin-furjan.hashnode.dev

Enforce Swift style and conventions with SwiftLint

What is a linter

Linter is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs. In other words, linter checks the source code for programmatic as well as stylistic errors. By doing that, it is helpful in identifying some common and uncommon mistakes that are made during coding, meaning that it helps in increasing the code's reliability and maintaining a higher level of code discipline.

What is SwiftLint

Well, it's a linter tool to enforce style and conventions for Swift programming language. SwiftLint enforces the style guide rules that are generally accepted by the Swift community, described in style guides like Ray Wenderlich's Swift Style Guide.

Installation

SwiftLint can be installed on OS level using Homebrew package manager with the following command: brew install swiftlint.

If you, however, want to install SwiftLint as dependency only to your project, you can do that using CocoaPods by adding pod 'SwiftLint' to your Podfile. This will download the SwiftLint binaries and dependencies in Pods/ folder during your next pod install execution.

Xcode configuration

SwiftLint can be integrated into your Xcode project to get warnings and errors displayed in the issue navigator. To enable this, click the Project in the file navigator, then click the primary app target, and go to Build Phases. Click the + and select "New Run Script Phase". Finally, add the following shell script:

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Enter fullscreen mode Exit fullscreen mode

Note: If you've installed SwiftLint via CocoaPods the script should look like this "${PODS_ROOT}/SwiftLint/swiftlint".

Rules

SwiftLint contains more than two hundred rules which are enforcing style and conventions for Swift programming language. Full list of SwiftLint rules can be found in Rule Directory.

Rules can be disabled or can be customized, by adjusting threshold for warnings and errors. This can be achieved using configuration file .swiftlint.yml or by disabling a rule directly in code.

Configuration file

Using .swiftlint.yml configuration file enables control over which rule is disabled/enabled and what thresholds are set for warnings and errors for a given rule. Also, it enables excluding some parts of your project, such as Pods, so SwiftLint won't check them. Configuration file needs to be manually created and added to project root directory.

Below is example of .swiftlint.yml configuration file that I personally use in SwiftUI projects.

excluded: # paths to ignore during linting
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift

disabled_rules: # rule identifiers turned on by default to exclude from running
  - multiple_closures_with_trailing_closure

force_cast: warning # implicitly
force_try:
  severity: warning # explicitly

file_length:
  warning: 400
  error: 700

type_name:
  min_length: 2 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
identifier_name:
  min_length: 2 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
Enter fullscreen mode Exit fullscreen mode

This configuration file will tell SwiftLint to exclude folders from linting where project dependencies are stored. It also optimizes some rules to be easier to work with when writing Swift and SwiftUI code. Example, by disabling multiple_closures_with_trailing_closure rule.

Disabling rules in code

Rules can be disabled with a comment inside a source file with the following format: // swiftlint:disable <rule>. The rule will be disabled until the end of the file or until the linter sees a matching enable comment: // swiftlint:enable <rule>.

When writing SwiftUI code, disabling rules in code can come in handy when dealing with SwiftUI Previews which can sometimes lead to longer line length. In that case, line_length rule can be disabled with // swiftlint:disable line_length:

// swiftlint:disable line_length

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        ... some long Swift code ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Thank you for reading and I hope this article was useful to you! In conclusion, this article went over SwiftLint, introduction to it and how you can use it in your own projects.


If you like my content and find it useful, please consider following me. If you are feeling extra generous, please consider buying me a coffee.

Connect with me on LinkedIn.

References

Top comments (0)