Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftLintをもっと活用する
Search
natsumi_oishi
February 16, 2022
Technology
1
1.4k
SwiftLintをもっと活用する
ラクマ x STORES LTイベント ~ iOS開発の裏側 ~
https://hey.connpass.com/event/236189/
で発表した内容の資料です。
natsumi_oishi
February 16, 2022
Tweet
Share
More Decks by natsumi_oishi
See All by natsumi_oishi
マルチモジュールアーキテクチャと開発プロセス改善の取り組み
kurapy
4
810
Other Decks in Technology
See All in Technology
ITエンジニアを取り巻く環境とキャリアパス / A career path for Japanese IT engineers
takatama
4
1.5k
ソフトウェアは捨てやすく作ろう/Let's make software easy to discard
sanogemaru
10
5.8k
DevOpsDays Taipei 2025 -- Creating Awesome Change in SmartNews!
martin_lover
0
160
データプレーンプログラミングとは? DPU&スイッチASICの開発経験から語る
ebiken
PRO
1
260
新卒から4年間、20年もののWebサービスと向き合って学んだソフトウェア考古学 - PHPカンファレンス新潟2025 / new graduate 4year software archeology
oguri
2
360
What's Next in OpenShift Q2 CY2025
redhatlivestreaming
1
810
toittaにOpenTelemetryを導入した話 / Mackerel APM リリースパーティ
cohalz
1
490
Redmineの意外と知らない便利機能 (Redmine 6.0対応版)
vividtone
0
1.2k
金融システムをモダナイズするためのAmazon Elastic Kubernetes Service(EKS)ノウハウ大全
daitak
0
120
Cursor Meetup Tokyo
iamshunta
2
510
Digitization部 紹介資料
sansan33
PRO
1
3.8k
Azure Developer CLI と Azure Deployment Environment / Azure Developer CLI and Azure Deployment Environment
nnstt1
1
130
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Into the Great Unknown - MozCon
thekraken
39
1.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
750
Balancing Empowerment & Direction
lara
1
87
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
460
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
How GitHub (no longer) Works
holman
314
140k
Transcript
SwiftLintをもっと活⽤する Natsumi Oishi Rakuten Group, Inc.
2 • Natsumi Oishi • ラクマ iOS Engineer Who am
I?
3
4 1. .swiftlint.yml を使い分ける 2. SwiftLintの実⾏時間を短くする 3. analyze を使う Environment
• Xcode 13.2.1 • SwiftLint 0.46.2 Topics
5 .swiftlint.yml を使い分ける
6 プロダクトコードでの不具合を防ぐために⼊れたルールによって、テストコードが 書きにくくなる .swiftlint.yml を使い分ける テストが書きにくい 🥺 ⚠ force_unwrapping ❌
force_try
7 ProjectRoot ├ .swiftlint.yml ├ SampleProject ├ Hoge.swift └ Fuga.swift
└ SampleProjectTests ├ HogeTest.swift └ FugaTest.swift .swiftlint.yml を使い分ける プロダクトとテストでルールを分ける .swiftlint.yml で設定したルールが全体に適⽤される
8 ProjectRoot ├ .swiftlint.yml ├ SampleProject ├ Hoge.swift └ Fuga.swift
└ SampleProjectTests ├ HogeTest.swift ├ FugaTest.swift └ .swiftlint.yml .swiftlint.yml を使い分ける プロダクトとテストでルールを分ける .swiftlint.yml で設定したルールが全体に適⽤される SampleProjectTests/.swiftlint.yml で設定したルールが SampleProjectTests 配下に適⽤される
9 ProjectRoot ├ .swiftlint.yml ├ SampleProject ├ Hoge.swift └ Fuga.swift
└ SampleProjectTests ├ HogeTest.swift ├ FugaTest.swift └ .swiftlint.yml .swiftlint.yml を使い分ける プロダクトとテストでルールを分ける ⚠ force_unwrapping ❌ force_try ❌ force_cast ✅ force_unwrapping ✅ force_try ❌ force_cast opt_in_rules: - force_unwrapping # force_try, force_castは有効 .swiftlint.yml disabled_rules: - force_unwrapping - force_try SampleProjectTests/.swiftlint.yml
10 親︓ .swiftlint.yml ⼦︓ SampleProjectTests/.swiftlint.yml .swiftlint.yml を使い分ける プロダクトとテストでルールを分ける • 親の設定が⼦にも反映する
• 親と⼦の設定が競合した場合、⼦の設定が優先される
11 ディレクトリ内に親と⼦のymlファイルを配置して、 App Target > Build Phases > Run Script
Phaseで `swiftlint` コマンドを実⾏する (オプションなどの追加をしなくても勝⼿に使い分けてくれる) .swiftlint.yml を使い分ける 実⾏⽅法 - Xcode
12 --config オプションで親⼦関係のすべてのymlファイルを指定する .swiftlint.yml を使い分ける 実⾏⽅法 – コマンドライン $ swiftlint
\ --config .swiftlint.yml \ --config SampleProjectTests/.swiftlint.yml 優先順位の低い順(親→⼦の順) で指定する
13 SwiftLintの実⾏時間を短くする
14 ⾃動修正の後に静的解析を実⾏すると、実⾏時間がどうしても⻑くなりがち SwiftLintの実⾏時間を短くする 実⾏時間が⻑い 🤔 $ swiftlint --fix --format $
swiftlint
15 SwiftLintの実⾏時間を短くする 必要なコードにのみSwiftLintを実⾏する https://github.com/realm/SwiftLint/issues/413 #issuecomment-184077062 変更があるファイル名を抽出、 そのファイルだけを対象に SwiftLintを実⾏
16 SwiftLintの実⾏時間を短くする 必要なコードにのみSwiftLintを実⾏する # 全てのファイルに対してSwiftLintを実⾏する run_swiftlint_all() { swiftlint --fix --format
swiftlint } # 指定したファイルに対してのみSwiftLintを実⾏する run_swiftlint() { local filename="${1}" if [[ "${filename##*.}" == "swift" ]]; then swiftlint --fix --format --path "${filename}" swiftlint --path "${filename}" fi }
17 SwiftLintの実⾏時間を短くする 必要なコードにのみSwiftLintを実⾏する # 変更があるファイルを抽出する diff_unstaged_files=`git diff --name-only` diff_staged_files=`git diff
--cached --name-only` diff_files=`echo "${diff_unstaged_files}"; echo "${diff_staged_files}"` if [ "`echo ${diff_files} | grep ".swiftlint.yml"`" ]; then # .swiftlint.ymlに変更があればすべてのファイルに対してSwiftLintを実⾏する run_swiftlint_all else # 変更があるファイルにのみSwiftLintを実⾏する echo "${diff_files}" | while read filename; do run_swiftlint "${filename}” done fi
18 analyze を使う
19 • 型チェックされたAST (abstract syntax tree、抽象構⽂⽊) を使い、通常の静的解 析より⾼度な分析ができる • README.mdには
”experimental” と記述がある • .swiftlint.yml の analyzer_rules にanalyze⽤のルールを記述する analyze を使う analyze?
20 analyze を使う analyzer_rules? https://realm.github.io/SwiftLint/ unused_import.html
21 1. DerivedDataをクリーンする(差分ビルドは analyze コマンドでは動作しない) 2. xcodebuild コマンドでコンパイラログを取得する 3. コンパイラログのパスを渡して
`swiftlint analyze` を実⾏ analyze を使う 実⾏⽅法
22 analyze を使う 実⾏⽅法 #!/bin/bash # https://github.com/realm/SwiftLint#analyze-experimental workspace=”YOURWORKSPACE.xcworkspace" scheme=”YOURSCHEME" logfile="xcodebuild.log"
# 1. DerivedDataをクリーンする xcodebuild clean \ -workspace ${workspace} \ -scheme ${scheme} # 2. コンパイラログを取得する xcodebuild build \ -workspace ${workspace} \ -scheme ${scheme} > ${logfile} # 3.コンパイラログのパスを渡して `swiftlint analyze` を実⾏ swiftlint analyze \ --compiler-log-path ${logfile} \ --fix exit 0 run_analyze.sh
23 • 実⾏時間が⾮常に⻑い • analyzer_rules がまだ4つしかなく、絶対に有効にしたいルールが無い analyze を使う イマイチなところ 🤔
• capture_variable • explicit_self • unused_declaration • unused_import
24 1. .swiftlint.yml を使い分ける 2. SwiftLintの実⾏時間を短くする 3. analyze を使う まとめ
None