返回顶部
s

swiftlintSwiftLint

Swift linting and style enforcement via CLI

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
270
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

swiftlint

SwiftLint

通过静态分析强制执行 Swift 风格和约定。对整个项目进行代码检查,自动修正可修复的违规,管理规则,并与 Xcode 和 CI 集成——全部通过命令行完成。



验证安装

bash
swiftlint version

如果未安装:

bash
brew install swiftlint

或通过 Mint:

bash
mint install realm/SwiftLint

或作为 Swift Package Manager 插件(添加到 Package.swift):

swift
.package(url: https://github.com/realm/SwiftLint.git, from: 0.57.0)

然后运行:

bash
swift package plugin swiftlint



基本用法

检查当前目录

bash
swiftlint

这将递归检查当前目录中的所有 .swift 文件。

检查特定路径

bash
swiftlint lint --path Sources/

检查特定文件

bash
swiftlint lint --path Sources/App/ViewModel.swift

从标准输入检查

bash
cat MyFile.swift | swiftlint lint --use-stdin --quiet

Agent 指导: 当用户说检查我的代码风格或检查我的 Swift 代码时,从项目根目录运行 swiftlint。如果他们指向特定文件或文件夹,使用 --path。


自动修正

SwiftLint 可以自动修复某些违规。

修复所有可自动修正的违规

bash
swiftlint --fix

修复特定路径

bash
swiftlint --fix --path Sources/

修复特定文件

bash
swiftlint --fix --path Sources/App/ViewModel.swift

预览将要修复的内容(试运行)

先检查以查看违规,然后修复:

bash
swiftlint lint --path Sources/ && swiftlint --fix --path Sources/

Agent 指导: 在自动修正之前始终先进行检查,以便用户看到将要更改的内容。某些违规无法自动修正——在修复后单独报告这些违规。


输出格式

默认(人类可读)

bash
swiftlint

输出:Sources/App.swift:12:1: warning: Line Length Violation: ...

JSON

bash
swiftlint lint --reporter json

CSV

bash
swiftlint lint --reporter csv

Checkstyle(XML)

bash
swiftlint lint --reporter checkstyle

GitHub Actions

bash
swiftlint lint --reporter github-actions-logging

Xcode 摘要(plist)

bash
swiftlint lint --reporter xcode-summary

SonarQube

bash
swiftlint lint --reporter sonarqube

Markdown

bash
swiftlint lint --reporter markdown

保存到文件

bash
swiftlint lint --reporter json > swiftlint-results.json

所有可用的报告器

报告器格式最佳用途
xcodeXcode 兼容(默认)本地开发
json
JSON 数组 | 程序化处理 | | csv | CSV | 电子表格分析 | | checkstyle | XML | Jenkins、CI 工具 | | codeclimate | Code Climate JSON | Code Climate 集成 | | github-actions-logging | GitHub 注释 | GitHub Actions CI | | sonarqube | SonarQube JSON | SonarQube 集成 | | markdown | Markdown 表格 | PR 评论 | | emoji | 表情符号装饰 | 有趣的终端输出 | | html | HTML 报告 | 浏览器查看 | | junit | JUnit XML | 测试报告工具 | | xcode-summary | Plist | Xcode 构建摘要 |

Agent 指导: 当需要以编程方式解析结果时使用 --reporter json。在 GitHub Actions 上使用 --reporter github-actions-logging 以在 PR 上获得内联注释。


规则

列出所有规则

bash
swiftlint rules

搜索特定规则

bash
swiftlint rules | grep force_cast

显示规则详情

bash
swiftlint rules force_cast

规则标识符快速参考

规则分为以下几类:

默认启用(常见)

规则捕获内容
linelength超过最大长度的行(默认警告 120,错误 200)
trailingwhitespace
行尾空白 |
| trailing_newline | 缺少或多余的行尾换行符 |
| opening_brace | 左大括号位置 |
| closing_brace | 右大括号位置 |
| colon | 冒号间距(例如 let x : Int → let x: Int) |
| comma | 逗号间距 |
| force_cast | 使用 as! |
| force_try | 使用 try! |
| force_unwrapping | 对可选类型使用 !(可选加入) |
| typebodylength | 类型体超过最大行数 |
| functionbodylength | 函数体超过最大行数 |
| file_length | 文件超过最大行数 |
| cyclomatic_complexity | 高圈复杂度 |
| nesting | 深层嵌套级别 |
| identifier_name | 命名约定违规 |
| type_name | 类型命名约定违规 |
| unused_import | 未使用的导入语句(可选加入) |
| unused_declaration | 未使用的声明(可选加入) |
| vertical_whitespace | 过多的空行 |
| todo | TODO/FIXME 注释作为警告 |
| mark | 不正确的 MARK 注释格式 |
| void_return | 显式 -> Void 而不是 -> () |
| syntactic_sugar | 优先使用 [Int] 而不是 Array |
| redundantoptionalinitialization | var x: Int? = nil(nil 是默认值) |
| redundantstringenum_value | 枚举 case 名称匹配原始值 |

可选加入(必须显式启用)

规则捕获内容
explicittypeinterface缺少显式类型注解
missing_docs
缺少文档注释 |
| multiline_arguments | 多行函数调用格式 |
| multiline_parameters | 多行函数参数格式 |
| verticalparameteralignment | 声明中的参数对齐 |
| sorted_imports | 未排序的导入语句 |
| file_header | 缺少或不正确的文件头 |
| accessibilitylabelfor_image | 没有无障碍标签的图像 |
| accessibilitytraitfor_button | 没有无障碍特性的按钮 |
| strict_fileprivate | 优先使用 private 而不是 fileprivate |
| prohibitedinterfacebuilder | Storyboard/XIB 使用 |
| nomagicnumbers | 代码中的魔法数字 |
| preferselfinstaticreferences | 在静态上下文中使用 Self 而不是显式类型名 |
| balancedxctestlifecycle | 有 setUp 但没有 tearDown |
| testcaseaccessibility | 测试方法未正确标记 |

Agent 指导: 为新项目设置 SwiftLint 时,从默认规则开始,只添加用户特别请求的可选加入规则。不要用所有可能的规则让用户不知所措。


配置(.swiftlint.yml)

SwiftLint 从当前目录(或父目录)读取 .swiftlint.yml。

生成默认配置

没有内置的生成器,但这里有一个最小配置:

yaml

.swiftlint.yml

要包含的路径(默认:所有 Swift 文件)

included: - Sources - Tests

要排除的路径

excluded: - Pods - DerivedData - .build - Packages

禁用特定规则

disabled_rules: - todo - trailing_whitespace

启用可选加入规则

optinrules: - sorted_imports - unused_import - missing_docs

配置特定规则

line_length: warning: 120 error: 200 ignores_comments: true ignores_urls: true

typebodylength:
warning: 300
error: 500

file_length:
warning: 500
error: 1000

functionbodylength:
warning: 50
error: 100

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 swiftlint-1776187767 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 swiftlint-1776187767 技能

通过命令行安装

skillhub install swiftlint-1776187767

下载

⬇ 下载 swiftlint v1.0.0(免费)

文件大小: 6.61 KB | 发布时间: 2026-4-15 13:42

v1.0.0 最新 2026-4-15 13:42
Initial release of the swiftlint skill.

- Enforces Swift style and conventions via SwiftLint CLI.
- Guides users on installation, configuration, and basic usage (linting, autocorrect, and output formats).
- Documents rule categories (enabled by default and opt-in) and common configuration options.
- Includes agent guidance for handling code style checks and linting requests.
- Provides reference commands for working with SwiftLint in local and CI environments.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部