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
800
Other Decks in Technology
See All in Technology
ソフトウェアテスト 最初の一歩 〜テスト設計技法をワークで体験しながら学ぶ〜 #JaSSTTokyo / SoftwareTestingFirstStep
nihonbuson
PRO
1
150
encoding/json v2を予習しよう!
yuyu_hf
PRO
0
120
UIパフォーマンス最適化: AIを活用して100倍の速度向上を実現した事例
kinocoboy2
0
130
Azure × MCP 入門
ry0y4n
8
1.7k
AI-in-the-Enterprise|OpenAIが公開した「AI導入7つの教訓」——ChatGPTで変わる企業の未来とは?
customercloud
PRO
0
160
DjangoCon Europe 2025 Keynote - Django for Data Science
wsvincent
0
560
Google Cloud Next 2025 Recap 生成AIモデルとマーケティングでのコンテンツ生成 / Generative AI models and content creation in marketing
kyou3
0
180
LangfuseではじめるAIアプリのLLMトレーシング
codenote
0
150
Part1 GitHubってなんだろう?その1
tomokusaba
3
770
AI駆動で進化する開発プロセス ~クラスメソッドでの実践と成功事例~ / aidd-in-classmethod
tomoki10
1
1.1k
Why Platform Engineering? - マルチプロダクト・少人数 SRE の壁を越える挑戦 -
nulabinc
PRO
5
410
データベース04: SQL (1/3) 単純質問 & 集約演算
trycycle
PRO
0
730
Featured
See All Featured
Bash Introduction
62gerente
613
210k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Practical Orchestrator
shlominoach
187
11k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
A better future with KSS
kneath
239
17k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
Navigating Team Friction
lara
185
15k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.2k
A designer walks into a library…
pauljervisheath
205
24k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Mobile First: as difficult as doing things right
swwweet
223
9.6k
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