iOS/Common

[Swift] Let's use SwiftLint (feat. swiftlint exclude pods not working)

TDCIAN 2022. 3. 23. 16:10

Hi! this is TDCIAN!

 

Today, let's find out how to use SwiftLint.

 

SwiftLint is a wonderful tool that makes you follow Swift coding conventions.

If you are a developer who works alone, or just want to know 'good' coding conventions,

SwiftLint will help you!

 

click and move to SwiftLint page

 

There are some ways to use SwiftLint, and I will introduce the way using SwiftLint with Homebrew.

 

First, turn on your CLI tool.

(I will assume that you already installed 'homebrew')

and type 'brew install swiftlint'.

brew install swiftlint

 

If you installed SwiftLint successfully, then let's make a Xcode project.

 

 

I made a Xcode project 'WantToUseSwiftLint'

Click your project file, and choose 'TARGETS', and 'Build Phases' tab.

Click + button under 'General' tab, and choose 'New Run Script Phase'.

As you show, 'Run Script' is added to Build Phases list.

If you click 'Run Script' title, then you can change the name.

 

In this time, I wll change this name as 'Swift Lint'.

You can change this name with whatever you want.

If you click disclosure button(>), you can edit your Run Script.

 

 

export PATH="$PATH:/opt/homebrew/bin"
if which swiftlint > /dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Copy and paste the code in the code block.

 

you can also find this contents on SwiftLint Github page.

 

That's all! Let's build project.

 

Even though I didn't code anything, there are so many warnings.

It means that you installed SwiftLint successfully into your project.

 

 

However, there are still some steps to use SwiftLint.

 

Let's add a empty file to your project.

 

and save as .swiftlint.yml

 

If you click 'Create' button, than an alert pops up like this.

just click 'Use "."'

 

then, another alert will pop up like this.

Don't worry, just click 'Use .yml'.

 

and copy and paste thsese code into your .swiftlint file.

# By default, SwiftLint uses a set of sensible default rules you can adjust:
disabled_rules: # rule identifiers turned on by default to exclude from running
  - colon
  - comma
  - control_statement
opt_in_rules: # some rules are turned off by default, so you need to opt-in
  - empty_count # Find all the available rules by running: `swiftlint rules`

# Alternatively, specify all rules explicitly by uncommenting this option:
# only_rules: # delete `disabled_rules` & `opt_in_rules` if using this
#   - empty_parameters
#   - vertical_whitespace

included: # paths to include during linting. `--path` is ignored if present.
  - Source
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze`
  - explicit_self

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
  severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
type_body_length:
  - 300 # warning
  - 400 # error
# or they can set both explicitly
file_length:
  warning: 500
  error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
  min_length: 4 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: iPhone # excluded via string
  allowed_symbols: ["_"] # these are allowed in type names
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging)

 

Just like this.

 

 

You can also check this step in SwiftLint Github page.

 

Congraturations!

I believe you are ready to use SwiftLint.

 

Let's handle a case.

In the 'ViewController.swift' file, you can show two warnings.

 

Let's fix first warning(Vertical Whitespace Violation: Limit vertical whitespace to a single empty line. Currently 2. (vertical_whitespace)) with two ways.

 

 

First way: Follow the guide of SwiftLint!

Just follow the SwiftLint guide, remove a vertical whitespace, then you can remove warning.

 

 

Second way: Change the rule!

You met this warning again, but this time, I will never change my code. I will change rule.

Let's show .swiftlint file.

You can add some rules that you don't want apply to your project.

 

In the ViewController file, you can check the whole warning.

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line. Currently 2. (vertical_whitespace)

 

If you do not want to apply this rule into your project,

then just type 'vertical_whitespace' under 'disabled_rules' of  .swiftlint file just like this.

 

 

Now, let's build the project again!

 

As you see, there are still two lines of empty vertical whitespace, but you can't show warning about this.

Because we changed the rule!

 

Let's change another warning

 

 

 

I removed another rule(trailing_newline) by adding into disabled_rules.

 

 

From now, let's handle Pod problem,

I believe that you found this post with

'swiftlint exclude pods not working'.

 

I know it sucks. Let's fix it!

 

first, let's install a library with CocoaPods.

I will install RxSwift.

 

 

 

After installation, I built project with xcworkspace file...

 

 

There are so many warnings and erros!

 

Maybe you already checked .swiftlint file

Then you may think

''- Pods' is already in excluded! Why excluded not working!?!?'

 

 

Calm down and fix it.

 

 

As you see, now .swiftlint.yml file is under 'WantToUseSwiftLint' folder, not under project file.

 

Let's move .swiftlint.yml file under project file like this.

 

Let's clean build files with 'command + shift + K' and then build the project again.

 

Now, excluded of .swiftlint.xml works well.

 

However, we met another error!

 

Command PhaseScriptExecution failed with a nonzero exit code

 

Let's move to .swiftlint.yml file again.

 

Then you can show included above excluded that we handled.

 

Change 'Source' with your project name.

In my case, project name is 'WantToUseSwiftLint'

 

Let's clean build files with 'command + shift + K' and then build the project again.

 

 

You nailed it! Congraturations!