Try   HackMD

SwiftLint 的安裝與使用

Tue, Jun 1, 2020 17:56 AM

tags: Xcode Swift

SwiftLintRealm 推出的一款 Swift 程式碼規範檢查工具,SwiftLint 基於 Github 公布的 Swift 代码规范 進行程式碼檢查。

一、安裝

  • SwiftLint 目前有三種方式提供安裝,可以根據需求自行選擇,這裡就分享我個人嘗試的兩種安裝方式。

1. Homebrew

這裡就不贅述 Homebrew 的安裝方式。

  1. 終端機輸入 brew install swiftlint

2. CocoaPods

  1. 在專案目錄下的 Podfile 新增 pod 'SwiftLint'
  2. 終端機輸入 pod install

二、SwiftLint 的指令

  • SwiftLint 安裝完畢後,終端機輸入swiftlint help 可以查閱全部相關指令。

三、SwiftLint 的使用

  • 依照圖片順序操作

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 依照不同的安裝方式貼入腳本

1. Homebrew

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

2. CocoaPods

"${PODS_ROOT}/SwiftLint/swiftlint"
  • 這裡是設定一個自動編譯的腳本,在每次編譯時都會執行該腳本。
  • 如果正確安裝 SwiftLint,就會執行 SwiftLint 中的程式碼規範檢查,如果沒有正確安裝,則腳本會拋出沒有安裝 SwiftLint 並提示下載的警告。
  1. 設定完成後,command(⌘) + B 編譯
  • 剛開始你可能會看見 999+ 的黄色警告和 999+ 的紅色錯誤,別擔心;因為 SwiftLint 預設的程式碼規範相對來說較於嚴格。
  • SwiftLint 在完成上述操作之後便已經生效,SwiftLint 全部的規則可以在 Source/SwiftLintFramework/Rules 目錄內找到。

四、自定義配置規範

  • 當你編譯完成後看見 999+ 的錯誤,會發現很多都來自於第三方程式碼,這裡可以做一些設置讓 SwiftLint 在檢查的時候自動忽略 PodsCarthage

1. 建立設定文件

  • 在專案目錄下的建立 .swiftlint.yml
  • 一般的文件大概長這個樣子
disabled_rules: # 禁用指定的规则
  - colon
  - comma
  - control_statement
opt_in_rules: # 启用指定的规则
  - empty_count
  - missing_docs
  # 可以通过执行如下指令来查找所有可用的规则:
  # swiftlint rules
included: # 执行 linting 时包含的路径。如果出现这个 `--path` 会被忽略。
  - Source
excluded: # 执行 linting 时忽略的路径。 优先级比 `included` 更高。
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift

disabled_rules

disabled_rules: # 禁用指定的规则
  - file_length
  - ...

opt_in_rules

opt_in_rules: # 启用指定的规则
  - file_length
  - ...

whitelist_rules

whitelist_rules: # 规则的白名单,但是不能和上面两个规则一起使用
  - file_length 
  - ...

included

included: # 你所希望Lint检索的路径,SwiftLint会扫描该路径下的所有.swift后缀的文件
  - ../

excluded

excluded: # 你所希望不要检索的路径,SwiftLint会无视掉该路径下的文件
  - Pods

2. 規則

由於規則太多,這裡不一一列舉,但是用法都是大致相同的:

force_cast: [warning | error] 当出现强制类型转换的时候是提示 Error 还是 Warning
type_body_length:
  - 300 # 当超过300行的时候飚黄
  - 400 # 当超过400行的时候飚红

3. 註解控制

** 該部分直接引用外文 **

還有一些場景有的時候,我們並不想大範圍的禁用掉一個規則,但是在某個文件中,我們必須要無視這條規則,那麼我們應該怎麼告訴 SwiftLint 來無視呢?

比如以下這種情況:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

哇,這個是系統給的方法啊,竟然還警告我方法名稱過長!難道無視他?不行,強迫症患者忍受不了啊!

於是我們可以這樣:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

OK,這樣在 // swiftlint:disable line_length 註釋之後的所有行長的警告都會被忽略掉。如果你想要在忽略掉這一行之後再次啟用,那麼只要再增加一行 // swiftlint:enable line_length 就可以了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

五、最後貼上官方的範例

disabled_rules: # 执行时排除掉的规则
  - colon
  - comma
  - control_statement
opt_in_rules: # 一些规则仅仅是可选的
  - empty_count
  - missing_docs
  # 可以通过执行如下指令来查找所有可用的规则:
  # swiftlint rules
included: # 执行 linting 时包含的路径。如果出现这个 `--path` 会被忽略。
  - Source
excluded: # 执行 linting 时忽略的路径。 优先级比 `included` 更高。
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift

# 可配置的规则可以通过这个配置文件来自定义
# 二进制规则可以设置他们的严格程度
force_cast: warning # 隐式
force_try:
  severity: warning # 显式
# 同时有警告和错误等级的规则,可以只设置它的警告等级
# 隐式
line_length: 110
# 可以通过一个数组同时进行隐式设置
type_body_length:
  - 300 # warning
  - 400 # error
# 或者也可以同时进行显式设置
file_length:
  warning: 500
  error: 1200
# 命名规则可以设置最小长度和最大程度的警告/错误
# 此外它们也可以设置排除在外的名字
type_name:
  min_length: 4 # 只是警告
  max_length: # 警告和错误
    warning: 40
    error: 50
  excluded: iPhone # 排除某个名字
variable_name:
  min_length: # 只有最小长度
    error: 4 # 只有错误
  excluded: # 排除某些名字
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # 报告类型 (xcode, json, csv, checkstyle)