Using SwiftLint and Danger for Swift Best Practices

SwiftLint 💪

SwiftLint is an open source tool to enforce Swift style and conventions. It is developed by Realm. You can set your coding style rules and force them during development. SwiftLint has a command line tool, Xcode plugin, AppCode and Atom integration. So, it always fits your development environment. It’ll show you warnings and/or errors if you violate the linting rules.

if let _ = Foo.optionalValue { } // Trigers a warning
if case .some(let _) = self {} // Triggers a warning
if foo() { let _ = bar() } // Not triggers a warning
if foo() { _ = bar() } // Not triggers a warning
- 200 # warning
- 300 # error
- Pods

Danger ⚡️

Every project/piece of code has its own specific flow. When the project grows, maintaining and adding new features become harder. Error prone increases. Having coding guidelines and applying best practices are generally not enough. We are human, we make mistakes. Danger can catch basic errors and let us think the harder problems. For example, it can catch common typos or generated file changes that you shouldn’t change by yourself. It can also force you to write tests if you write more than 20 lines of code. The rules are in your hands as same as SwiftLint.

Danger can leave messages, warnings, errors on the PR/MR. Source:
warn "Big PR, consider splitting into smaller" if git.lines_of_code > 600
## Let's check if there are any changes in the project folder
has_app_changes = !git.modified_files.grep(/ProjectName/).empty?
## Then, we should check if tests are updated
has_test_changes = !git.modified_files.grep(/ProjectNameTests/).empty?
## Finally, let's combine them and put extra condition
## for changed number of lines of code
if has_app_changes && !has_test_changes && git.lines_of_code > 20
fail("Tests were not updated", sticky: false)
swiftlint.lint_files inline_mode: true
danger pr https://YOUR_PR_URL --dangerfile=YOUR_DANGERFILE_PATH

Bonus: SwiftLint with Git Hook

If you’re working with different text editors or IDEs which SwiftLint doesn’t support, you can only use command line tools to lint your code. This is an extra step and it’s easy to forget. Good thing, we can automate this. Hook feature in Git is another place to automate things. Basically, Git hooks are scripts where Git executes before or after events such as commit, push, and receive. We can run SwiftLint in one of these scripts. Personally, I’m using SwiftLint in the pre-commit hook while I’m writing Swift in Sublime Text.

The End 😌

Let Danger and SwiftLint handle the trivial stuff for you. From now on, you can skip basic problems and focus on more complicated things during code review. SwiftLint and Danger ensure that everything is in place as you want. Wanna try?

Developer Mind

Thoughts, experiences and knowledge sharing from software developers

Candost Dağdeviren

Written by

iOS/Software Developer. Curious. Writes about tech & science. Editor @theswiftpostco.

Developer Mind

Thoughts, experiences and knowledge sharing from software developers