Swift之swiftLint

一、以下是可選規則, 需要額外添加

identifier opt-in correctable enabled in your config 說明
attributes yes no no Attributes 針對類和func重啓一行, 針對變量在同一行
closure_end_indentation yes no no 閉包的 } 與 { 所在行的縮進要相同
closure_spacing yes no no 閉包應該與括號有間隔
conditional_returns_on_newline yes no no 條件判斷語句需要重啓一行 return
empty_count yes no no 容器 使用 isEmpty 代替 count 判斷是否爲空
explicit_type_interface yes no no 需要跑明確參數的類型定義
fatal_error_message yes no no fatalError 必須擁有一個 message
file_header yes no no file header 的限制
first_where yes no no 使用 .first(where:) 代替 .filter { }.first
force_unwrapping yes no no 避免強制解包
implicitly_unwrapped_optional yes no no 避免隱式解包(定義 ! 類型)
missing_docs yes no no 避免遺失文檔
sorted_imports yes no no 引用需要按照字母排序
switch_case_on_newline yes no no switch 的 case 需要新啓一行
object_literal yes no no 避免 image and color 使用字面量初始化, 需要把相關圖片名,顏色RGB 等參數定義爲 enum struct 或者常量
overridden_super_call yes no no override 方法需要調用 super method
private_outlet yes no no IBOutlets 應該設置爲 private, 來避免泄露
prohibited_super_call yes no no 某些特殊的 override 方法, 禁止調用 super method
redundant_nil_coalescing yes yes no 避免使用 object ?? nil
explicit_init yes yes no 避免直接調用 init 方法
operator_usage_whitespace yes yes no 操作符需要使用一個空格間隔
nimble_operator yes yes no 避免 expect 一個確定的判斷
number_separator yes yes no 使用 _ 分割大數, 讓數字更清晰
prefixed_toplevel_constant yes no no 類似全局常量,建議前綴以k開頭

二、以下是默認規則

identifier opt-in correctable enabled in your config 說明
empty_parameters no yes yes 使用 () -> 代替 `Void ->
empty_parentheses_with_trailing_closure no yes yes 尾閉包避免空參數括號
colon no yes yes 冒號左邊沒有空格, 右邊有空格
comma no yes yes 逗號左邊沒有空格, 右邊有空格
leading_whitespace no yes yes 文件末尾不應該存在空格符
legacy_cggeometry_functions no yes yes 避免使用 C 風格 的 CG 遺留函數, 使用 struct extension
legacy_constant no yes yes 避免使用 遺留的全局常量, 使用 struct 內定義的 常量
legacy_constructor no yes yes 使用 swift 提供的 struct 構造函數, 避免使用 遺留的構造函數 比如 CGPointMake(10, 10)
legacy_nsgeometry_functions no yes yes 避免使用 C 風格 的 NS 遺留函數, 使用 struct extension
mark no yes yes 正確使用 mark 的格式 // MARK: - message
opening_brace no yes yes 需要正確書寫大括號格式
redundant_discardable_let no yes yes 使用 _ = foo() 代替 let _ = foo()
redundant_optional_initialization no yes yes 不需要寫默認值爲 nil
closing_brace no yes yes 小括號內包含函數(大括號)的時候,之間沒有空格
redundant_void_return no yes yes 在不必要的時候, 不需要寫 ->() and -> Void
return_arrow_whitespace no yes yes 函數定義返回的 -> 前後有空格, 不換行
statement_position no yes yes else and catch 應該與 } 在同一行, 以空格間隔
trailing_newline no yes yes 文件末尾應該有一個空行
trailing_semicolon no yes yes 行末尾不加分號
trailing_whitespace no yes yes 行末尾不加空格
unused_closure_parameter no yes yes 函數的參數必須被使用
vertical_whitespace no yes yes 不能有連續多個空行
void_return no yes yes 使用 -> Void 代替 `-> ()
class_delegate_protocol no no yes delegate protocol 應該被設定爲 class-only
closure_parameter_position no no yes 閉包的參數應該和 { 在同一行
compiler_protocol_init no no yes 不應該直接調用字面量轉換的初始化方法
control_statement no no yes if while 等判斷條件不要用括號 括起來
custom_rules no no yes 一些自定義規則
cyclomatic_complexity no no yes 不應該存在太複雜的函數(判斷語句過多)
discarded_notification_center_observer no no yes 當使用 block 註冊通知中心 observer 的時候, 應該存儲函數返回的 observer, 以便之後的刪除
file_length no no yes 文件長度限制
for_where no no yes 使用 for where 代替 簡單的 for { if }
force_cast no no yes 避免強制的類型轉化
force_try no no yes 避免 try!
function_body_length no no yes body 長度限制
function_parameter_count no no yes 函數參數數目限制
generic_type_name no no yes 類型命名規則限制
identifier_name no no yes 參數變量命名規則
implicit_getter no no yes read-only 參數不應該有 getter
large_tuple no no yes tuple 不應該包括太多參數
line_length no no yes 行長度限制
nesting no no yes 類型定義嵌套不要超過1層 , 聲明嵌套不要超過5層
notification_center_detachment no no yes NotificationCenter.default.removeObserver 只在 deinit 中被調用
operator_whitespace no no yes 定義操作符的時候 操作符左右應該各有一個空格
private_unit_test no no yes 單元測試方法 不能設置爲 private
redundant_string_enum_value no no yes 字符串類型枚舉, 會有默認 string 值,與名字相同, 不要再次設置
shorthand_operator no no yes 使用 +=, -=, *=, /=
syntactic_sugar no no yes 要使用 [] ? 等數組字典可選項的語法糖
todo no no yes 避免 TODOs and FIXMEs 標識
trailing_comma no no yes 數組末尾不要加括號
type_body_length no no yes 類型體行數限制
type_name no no yes 類型名字限制規則
unused_enumerated no no yes 當參數沒有被全部使用的時候, 不要使用容器的 enumerated 方法
unused_optional_binding no no yes 必須使用定義的 optional binding
valid_ibinspectable no no yes IBInspectable 必須是可變參數
vertical_parameter_alignment no no yes 函數參數分爲多行書寫的時候, 頭部(小括號後面一位)必須對其
weak_delegate no no yes delegate 應該被設置爲 weak

三、配置

homebrew全局安裝swiftlintxcode Build PhasesRun Script 配置

if test -d "/opt/homebrew/bin/"; then
  PATH="/opt/homebrew/bin/:${PATH}"
fi

export PATH

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章