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.3k
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
790
Other Decks in Technology
See All in Technology
Amazon S3 Tables + Amazon Athena / Apache Iceberg
okaru
0
230
DETR手法の変遷と最新動向(CVPR2025)
tenten0727
2
1k
データベースで見る『家族アルバム みてね』の変遷 / The Evolution of Family Album Through the Lens of Databases
kohbis
4
1.1k
Zabbixチョットデキルとは!?
kujiraitakahiro
0
160
ブラウザのレガシー・独自機能を愛でる-Firefoxの脆弱性4選- / Browser Crash Club #1
masatokinugawa
1
370
NLP2025 参加報告会 / NLP2025
sansan_randd
4
500
テキスト解析で見る PyCon APAC 2025 セッション&スピーカートレンド分析
negi111111
0
270
DuckDB MCPサーバーを使ってAWSコストを分析させてみた / AWS cost analysis with DuckDB MCP server
masahirokawahara
0
410
AI AgentOps LT大会(2025/04/16) Algomatic伊藤発表資料
kosukeito
0
120
Lightdashの利活用状況 ー導入から2年経った現在地_20250409
hirokiigeta
2
270
20250413_湘南kaggler会_音声認識で使うのってメルス・・・なんだっけ?
sugupoko
1
340
MCP Documentation Server @AI Coding Meetup #1
yyoshiki41
2
2.6k
Featured
See All Featured
Designing for humans not robots
tammielis
252
25k
Faster Mobile Websites
deanohume
306
31k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
135
33k
For a Future-Friendly Web
brad_frost
176
9.7k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Raft: Consensus for Rubyists
vanstee
137
6.9k
Testing 201, or: Great Expectations
jmmastey
42
7.4k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Making Projects Easy
brettharned
116
6.1k
Visualization
eitanlees
146
16k
Writing Fast Ruby
sferik
628
61k
Building Flexible Design Systems
yeseniaperezcruz
329
38k
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