SwiftLint Rule 速覽

SwiftLint 是一個很好的 lint 工具, 我簡單查看記錄了 lint 的規(guī)則, 方便記憶和查看
更具體的規(guī)則使用 swiftlint rules _rulename_ 查看

| identifier                              | opt-in | correctable | enabled in your config
----------------------+

| 以下是可選規(guī)則, 需要額外添加
| attributes                              | yes    | no          | no    Attributes 針對類和func重啟一行, 針對變量在同一行
| closure_end_indentation                 | yes    | no          | no    閉包的 } 與 { 所在行的縮進要相同
| closure_spacing                         | yes    | no          | no    閉包應(yīng)該與括號有間隔                
| conditional_returns_on_newline          | yes    | no          | no    條件判斷語句需要重啟一行 return                
| empty_count                             | yes    | no          | no    容器 使用 isEmpty 代替 count 判斷是否為空               
| explicit_type_interface                 | yes    | no          | no    需要跑明確參數(shù)的類型定義                
| 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    避免隱式解包(定義 ! 類型)                 
| valid_docs                              | yes    | no          | no    文檔聲明應(yīng)該是正確有效的
| 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 使用字面量初始化, 需要把相關(guān)圖片名,顏色RGB 等參數(shù)定義為 enum struct 或者常量                          
| overridden_super_call                   | yes    | no          | no    override 方法需要調(diào)用 super method                 
| private_outlet                          | yes    | no          | no    IBOutlets 應(yīng)該設(shè)置為 private, 來避免泄露
| prohibited_super_call                   | yes    | no          | no    某些特殊的 override 方法, 禁止調(diào)用 super method                 
| redundant_nil_coalescing                | yes    | yes         | no    避免使用 `object ?? nil` 
| explicit_init                           | yes    | yes         | no    避免直接調(diào)用 init 方法
| operator_usage_whitespace               | yes    | yes         | no    操作符需要使用一個空格間隔
| nimble_operator                         | yes    | yes         | no    避免 expect 一個確定的判斷                 
| number_separator                        | yes    | yes         | no    使用 _ 分割大數(shù), 讓數(shù)字更清晰    

| 以下是默認規(guī)則             
| empty_parameters                        | no     | yes         | yes   使用 `() -> ` 代替 `Void ->                 
| empty_parentheses_with_trailing_closure | no     | yes         | yes   尾閉包避免空參數(shù)括號             
| colon                                   | no     | yes         | yes   冒號左邊沒有空格, 右邊有空格                 
| comma                                   | no     | yes         | yes   逗號左邊沒有空格, 右邊有空格   
| leading_whitespace                      | no     | yes         | yes   文件末尾不應(yīng)該存在空格符                 
| legacy_cggeometry_functions             | no     | yes         | yes   避免使用 C 風格 的 CG 遺留函數(shù), 使用 struct extension                
| legacy_constant                         | no     | yes         | yes   避免使用 遺留的全局常量, 使用 struct 內(nèi)定義的 常量                 
| legacy_constructor                      | no     | yes         | yes   使用 swift 提供的 struct 構(gòu)造函數(shù), 避免使用 遺留的構(gòu)造函數(shù) 比如 CGPointMake(10, 10)                
| legacy_nsgeometry_functions             | no     | yes         | yes   避免使用 C 風格 的 NS 遺留函數(shù), 使用 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   小括號內(nèi)包含函數(shù)(大括號)的時候,之間沒有空格                 
| redundant_void_return                   | no     | yes         | yes   在不必要的時候, 不需要寫 ->() and -> Void                 
| return_arrow_whitespace                 | no     | yes         | yes   函數(shù)定義返回的 ->  前后有空格, 不換行 
| statement_position                      | no     | yes         | yes   else and catch 應(yīng)該與 } 在同一行, 以空格間隔
| trailing_newline                        | no     | yes         | yes   文件末尾應(yīng)該有一個空行
| trailing_semicolon                      | no     | yes         | yes   行末尾不加分號
| trailing_whitespace                     | no     | yes         | yes   行末尾不加空格
| unused_closure_parameter                | no     | yes         | yes   函數(shù)的參數(shù)必須被使用
| vertical_whitespace                     | no     | yes         | yes   不能有連續(xù)多個空行
| void_return                             | no     | yes         | yes   使用 `-> Void` 代替 `-> ()

| class_delegate_protocol                 | no     | no          | yes   delegate protocol 應(yīng)該被設(shè)定為 class-only                 
| closure_parameter_position              | no     | no          | yes   閉包的參數(shù)應(yīng)該和 { 在同一行                           
| compiler_protocol_init                  | no     | no          | yes   不應(yīng)該直接調(diào)用字面量轉(zhuǎn)換的初始化方法                  
| control_statement                       | no     | no          | yes   if while 等判斷條件不要用括號 括起來                 
| custom_rules                            | no     | no          | yes   一些自定義規(guī)則                 
| cyclomatic_complexity                   | no     | no          | yes   不應(yīng)該存在太復(fù)雜的函數(shù)(判斷語句過多)                 
| discarded_notification_center_observer  | no     | no          | yes   當使用 block 注冊通知中心 observer 的時候, 應(yīng)該存儲函數(shù)返回的 observer, 以便之后的刪除
| file_length                             | no     | no          | yes   文件長度限制                 
| for_where                               | no     | no          | yes   使用 `for where` 代替 簡單的 `for { if }`                 
| force_cast                              | no     | no          | yes   避免強制的類型轉(zhuǎn)化                 
| force_try                               | no     | no          | yes   避免 `try!`                 
| function_body_length                    | no     | no          | yes   body 長度限制                 
| function_parameter_count                | no     | no          | yes   函數(shù)參數(shù)數(shù)目限制                 
| generic_type_name                       | no     | no          | yes   類型命名規(guī)則限制                
| identifier_name                         | no     | no          | yes   參數(shù)變量命名規(guī)則                 
| implicit_getter                         | no     | no          | yes   read-only 參數(shù)不應(yīng)該有 getter                  
| large_tuple                             | no     | no          | yes   tuple 不應(yīng)該包括太多參數(shù)                 
| line_length                             | no     | no          | yes   行長度限制                 
| nesting                                 | no     | no          | yes   類型定義嵌套不要超過1層 , 聲明嵌套不要超過5層                
| notification_center_detachment          | no     | no          | yes   `NotificationCenter.default.removeObserver` 只在 `deinit` 中被調(diào)用                         
| operator_whitespace                     | no     | no          | yes   定義操作符的時候 操作符左右應(yīng)該各有一個空格                                 
| private_unit_test                       | no     | no          | yes   單元測試方法 不能設(shè)置為 private                             
| redundant_string_enum_value             | no     | no          | yes   字符串類型枚舉, 會有默認 string 值,與名字相同, 不要再次設(shè)置                               
| shorthand_operator                      | no     | no          | yes   使用 +=, -=, *=, /=                 
| syntactic_sugar                         | no     | no          | yes   要使用 [] ? 等數(shù)組字典可選項的語法糖
| todo                                    | no     | no          | yes   避免 TODOs and FIXMEs 標識
| trailing_comma                          | no     | no          | yes   數(shù)組末尾不要加括號
| type_body_length                        | no     | no          | yes   類型體行數(shù)限制
| type_name                               | no     | no          | yes   類型名字限制規(guī)則
| unused_enumerated                       | no     | no          | yes   當參數(shù)沒有被全部使用的時候, 不要使用容器的 enumerated 方法
| unused_optional_binding                 | no     | no          | yes   必須使用定義的 optional binding
| valid_ibinspectable                     | no     | no          | yes   IBInspectable 必須是可變參數(shù)
| vertical_parameter_alignment            | no     | no          | yes   函數(shù)參數(shù)分為多行書寫的時候, 頭部(小括號后面一位)必須對其
| weak_delegate                           | no     | no          | yes   delegate 應(yīng)該被設(shè)置為 weak
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,288評論 6 342
  • 兩個月之前在項目中就開始使用 Eslint ,當時直接 copy 別人的 .eslintrc.js 文件,感覺好復(fù)...
    dkvirus閱讀 112,583評論 33 190
  • 前幾日,看著本月的寫作主題,思考了幾日,竟無處著筆。本欲草草胡寫幾篇了事。想起樓主教導(dǎo)不可無病呻吟,敷衍...
    袁一笑閱讀 405評論 0 2
  • 風吹蟲鳴醉秋美, 酒盡醋酸...
    紅城客閱讀 537評論 15 36

友情鏈接更多精彩內(nèi)容