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
登壇ネタの見つけ方 / How to find talk topics
pinkumohikan
3
290
Snowflake Summit 2025 データエンジニアリング関連新機能紹介 / Snowflake Summit 2025 What's New about Data Engineering
tiltmax3
0
230
CI/CDとタスク共有で加速するVibe Coding
tnbe21
0
230
Абьюзим random_bytes(). Фёдор Кулаков, разработчик Lamoda Tech
lamodatech
0
280
IAMのマニアックな話 2025を執筆して、 見えてきたAWSアカウント管理の現在
nrinetcom
PRO
4
660
Observability infrastructure behind the trillion-messages scale Kafka platform
lycorptech_jp
PRO
0
130
Create a Rails8 responsive app with Gemini and RubyLLM
palladius
0
140
Clineを含めたAIエージェントを 大規模組織に導入し、投資対効果を考える / Introducing AI agents into your organization
i35_267
4
1.4k
2025/6/21 日本学術会議公開シンポジウム発表資料
keisuke198619
2
480
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
180
知識を整理して未来を作る 〜SKDとAI協業への助走〜
yosh1995
0
150
第9回情シス転職ミートアップ_テックタッチ株式会社
forester3003
0
140
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Designing for Performance
lara
609
69k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Writing Fast Ruby
sferik
628
61k
Side Projects
sachag
455
42k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Why Our Code Smells
bkeepers
PRO
337
57k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
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