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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章