kokoichi206 / ios-app

1 stars 1 forks source link

Linter の導入 #6

Open kokoichi206 opened 2 years ago

kokoichi206 commented 2 years ago

以下の記事に従って実行してみた https://frog9.com/post/45

SwiftLint

kokoichi206 commented 2 years ago

1. swift lint の導入

brew install swiftlint

2. project ごとに設定

  1. プロジェクト名→Build Phases 画面に遷移し、「+」ボタンを押下。
  2. New Run Script Phasesを選択する
if type "/opt/homebrew/bin/swiftlint"  > /dev/null 2>&1; then
## format autocorrect
/opt/homebrew/bin/swiftlint --fix --format
## format check
/opt/homebrew/bin/swiftlint
elif type "/usr/local/bin/swiftlint" > /dev/null 2>&1; then
## format autocorrect
/usr/local/bin/swiftlint --fix --format
## format check
/usr/local/bin/swiftlint
else
  echo "swiftlint is not installed."
fi
kokoichi206 commented 2 years ago

Lint のルール変更

  1. cd [プロジェクトの場所]
  2. touch .swiftlint.yml

.swiftlint.ymlの記述例

# Swiftlint Rules

# 無効にするルール
disabled_rules:
  # 一行の文字数が多い
  - line_length
  # 関数の行数が多い
  - function_body_length
  # 関数の複雑度が大きい
  - cyclomatic_complexity
  # ファイルの行数が多い
  - file_length # ファイルの行数が多い
  # クラスの行数が多い
  - type_body_length # 

# 有効にするルール(標準ルールに追加して明示的に有効にする)
opt_in_rules:
  # 不要なbreak文がある
  - unneeded_break_in_switch
  # 強制キャストがある 
  - force_cast
  # +=などを使っていない
  - shorthand_operator
  # ToDoが残っている
  - todo

参考

SwiftLint 運用ノウハウ