What and Why SwiftLint???
Linter is a specialized tool that analyses our source code and helps us to find errors, bug and indentation issues. As the name suggests SwiftLint helps us to enforce the coding styles on the swift code and makes us follow its rules and conventions. SwiftLint is an open source project and is maintained by Realm team. There are many rules which are defined in SwiftLint. These rules are applied to our code. If any of the compulsory rules are broken SwiftLint will either generate a warning(if not critical) or build will fail(in case of critical). This way developer has to strictly follow the rules before making any pull request and code being merged to the main codebase. This will help us in many ways. In a codebase where many developers are working from junior to senior level, there will be uniformity in writing the code and style. So codebase will be consistent and uniformed throughout. Also, the codebase will be easy to maintain and debug. The more maintainable the code more the scalability.
Setting up...
The first step we will look into how to setup SwiftLint locally on the machine. Since we will be using Brew to install SwiftLint on our machine. The Brew is a must before starting the process. In case Brew is not installed, take a look at the installation process here.
Once brew setup is done, open terminal and type,
brew help
If the brew is installed successfully, you will get an output. If not, command not found error will get displayed on the terminal.
Next step is to install SwiftLint on our machine.
brew install swiftlint
After installation, the next step is to link it to our Xcode project Open Xcode, go to build phases and add new “Run Script Phase” and paste below code there
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Configuration...
Once SwiftLint is set up, in order to work we need to add configurations. We can configure by adding .swiftlint.yml to the root folder of our project directory.
To create
".swiftlint.yml" -> Go to Project directory -> Open terminal -> "touch .swiftlint.yml"
Once the file is created we can configure the following parameters
- disabled_rules: Disable the default enabled rules from the SwiftLint
- opt_in_rules: Opt-in rules which are not set by default
- whitelist_rules: Only the rules specified in this list will be enabled. This will not work alongside opt_in_rules & disabled_rules
- analyser_rules: This is an entirely separate list of rules that are only run by the analyse command
This completes setting up of SwiftLint, Please feel free to comment or ask any questions if there are any queries.
Comments
Post a Comment