7
1

More than 3 years have passed since last update.

Interface Builderの静的解析ツール「IBLinter」のセットアップ方法

Last updated at Posted at 2021-02-25

「IBLinter」とは?

Interface Builder(以下「IB」と呼ぶ)用の静的解析ツールです。

環境

  • OS:macOS Big Sur 11.1
  • Xcode:12.3 (12C33)
  • Swift:5.3.2
  • IBLinter:0.4.25

セットアップ

インストール

Mintからインストールします。

Mintfile
+ IBDecodable/IBLinter@0.4.25
$ mint bootstrap

ビルド時に静的解析を行うようにする

Xcodeで対象のプロジェクトファイルを開く>TARGETS
→対象のターゲットを選択>[Build Phases]タブ
→左上にある[+]をクリック>New Run Script Phase
Run Scriptに以下のスクリプトを記述すると、ビルド時に静的解析されるようになります。

if which mint >/dev/null; then
  mint run IBDecodable/IBLinter iblinter lint
else
  echo "warning: Mint not installed, download from https://github.com/yonaskolb/Mint"
fi

ルールの詳細設定

プロジェクトのルートディレクトリに .iblinter.yml を作成し、その中にルールの詳細を記述します。
設定の書き方は公式ページに詳しく載っています。

私の設定ファイルも公開します(随時更新)。

.iblinter.yml
# デフォルト有効で無効にするルール
disabled_rules:
  #- enable_autolayout
  #- duplicate_constraint
  #- duplicate_id
  #- custom_module
  #- ambiguous

# デフォルト無効で有効にするルール
enabled_rules:
  #- custom_class_name # ファイル名は末尾の `ViewController` を省略したいため
  - relative_to_margin
  - misplaced
  #- storyboard_viewcontroller_id # IDは末尾の `ViewController` を省略したいため
  - stackview_backgroundcolor
  - image_resources
  - use_base_class
  - view_as_device
  - reuse_identifier
  - color_resources
  - use_trait_collections

# 対象外のファイル・フォルダ
excluded:
  - Pods
  - Carthage
  - SourcePackages
  - Generated

view_as_device_rule:
  device_id: retina6_1 # iPhone 11

静的解析

ここまで設定したら、ビルドするたびに静的解析が行われます。
警告やエラーが出たら、コードを修正するか、ルールを緩和するかしましょう!

参考リンク

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1