SwiftLint
SwiftLint 介绍
- SwiftLint 是
Realm
推出的一款Swift
代码规范检查工具,SwiftLint
基于 Github 公布的 Swift 代码规范 进行代码检查,并且能够很好的和 Xcode 整合 - 配置好所有的设置之后,在
Xcode
中执行编译时,SwiftLint
会自动运行检查,不符合规范的代码会通过警告
或者红色错误
的形式指示出来 - 支持自定义规则,可禁用或者开启某一些规则
SwiftLint 安装
安装
- 使用Homebrew
brew install swiftlint
- CocoaPods 安装
pod 'SwiftLint'
- 注意在
.gitignore
添加对应目录
避免不必要的麻烦
- 使用Homebrew
配置
Build Phases
->New Run Script Phase
(-> 重命名为 SwiftLint)添加脚本
Homebrew 安装
1
2
3
4
5if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/ SwiftLint"
fiCocoaPods安装
"${PODS_ROOT}/SwiftLint/swiftlint"
Command+B
编译项目,可以查看你不规范的地方了
SwiftLint 规则
- Rules.md
- 在代码中关闭某个规则
1
2
3
4可以通过在一个源文件中定义一个如下格式的注释来关闭某个规则:
// swiftlint:disable <rule>
在该文件结束之前或者在定义如下格式的匹配注释之前,这条规则都会被禁用:
// swiftlint:enable <rule>
1 | 也可以通过添加 :previous, :this 或者 :next 来使关闭或者打开某条规则的命令分别应用于前一行,当前或者后一行代码。 |
自定义配置
当编译后发现 999+警告 999+ 错误,是不是很崩溃,如果觉得规范太严格,我们可以自定义一些配置来符合自己的风格,再做一些配置忽略 CocoaPods
、Carthage
、自己导入的包
等包管理器引入的第三方库
- 创建配置文件
- 根目录下
touch .swiftlint.yml
新建一个.swiftlint.yml
的配置文件
- 根目录下
几个配置选项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49disabled_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 # 排除某个名字
identifier_name:
min_length: # 只有最小长度
error: 4 # 只有错误
excluded: # 排除某些名字
- id
- URL
- GlobalAPIKey
reporter: "xcode" # 报告类型 (xcode, json, csv, checkstyle, junit, html, emoji)我的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77disabled_rules: # rule identifiers to exclude from running
- missing_docs
- unused_closure_parameter
- cyclomatic_complexity
opt_in_rules: # some rules are only opt-in
- empty_count
# Find all the available rules by running:
# swiftlint rules
included: # paths to include during linting. `--path` is ignored if present.
- xxx
- xxx
- xxx
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Pods
- xxx
- xxx
# 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: 300
function_body_length:
- 300 # warning
- 400 # error
function_parameter_count:
- 10 # warning
- 15 # error
large_tuple:
- 6 # warning
- 12 # error
# they can set both implicitly with an array
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 1000
error: 2000
cyclomatic_complexity:
- 15 # warning
- 30 # error
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
min_length: 3 # only warning
max_length: # warning and error
warning: 40
error: 50
excluded: # excluded via string
- T
- E
identifier_name:
min_length: # only min_length
error: 3 # only error
excluded: # excluded via string array
- vc
- id
- in
- kf
- x
- y