/ Swift

Linting swift code

It's really great to have a tool that will enforce your team members to follow the same language conventions. Sometimes I realise that I am not using the same coding styles. Shame on me.

SwiftLint to the rescue!

First I was trying to solve this problem with SwiftLint. It's an amazing tool by the awesome realm.io guys, but my problem was that it's too complex, it has a lot of dependencies and it has many many stupid bugs. Anyway, try it for yourself and you will see what I am talking about!

Alternatives?

Erica Sadun has written an article about her alternative lintage solution. This post is a little bit outdated and the source code is not available anymore.

UPDATE: still available on github.

That's it. I have not found any more alternatives. Do you?

Let's build a linter!

Challenge accepted. I am going to make my own linter! Wait. How should I start this? I remember that some people were using a shell script to threat @todo and @fixme comments as warnings...

All these people posted the same thing, but they are not really explaining the logic behind it. So let me give you a quick explanation about this magic: == you just have to create output from your script in a predefined format, Xcode will take care of everything else.==

<file_path>:<line_number>: warning: <message>

So you can add a shell script and do something like this:

echo "./Sources/Tests.swift:10: warning: This is a warning @ line 10."

It will generate a warning inside Test.swift on the 10th line with the message: "This is a warning @ line 10."

That's it! Super easy! Now you have superpowers! It works with errors as well, but I think it's too drastic to add errors from a script file.

After I realised this, it was super easy to build my own linter.

Lint.swift is here!

I worked about a week on this project, and finally it's released. You can download it from github. I refactored the SwiftLint rules in bash, and made some new ones. It's super easy to use, you just have to download the repository to your source directory and add a new build phase in Xcode:

sh "$SRCROOT"/lint.swift/lint

You can find the rules inside the rules directory (thank you Captain obvious!). Of course you can configure or remove anything you want. This project has no dependencies at all! It's pure bash with a lot of awk scripts. Lint.swift has a built-in cache mechanism, as a gratis you will have file with all the code issues inside the lint directory.

Try it out for yourself. I am not telling you that you should switch from SwiftLint. As you can see my solution has some really nice advantages, but in some cases SwiftLint is better than this approach. At least now you can have an alternative solution for linting swift files.

Feel free to contribute, fork or comment on github or twitter!