arusu0629のブログ

UnityエンジニアからiOSエンジニアへ

.swiftlint.yml の中身をザックリ把握

【概要】
業務で使用している .swiftlint.yml の中身について理解を深める

# 無効にするルール。
disabled_rules:

# コードの記載のない改行を許可したい
- trailing_whitespace

# force_cast は場合によっては使用するため
- force_cast // object as! String を許可

# 変数名の直後に ":" を置かないのを許可する
- colon // let noWarning :String = "" , let noWarning : String = "" を許可

# "," の前にスペースを入れないのと、 "," の後にスペースを入れなくてよいのを許可する
- comma // (a,b), (c , d) // これらを許可

# identifier_name 変数名にアンスコ使いたい
- identifier_name // 3文字~41文字以内の制限を外すため

# 一行で書いたほうが簡潔な場合もある
- switch_case_on_newline // case .sunday: print("Today is Holiday") を許可する

# 値を使わない enum case を許可するため
- empty_enum_arguments // case monday(day: Int) の時に case .monday(_): を許可する

# typealiasで1文字も使うため
- type_name // typealias x = Void を許可

# 1つのメソッド内で条件式を11個以上使うのを許可する
- cyclomatic_complexity  // Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 11 (cyclomatic_complexity)

# switch 文で fallthrough を許可するため
- fallthrough // case .bar: fallthrough case .bar2: something() がOK

# デフォルト無効のルールのうち、有効にするもの。
opt_in_rules:
- attributes                     // @IBOutlet \n private var label: UILabel を許可
- closure_end_indentation        // クロージャのインデントを揃える
- closure_spacing                // クロージャの処理を始める際に空白スペースを入れる [].map { print($0) }
- conditional_returns_on_newline // return は改行する if true { return } は NG
- empty_count                    // if [Int]().count == 0 は NG [Int]().isEmpty を使用する
- explicit_init                  // let hoge = String.init("hoge") は NG
- fatal_error_message            // fatalError("") は NG fatalError("Error") など必ずメッセージ有りにする
- first_where                    // filter して first よりも first(where:) を使うことを推奨する
                                 // let hasWarning = hoge.filter { $0 > 1 }.first
                                 // let noWarning  = hoge.first { $0 > 1 }
- missing_docs                   // クラス外の public メソッドや public な protocol には docs を付ける必要がある
- nimble_operator                // 簡単(単純な?)な演算子比較を行いましょう?(よく分からなかった)
                                 // hasWarning: expect(10).to(equal(10))
                                 // noWarning : expect(10) == 10
- overridden_super_call          // (いくつかの)オーバーライドしたら親のメソッドを必ず呼ぶ & 複数回呼ぶのは NG
- prohibited_super_call          // (いくつかの)オーバーライドした親のメソッドを呼んではいけない override func loadView() { super.loadView() } は NG
- redundant_nil_coalescing       // 値が nil だと分かっている時に nil 結合演算子 を使うのはダメ var hoge: Int? = nil; var fuga = hoge ?? nil は NG
- sorted_imports                 // import ファイル名は名前でソートさせる
- valid_docs                     // missing_docs に変わった?

excluded:
# 追加した OSS ライブラリは対象に含めたくない
- Pods/
- Carthage/

# 1行あたりの文字数制限を300に変更
# プロジェクト作成時にデフォルトで追加されるコメントをひっかけないため
line_length: 300

# Array や Dictionary 中の末尾の "," を矯正で付ける
trailing_comma:
    mandatory_comma: true

# elseは改行する
statement_position:
    statement_mode: uncuddled_else

# メソッドの行数は50行以下にする
function_body_length: 50

【おまけ】

$ swiftlint autocorrect --format // .swiftlint.yml に応じて自動フォーマットしてくれる
$ swiftlint // 構文チェック