LoginSignup
1
1

More than 5 years have passed since last update.

既存のプロジェクトにSwiftLintを導入してみた

Last updated at Posted at 2018-03-16

前置き

一人で対応しているiOSアプリ開発で、Swiftを書いた事がないメンバーが新しく来たため、ソースコードの統一性と品質をカバーできないかと、Swiftの静的解析ツールでよく聞くSwiftLintを試しに導入してみました。

対象環境

  • MacBook Pro (Retina 13-inch、Early 2015) 128GB
  • Mac OS Sierra
  • Xcode 9.2

前提

Homebrewがインストールされていること

インストール

下記コマンドを発行

$ brew install swiftlint

インストール確認

$ swiftlint version
0.25.0

設定

  1. Xcodeを開き、プロジェクトのSchemeからBuild Phasesを選択し、+からNew Run Script Phaseを選択する 01_01.png
  2. 追加したRun ScriptのType a script or drag a script file from your workspace to insert its pathに下記を設定する
  if which swiftlint >/dev/null; then
    swiftlint
  else
    echo "warning: SwiftLint not installed, download from 
  https://github.com/realm/SwiftLint"
  fi

01_02.png

ルールを追加

プロジェクトの.xcodeprojが配置されているディレクトリに移動し.swiftlint.ymlを追加する

$ cd 対象ディレクトリ
$ touch .swiftlint.yml

.swiftlint.ymlをエディタで開き、ルールを貼り付ける(下記は個人的に設定したもの)

swiftlint.yml
disabled_rules:
- trailing_whitespace
- force_cast
- line_length
- function_body_length

opt_in_rules:
- attributes
- closure_spacing
- empty_count
- explicit_init
- fatal_error_message
- first_where
- implicitly_unwrapped_optional
- missing_docs
- nimble_operator
- number_separator
- object_literal
- operator_usage_whitespace
- overridden_super_call
- private_outlet
- prohibited_super_call
- redundant_nil_coalescing
- sorted_imports
- switch_case_on_newline
- valid_docs

excluded:
- Carthage/

identifier_name:
  min_length: 2

file_length:
  warning: 600
  error: 1200

実行

設定したプロジェクトのスキーマをビルドする

結果

警告460件、ビルドエラー23件出て泣いた。
フォーマット系は対応が楽でしたが、implicitly_unwrapped_optionalやmultiple_closures_with_trailing_closureの対応をする際はある程度影響範囲が広かったため、対応が少し面倒でした。

今後

運用しつつ様子を見て、効果があるのか、また、ルール設定をどのようにした方がよいのか検討していきたい

その他

  • 既に他のスクリプトが設定されているRun ScriptにSwiftLintのスクリプトを設定してしまい10分ほど悩んだ...
  • Edit SchemeのBuild -> Pre-actionsのRun ScriptにSwiftLintのスクリプトを設定してしまいしばらく悩んだ...
  • .swiftlint.yml設定時にexcludedを忘れ、Carthage配下のライブラリも解析対象としてしまい、警告が999+ビルドエラーが999+出て唖然となった...

参考にさせて頂いた記事

1
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
1
1