一种实施快速风格和约定的工具,它基于现在存档的GitHub Swift样式指南。 Swiftlint强制执行Swift社区通常接受的样式指南规则。这些规则在流行风格指南中得到很好的描述,例如Kodeco的Swift Style Guide。
Swiftlint挂钩进入Clang和SourceKit,以使用源文件的AST表示,以获得更准确的结果。
Swiftlint可以用作命令插件或构建工具插件。
添加
. package ( url : " https://github.com/SimplyDanny/SwiftLintPlugins " , from : " <version> " )
到您的Package.swift
文件以自动消耗Swiftlint的最新版本,或将依赖项固定到特定版本:
. package ( url : " https://github.com/SimplyDanny/SwiftLintPlugins " , exact : " <version> " )
在其中,将<version>
替换为所需的最小值或精确版本。
笔记
直接从Swiftlint存储库中食用插件带有几个缺点。为了避免它们并减少施加的开销,强烈建议使用专用SwiftLintplugins存储库中的插件,即使Swiftlint存储库中的插件也绝对功能正常。如果首选来自Swiftlint的插件,只需在上面的软件包声明中使用url https://github.com/realm/SwiftLint
。
但是,SwiftLintPlugins促进了插件的采用。它列出了驱动插件的一些原因,而Swiftlint本身提供的插件非常麻烦。由于插件代码和发行版保持同步,因此两者之间的功能没有差异,但是使用专用插件存储库,您可以花费大量时间和麻烦。
本文档假设您依靠SwiftlintPlugins。
使用以下链接将Swiftlint作为软件包依赖关系添加到Xcode项目:
https://github.com/SimplyDanny/SwiftLintPlugins
brew install swiftlint
将以下内容添加到您的Podfile
:
pod 'SwiftLint'
这将在Pods/
pod install
执行过程中下载Swiftlint二进制文件和依赖项,并允许您通过脚本构建阶段中的${PODS_ROOT}/SwiftLint/swiftlint
调用它。
通过Cocoapods安装还可以固定到特定版本的Swiftlint,而不仅仅是最新版本(Homebrew是这种情况)。
请注意,这将添加Swiftlint二进制文件,其依赖关系的二进制文件以及Swift二进制库的分布到Pods/
Directory,因此请在此目录中检查到SCM,例如GIT等SCM。
mint install realm/SwiftLint
将其放在您的MODULE.bazel
中。Bazel:
bazel_dep ( name = "swiftlint" , version = "0.52.4" , repo_name = "SwiftLint" )
或将其放在您的WORKSPACE
中:
load ( "@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" )
http_archive (
name = "build_bazel_rules_apple" ,
sha256 = "390841dd5f8a85fc25776684f4793d56e21b098dfd7243cd145b9831e6ef8be6" ,
url = "https://github.com/bazelbuild/rules_apple/releases/download/2.4.1/rules_apple.2.4.1.tar.gz" ,
)
load (
"@build_bazel_rules_apple//apple:repositories.bzl" ,
"apple_rules_dependencies" ,
)
apple_rules_dependencies ()
load (
"@build_bazel_rules_swift//swift:repositories.bzl" ,
"swift_rules_dependencies" ,
)
swift_rules_dependencies ()
load (
"@build_bazel_rules_swift//swift:extras.bzl" ,
"swift_rules_extra_dependencies" ,
)
swift_rules_extra_dependencies ()
http_archive (
name = "SwiftLint" ,
sha256 = "c6ea58b9c72082cdc1ada4a2d48273ecc355896ed72204cedcc586b6ccb8aca6" ,
url = "https://github.com/realm/SwiftLint/releases/download/0.52.4/bazel.tar.gz" ,
)
load ( "@SwiftLint//bazel:repos.bzl" , "swiftlint_repos" )
swiftlint_repos ()
load ( "@SwiftLint//bazel:deps.bzl" , "swiftlint_deps" )
swiftlint_deps ()
然后,您可以使用此命令在当前目录中运行Swiftlint:
bazel run -c opt @SwiftLint//:swiftlint
从最新的GitHub版本下载SwiftLint.pkg
并运行它。
确保安装了构建工具Bazel和最近的Swift工具链,并且所有工具都可以在您的PATH
中发现。
要构建Swiftlint,请克隆此存储库并运行make install
。
重要的
虽然在违反皮棉时,在编译Swift源文件之前运行Swiftlint似乎很直观,但重要的是要了解Swiftlint旨在分析可编译的有效源代码。非编译代码很容易导致意外且令人困惑的结果,尤其是在使用--fix
/ --autocorrect
命令行参数时。
Swiftlint可以用作Swift软件包项目和Xcode项目的构建工具插件。
构建工具插件通过在软件包/项目目录中找到最上方的配置文件来确定Swiftlint工作目录。如果在其中找不到配置文件,则包装/项目目录用作工作目录。
当插件无法解析SwiftLint工作目录时,该插件会引发错误。例如,这将发生在XCode Projects中,该项目的SWIFT文件不在项目目录中。
为了最大程度地提高与插件的兼容性,请避免使用--config
选项的项目结构。
笔记
需要通过Swift软件包管理器安装。
构建工具插件在构建每个目标时运行。当项目具有多个目标时,必须单独将插件添加到所需的目标中。
为此,请将插件添加到目标,以伸有如下:
. target (
...
plugins : [ . plugin ( name : " SwiftLintBuildToolPlugin " , package : " SwiftLintPlugins " ) ]
) ,
笔记
需要通过Swift软件包管理器安装。
命令插件可以从命令行中运行Swiftlint,如下所示:
swift package plugin swiftlint
笔记
需要通过Xcode软件包依赖项安装。
构建工具插件作为每个目标的构建阶段运行。当项目具有多个目标时,必须单独将插件添加到所需的目标中。
为此,请将SwiftLintBuildToolPlugin
添加到Build Phases
的Run Build Tool Plug-ins
阶段,以覆盖目标。
提示
首次使用插件时,请确保在提示时信任和启用它。如果宏存在警告,请选择它以信任并启用宏。
对于无人看管的使用(例如,在CI上),可以使用以下任何一个禁用软件包插件和宏验证:
使用xcodebuild
选项:
-skipPackagePluginValidation
-skipMacroValidation
设置XCode默认值:
defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES
defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES
重要的
无人看管的使用选项绕过Xcode的验证对话框,并隐式信任所有插件和宏,这具有安全性。
Swiftlint的配置文件位于软件包/项目目录之外的项目结构未直接由构建工具插件支持。这是因为不可能通过参数来构建工具插件(例如,传递配置文件路径)。
如果您的项目结构与构建工具插件不直接使用,请考虑以下选项之一:
parent_config: path/to/.swiftlint.yml
。笔记
基于所使用的安装方法,运行脚本构建阶段中的shell命令语法可能不同,也可能需要其他配置。有关更多信息,请参阅安装说明。
如果构建工具插件不适用于您的项目设置,或者需要其他自定义设置时,可以将SwiftLint作为运行脚本构建阶段添加。当项目设置依赖--config
swiftlint选项时,这很有用;或以单个swiftlint
调用在一起将所有目标贴在一起。可以在.swiftlint.yml
配置中配置文件包含和排除。
为此,在Compile Sources
阶段之后,在主要应用程序目标的Build Phases
的Run Script
阶段中添加自定义脚本阶段。使用以下脚本实现:
if command -v swiftlint > /dev/null 2>&1
then
swiftlint
else
echo " warning: ` swiftlint ` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions. "
fi
如果您在Swift软件包中使用SwiftLintplugin,则可以通过以下方式参考swiftlint
可执行文件:
SWIFT_PACKAGE_DIR= " ${BUILD_DIR % Build /* } SourcePackages/artifacts "
SWIFTLINT_CMD= $( ls " $SWIFT_PACKAGE_DIR " /swiftlintplugins/SwiftLintBinary/SwiftLintBinary.artifactbundle/swiftlint- * /bin/swiftlint | head -n 1 )
if test -f " $SWIFTLINT_CMD " 2>&1
then
" $SWIFTLINT_CMD "
else
echo " warning: ` swiftlint ` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions. "
fi
笔记
SWIFTLINT_CMD
路径使用默认的Xcode配置,并已在Xcode 15/16上进行了测试。如果另一种配置(例如自定义Swift软件包路径),请相应地调整值。
提示
Based on dependency analysis
取消选中,以在所有增量构建上运行swiftlint
,从而抑制未指定的输出警告。
Xcode 15通过设置ENABLE_USER_SCRIPT_SANDBOXING
build设置的默认值从NO
到YES
进行了重大更改。结果,Swiftlint遇到了与缺少文件权限有关的错误,该错误通常表现为error: Sandbox: swiftlint(19427) deny(1) file-read-data.
为了解决此问题,有必要手动将ENABLE_USER_SCRIPT_SANDBOXING
设置设置为Swiftlint正在配置的特定目标的NO
。
如果您在Apple Silicon上通过Homebrew安装Swiftlint,则可能会遇到此警告:
warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint
那是因为默认情况下,苹果硅上的自制二进制文件将二进制文件安装到/opt/homebrew/bin
文件夹中。要指示Xcode在哪里找到Swiftlint,您可以在构建阶段中添加/opt/homebrew/bin
到PATH
环境变量:
if [[ " $( uname -m ) " == arm64 ]]
then
export PATH= " /opt/homebrew/bin: $PATH "
fi
if command -v swiftlint > /dev/null 2>&1
then
swiftlint
else
echo " warning: ` swiftlint ` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions. "
fi
或者,您可以在/usr/local/bin
中创建一个指向实际二进制文件的符号链接:
ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint
如果您也想修复违规行为,您的脚本可以运行swiftlint --fix && swiftlint
而不仅仅是swiftlint
。这将意味着所有可更正的违规行为都是固定的,同时确保项目中出现警告,以免违规。
如果您已经通过可可录安装了Swiftlint,则脚本应该看起来像这样:
" ${PODS_ROOT} /SwiftLint/swiftlint "
要将Swiftlint与Visual Studio Code集成,请从市场上安装vscode-swiftlint
扩展。
您可以使用官方的swiftlint
Fastlane动作来运行Swiftlint,作为您的快速手术过程的一部分。
swiftlint (
mode : :lint , # SwiftLint mode: :lint (default) or :autocorrect
executable : "Pods/SwiftLint/swiftlint" , # The SwiftLint binary path (optional). Important if you've installed it via CocoaPods
path : "/path/to/lint" , # Specify path to lint (optional)
output_file : "swiftlint.result.json" , # The path of the output file (optional)
reporter : "json" , # The custom reporter to use (optional)
config_file : ".swiftlint-ci.yml" , # The path of the configuration file (optional)
files : [ # List of files to process (optional)
"AppDelegate.swift" ,
"path/to/project/Model.swift"
] ,
ignore_exit_status : true , # Allow fastlane to continue even if SwiftLint returns a non-zero exit status (Default: false)
quiet : true , # Don't print status logs like 'Linting ' & 'Done linting' (Default: false)
strict : true # Fail on warnings? (Default: false)
)
Swiftlint也可以使用Ubuntu
作为Docker映像。因此,仅首次需要使用下一个命令来拉码头映像:
docker pull ghcr.io/realm/swiftlint:latest
然后,随后,您只需在Docker内部运行swiftlint
,就可以了:
docker run -it -v ` pwd ` : ` pwd ` -w ` pwd ` ghcr.io/realm/swiftlint:latest
这将在您现在所在的文件夹中执行swiftlint
( pwd
),显示一个类似的输出:
$ docker run -it -v ` pwd ` : ` pwd ` -w ` pwd ` ghcr.io/realm/swiftlint:latest
Linting Swift files in current working directory
Linting ' RuleDocumentation.swift ' (1/490)
...
Linting ' YamlSwiftLintTests.swift ' (490/490)
Done linting ! Found 0 violations, 0 serious in 490 files.
在这里,您有更多有关Docker图像使用的文档。
$ swiftlint help
OVERVIEW: A tool to enforce Swift style and conventions.
USAGE: swiftlint <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
analyze Run analysis rules
docs Open SwiftLint documentation website in the default web browser
generate-docs Generates markdown documentation for selected group of rules
lint (default) Print lint warnings and errors
baseline Operations on existing baselines
reporters Display the list of reporters and their identifiers
rules Display the list of rules and their identifiers
version Display the current version of SwiftLint
See 'swiftlint help <subcommand>' for detailed help.
在包含Swift文件的目录中运行swiftlint
。目录将被递归搜索。
在使用lint
或analyze
时指定文件列表(例如由XCode修改的文件列表,由ExtraBuildPhase
Xcode插件指定,或基于git ls-files -m
中的工作树中修改的文件),您可以通过传递选项--use-script-input-files
和设置以下实例变量: SCRIPT_INPUT_FILE_COUNT
和SCRIPT_INPUT_FILE_0
, SCRIPT_INPUT_FILE_1
,..., SCRIPT_INPUT_FILE_{SCRIPT_INPUT_FILE_COUNT - 1}
。同样,可以通过传递选项--use-script-input-file-lists
SCRIPT_INPUT_FILE_LIST_{SCRIPT_INPUT_FILE_LIST_COUNT - 1}
设置以下实例变量: SCRIPT_INPUT_FILE_LIST_COUNT
和SCRIPT_INPUT_FILE_LIST_0
, SCRIPT_INPUT_FILE_LIST_1
,...
这些是为输入文件设置为自定义Xcode脚本阶段的相同环境变量。
Swiftlint将钩子挂入SourceKit,因此即使Swift进化,它仍在继续工作!
这也可以使Swiftlint精益保持,因为它不需要与完整的Swift编译器一起运送,它只是与您已经安装在机器上的官方官方的官方交流。
您应该始终使用与编译代码相同的工具链运行Swiftlint。
如果您安装了多个工具链或Xcodes,则可能需要覆盖Swiftlint的默认Swift工具链。
这是Swiftlint确定要使用哪种Swift工具链的顺序:
$XCODE_DEFAULT_TOOLCHAIN_OVERRIDE
$TOOLCHAIN_DIR
或$TOOLCHAINS
xcrun -find swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
~/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
~/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
sourcekitd.framework
有望在上述路径中传递的值的usr/lib/
subdirectory中找到。
您还可以将TOOLCHAINS
环境变量设置为标识Swift Toolchain版本的反向DNS符号:
TOOLCHAINS=com.apple.dt.toolchain.Swift_2_3 swiftlint --fix
在Linux上,预计SourceKit将位于/usr/lib/libsourcekitdInProc.so
中或由LINUX_SOURCEKIT_LIB_PATH
环境变量指定。
pre-commit
钩子Swiftlint可以作为预先承诺的挂钩运行。安装后,将其添加到存储库的根部中的.pre-commit-config.yaml
:
repos :
- repo : https://github.com/realm/SwiftLint
rev : 0.50.3
hooks :
- id : swiftlint
将rev
调整为您选择的Swiftlint版本。 pre-commit autoupdate
可用于更新当前版本。
可以使用entry
配置Swiftlint来应用修复程序并在错误上失败:
- repo : https://github.com/realm/SwiftLint
rev : 0.50.3
hooks :
- id : swiftlint
entry : swiftlint --fix --strict
随着时间的流逝,Swiftlint和Swift社区(就是您!)中包含200多个规则。鼓励拉动请求。
您可以在此处找到有关规则的更新列表和更多信息。
您还可以检查源/swiftlintbuiltinrules/规则目录以查看其实现。
默认情况下, opt_in_rules
将被禁用(即,您必须在配置文件中明确启用它们)。
关于何时将规则标记为选择加入的准则:
empty_count
)force_unwrapping
)可以通过以下格式在源文件中使用注释来禁用规则:
// swiftlint:disable <rule1> [<rule2> <rule3>...]
规则将被禁用到文件末尾或直到Linter看到匹配启用评论之前:
// swiftlint:enable <rule1> [<rule2> <rule3>...]
例如:
// swiftlint:disable colon
let noWarning : String = " " // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning : String = " " // Warning generated about colons immediately after variable names
包括all
关键字将禁用所有规则,直到Linter看到匹配的启用评论:
// swiftlint:disable all
// swiftlint:enable all
例如:
// swiftlint:disable all
let noWarning : String = " " // No warning about colons immediately after variable names!
let i = " " // Also no warning about short identifier names
// swiftlint:enable all
let hasWarning : String = " " // Warning generated about colons immediately after variable names
let y = " " // Warning generated about short identifier names
也可以通过附加:previous
:this
或:next
行,也可以通过附加:上一个或:下一行修改disable
或enable
命令。
例如:
// swiftlint:disable:next force_cast
let noWarning = NSNumber ( ) as! Int
let hasWarning = NSNumber ( ) as! Int
let noWarning2 = NSNumber ( ) as! Int // swiftlint:disable:this force_cast
let noWarning3 = NSNumber ( ) as! Int
// swiftlint:disable:previous force_cast
运行swiftlint rules
以打印所有可用规则及其标识符的列表。
通过从您将从SwiftLint运行的目录中添加.swiftlint.yml
文件来配置Swiftlint。可以配置以下参数:
规则包容:
disabled_rules
:从默认启用设置中禁用规则。opt_in_rules
:启用不属于默认设置的规则。特殊的all
标识符将启用所有OPT在Linter规则中,除了disabled_rules
中列出的规则。only_rules
:仅启用此列表中指定的规则。不能与disabled_rules
或opt_in_rules
一起指定。analyzer_rules
:这是仅由analyze
命令运行的规则的完全独立的列表。所有分析仪规则均可选择加入,因此这是唯一可配置的规则列表, disabled_rules
和only_rules
没有等效物。特殊的all
标识符也可以在此处用于启用所有分析仪规则,除了在disabled_rules
中列出的规则。 # By default, SwiftLint uses a set of sensible default rules you can adjust:
disabled_rules : # rule identifiers turned on by default to exclude from running
- colon
- comma
- control_statement
opt_in_rules : # some rules are turned off by default, so you need to opt-in
- empty_count # find all the available rules by running: `swiftlint rules`
# Alternatively, specify all rules explicitly by uncommenting this option:
# only_rules: # delete `disabled_rules` & `opt_in_rules` if using this
# - empty_parameters
# - vertical_whitespace
analyzer_rules : # rules run by `swiftlint analyze`
- explicit_self
# Case-sensitive paths to include during linting. Directory paths supplied on the
# command line will be ignored.
included :
- Sources
excluded : # case-sensitive paths to ignore during linting. Takes precedence over `included`
- Carthage
- Pods
- Sources/ExcludedFolder
- Sources/ExcludedFile.swift
- Sources/*/ExcludedFile.swift # exclude files with a wildcard
# If true, SwiftLint will not fail if no lintable files are found.
allow_zero_lintable_files : false
# If true, SwiftLint will treat all warnings as errors.
strict : false
# If true, SwiftLint will treat all errors as warnings.
lenient : false
# The path to a baseline file, which will be used to filter out detected violations.
baseline : Baseline.json
# The path to save detected violations to as a new baseline.
write_baseline : Baseline.json
# If true, SwiftLint will check for updates after linting or analyzing.
check_for_updates : true
# 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 : 110
# they can set both implicitly with an array
type_body_length :
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length :
warning : 500
error : 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name :
min_length : 4 # only warning
max_length : # warning and error
warning : 40
error : 50
excluded : iPhone # excluded via string
allowed_symbols : ["_"] # these are allowed in type names
identifier_name :
min_length : # only min_length
error : 4 # only error
excluded : # excluded via string array
- id
- URL
- GlobalAPIKey
reporter : " xcode " # reporter type (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging, summary)
您还可以通过在字符串中使用${SOME_VARIABLE}
中的配置文件中的环境变量。
除了主要Swiftlint项目与使用的规则外,Swiftlint还可以运行两种类型的自定义规则,您可以在自己的项目中定义自己:
这些规则的编写方式与Swiftlint发货的基于Swift的规则相同,因此它们快速,准确,可以利用SwiftSnntax,可以进行单位测试,等等。
使用这些需要如本视频中所述,在github.com/jpsim/swiftlint-bazel-example中使用Bazel构建Swiftlint。
您可以使用以下语法在您的配置文件中定义基于自定义的正则规则:
custom_rules :
pirates_beat_ninjas : # rule identifier
included :
- " .* \ .swift " # regex that defines paths to include during linting. optional.
excluded :
- " .*Test \ .swift " # regex that defines paths to exclude during linting. optional
name : " Pirates Beat Ninjas " # rule name. optional.
regex : " ([nN]inja) " # matching pattern
capture_group : 0 # number of regex capture group to highlight the rule violation at. optional.
match_kinds : # SyntaxKinds to match. optional.
- comment
- identifier
message : " Pirates are better than ninjas. " # violation message. optional.
severity : error # violation severity. optional.
no_hiding_in_strings :
regex : " ([nN]inja) "
match_kinds : string
这就是输出的样子:
重要的是要注意,正则表达模式与启用标志s
和m
一起使用.
匹配newlines, ^
/ $
分别匹配线路的开始和结尾。如果您不想拥有.
例如,匹配新线,例如(?-s)
可以预先启动正则。
您可以通过提供一个或多个match_kinds
来过滤匹配项,该匹配项将拒绝包括此列表中不存在的语法类型的匹配项。这是所有可能的语法类型:
argument
attribute.builtin
attribute.id
buildconfig.id
buildconfig.keyword
comment
comment.mark
comment.url
doccomment
doccomment.field
identifier
keyword
number
objectliteral
parameter
placeholder
string
string_interpolation_anchor
typeidentifier
可以提取询问sourcekitten中摘要中使用的所有语法类型。例如, sourcekitten syntax --text "struct S {}"
提供
source.lang.swift.syntaxtype.keyword
for struct
关键字和source.lang.swift.syntaxtype.identifier
的S
在上面列表中与keyword
和identifier
匹配。
如果将自定义规则与only_rules
结合使用,则必须在only_rules
列表中包含字符串custom_rules
:
only_rules :
- custom_rules
custom_rules :
no_hiding_in_strings :
regex : " ([nN]inja) "
match_kinds : string
与Swift自定义规则不同,您可以使用Swiftlint官方构建(例如,来自Homebrew)来运行Regex自定义规则。
Swiftlint可以自动纠正某些违规行为。磁盘上的文件被更正的版本覆盖。
在运行swiftlint --fix
固定,否则可能会丢失重要数据。
由于在应用更正时修改文件后,纠正措施(或其偏移)的可能性很高,因此在纠正时被禁用。
swiftlint analyze
命令可以使用完整的类型检查AST来覆盖Swift文件。包含清洁swiftc
构建命令调用的编译器日志路径(必须失败)通过--compiler-log-path
标志进行analyze
。例如--compiler-log-path /path/to/xcodebuild.log
这可以通过
xcodebuild -workspace {WORKSPACE}.xcworkspace -scheme {SCHEME} > xcodebuild.log
swiftlint analyze --compiler-log-path xcodebuild.log
分析仪规则往往比棉绒规则要慢得多。
Swiftlint提供了多种包含多个配置文件的方法。多个配置文件被合并到一个单个配置中,然后将其应用于单个配置文件会被应用。
在很多用例中,使用多个配置文件可能会有所帮助:
例如,人们可以使用团队范围的共享Swiftlint配置,同时通过儿童配置文件允许在每个项目中覆盖。
全队范围的配置:
disabled_rules :
- force_cast
特定于项目的配置:
opt_in_rules :
- force_cast
您可以在配置文件中指定child_config
和/或parent_config
引用。这些引用应该是相对于它们指定的配置文件的文件夹的本地路径。只要没有循环且没有歧义,这甚至递归起作用。
儿童配置被视为改进,因此具有较高的优先级,而父母配置在发生冲突的情况下被认为是优先级较低的基础。
以下是一个示例,假设您有以下文件结构:
ProjectRoot
|_ .swiftlint.yml
|_ .swiftlint_refinement.yml
|_ Base
|_ .swiftlint_base.yml
要同时包括细化和基本文件,您的.swiftlint.yml
应该看起来像:
child_config : .swiftlint_refinement.yml
parent_config : Base/.swiftlint_base.yml
合并父型和子配置时,请仔细included
和excluded
配置,以说明包含配置文件的目录位置的差异。
就像您可以提供本地child_config
/ parent_config
参考,而不是引用本地路径一样,您只能将导致配置文件的URL放置。为了使Swiftlint检测这些远程引用,它们必须从http://
或https://
开始。
引用的远程配置文件甚至可以递归地引用其他远程配置文件,但不允许包括本地参考。
使用远程引用,您的.swiftlint.yml
看起来像这样:
parent_config : https://myteamserver.com/our-base-swiftlint-config.yml
每次运行Swiftlint并具有Internet连接时,Swiftlint都会尝试获取每个引用的每个远程配置的新版本。如果此请求耗尽,则使用缓存版本(如果有)。如果没有可用的缓存版本,Swiftlint会失败 - 但不用担心,一旦Swiftlint至少成功运行一次,则应该在那里进行缓存的版本。
如果需要,可以使用remote_timeout
/ remote_timeout_if_cached
specifiers手动指定远程配置获取的超时。这些值默认为2秒或1秒。
您还可以通过命令行运行Swiftlint时仅提供一个配置文件,而是可以通过层次结构,在该层次结构中,第一个配置被视为父,而最后一个配置被视为最高优先级的孩子。
一个简单的示例,包括两个配置文件,如下所示:
swiftlint --config .swiftlint.yml --config .swiftlint_child.yml
除了主要配置(根文件夹中的.swiftlint.yml
文件)外,您还可以将其他配置文件命名为.swiftlint.yml
放入目录结构中,然后将其合并为儿童配置,但仅对这些效果产生效果与配置或没有其他配置文件的更深目录在同一目录内的文件。换句话说:嵌套配置无法递归工作 - 除了主要配置外,每个文件中最多可以使用一个嵌套配置。
.swiftlint.yml
文件仅被视为嵌套配置,如果它们尚未用于构建主配置(例如,通过child_config: Folder/.swiftlint.yml
引用的内容)。另外,嵌套配置的parent_config
/ child_config
规格也被忽略了,因为对此没有意义。
如果通过--config
参数明确指定一个(或更多)Swiftlint文件,则该配置将被视为覆盖,无论是否存在其他.swiftlint.yml
文件,目录中的某个地方都存在。因此,如果要使用嵌套配置,则不能使用--config
参数。
麻省理工学院许可。
Swiftlint由Realm Inc.维护和资助。领域的名称和徽标是Realm Inc.的商标。
我们❤️开源软件!请参阅我们的其他开源项目,阅读我们的博客,或在Twitter(@RealM)上打个电话。
我们感谢MacStadium提供了Mac Mini来进行我们的性能测试。