Skip to content

TIPS SwiftLintのルール

stv-ekushida edited this page Apr 18, 2018 · 1 revision

.swiftlint.yml

# 対象にするパス
included:
  - <Project Name>
  - <Project Name>Tests

# 対象から除外するパス
excluded:
  - Pods
  - Tests/SwiftLintFrameworkTests/Resources

# 採用するopt-inルール
opt_in_rules:
  - closure_end_indentation
  - closure_spacing
  - conditional_returns_on_newline
  - contains_over_first_not_nil
  - empty_count
  - explicit_init
  - fatal_error_message
  - file_header
  - first_where
  - force_unwrapping
  - implicitly_unwrapped_optional
  - object_literal
  - operator_usage_whitespace
  - overridden_super_call
  - private_outlet
  - redundant_nil_coalescing
  - sorted_imports
  - switch_case_on_newline
  - unneeded_parentheses_in_closure_argument
  - vertical_parameter_alignment_on_call

file_header:
  required_pattern: |
                    \/\/
                    \/\/  .*\.swift
                    \/\/  .*?
                    \/\/
                    \/\/  Created by .*? on \d{1,4}\/\d{1,2}\/\d{1,2}\.
                    \/\/  Copyright © \d{4}.*? .*?\. All rights reserved\.
                    \/\/

# ファイルヘッダのサンプル
#                    //
#                    //  AppDelegate.swift
#                    //  DemoApp
#                    //
#                    //  Created by OkuderaYuki on 2017/12/30.
#                    //  Copyright © 2017年 OkuderaYuki. All rights reserved.
#                    //

file_length: 600
line_length: 120
number_separator:
  minimum_length: 5
variable_name_min_length:
  - 3
  - 2
force_cast: error
force_try: error

# 無効にするルール
disabled_rules:
  - leading_whitespace
  - trailing_whitespace

ルールの一覧

swiftlint rules
ルール 説明 必須
array_init
attributes
block_based_kvo
class_delegate_protocol Protcolはweakで保持される可能性があるため、class-onlyなProtocolにすべき
closing_brace }の後に)が続く場合,間に空白があるべきでない
closure_end_indentation
closure_parameter_position クロージャの引数リストはクロージャの始まり{と同じ行にあるべき.
closure_spacing クロージャ内の式はカッコの間に1つのスペースがあるべき
colon :は変数名の直後に書き,型名はその後に1スペース開けて書くべき
comma ,の前にはスペースを入れず,,の後には1つのスペースを入れるべき
compiler_protocol_init リテラルを直接書き並べて初期化するタイプのinitializerは直接呼び出すべきではない
conditional_returns_on_newline 条件文は始まった行の次の行でreturnするべき
contains_over_first_not_nil
control_statement if等の制御文の条件式部分は( )で囲むべきでない
custom_rules
cyclomatic_complexity  関数内の複雑性(ネストの深いifやifの連続)はさけるべき
discarded_notification_center_observer
discouraged_direct_init NotificationCenter.addObserver(forName:object:queue:using:)で追加されたobserverは後で除去できるように変数に格納して保持するべき
dynamic_inline @inline指定とdynamicを同時に使うべきでない
empty_count 要素が何もないことを確認する際はcount == 0よりもisEmptyを用いるべき
empty_enum_arguments 末尾クロージャを使用するときはメソッド名の後に引数のない空の()は書くべきでない
empty_parameters 関数の引数がない時,Void ->でなく() ->を用いるべき
empty_parentheses_with_trailing_closure 末尾クロージャを使用するときはメソッド名の後に引数のない空の()は書くべきでない
explicit_enum_raw_value
explicit_init 明示的な.init()メソッドの呼び出しは避けるべき
explicit_top_level_acl
explicit_type_interface
extension_access_modifier
fallthrough
fatal_error_message
file_header 各ファイルは一貫性のあるヘッダコメントを持つべき
file_length ファイルの行数は多くなりすぎないようにすべき
first_where .filter { }.firstよりも.first(where:)を用いべき
for_where for文において,その内部でifによる値チェック処理しかしないのであればwhere句を使用すべき
force_cast 強制キャスト(as!)は避けるべき
force_try try!の使用は避けるべき
force_unwrapping 強制アンラップは避けるべき
function_body_length 関数の行数は長くすべきでない(41行以上)
function_parameter_count 関数の引数は少なくすべき(6個以内)
generic_type_name  ジェネリクスの型名は大文字のアルファベットから初めて,英数字のみを含み,かつ1〜20文字の長さにするべき
identifier_name 変数名等の識別子は小文字で始まるか全て大文字の英数字だけで構成させるべき
implicit_getter read-onlyな計算プロパティはgetキーワードを書くべきでない
implicit_return
implicitly_unwrapped_optional
is_disjoint
joined_default_parameter
large_tuple タプルのメンバは少なめにすべき
leading_whitespace ファイルの先頭に空白類文字を含むべきでない
legacy_cggeometry_functions
legacy_constant
legacy_constructor
legacy_nsgeometry_functions
let_var_whitespace
line_length 1行における文字数は多くなりすぎないようにすべき
literal_expression_end_indentation
mark  MARKコメントは正しいフォーマットで書くべき
multiline_arguments
multiline_parameters
multiple_closures_with_trailing_closure
nesting  ネスト型は深くても1レベルの深さまで,その他の文は深くても5レベルの深さまでのネストに留めるべき
nimble_operator
no_extension_access_modifier
no_grouping_extension
notification_center_detachment NSNotificationCenterに登録したobserverはクラスのdeinit内で除去するべき
number_separator 大きな数値を書く際はセパレータ(1_000_000 みたいな)を書くべき
object_literal イニシャライザを直接呼び出すよりは#imageLiteralや#colorLiteralを用いるべき
opening_brace 関数等の開き{は直前に1つのスペースを置いてから書き,なおかつ宣言と同じ行に書くべき
operator_usage_whitespace 演算子を使用する際は前後に1つのスペースを開けるべき
operator_whitespace 演算子を定義する際は演算子の前後に1つのスペースを置くべき
overridden_super_call オーバーライドされたメソッドは常にsuperを呼び出すべき
override_in_extension
pattern_matching_keywords
private_outlet @IBOutlet変数はprivate修飾するべき
private_over_fileprivate
private_unit_test privateなテストケースメソッドは書かない
prohibited_super_call
protocol_property_accessors_order
quick_discouraged_call
quick_discouraged_focused_test
quick_discouraged_pending_test
redundant_discardable_let 関数の戻り値を参照しない場合、letはわざわざ書くべきではない
redundant_nil_coalescing nil結合演算子において,左辺がnilの場合のみ評価される性質上,右辺にnilを書くのは冗長なため,書くべきでない
redundant_optional_initialization オプショナル変数宣言時のnil初期化は冗長なため,書くべきでない
redundant_string_enum_value String型のenumの各caseの値をcase名と同じにする際は,その処理は省略できるので書くべきでない
redundant_void_return Voidを返す関数の宣言時に-> Voidを書くのは冗長なため,書くべきでない
return_arrow_whitespace 関数の戻り値を指定する->と型名は,それぞれ1つのスペースで区切られるか,別の行に書くべき
shorthand_operator  複号代入演算子をなるべく使うべき
single_test_class
sorted_first_last
sorted_imports import部分はソートされているべき
statement_position else節やcatch節は直前の}の後に1つのスペースを置き,同じ行に書くべき
strict_fileprivate
superfluous_disable_command
switch_case_alignment
switch_case_on_newline switch文におけるcaseの各処理は改行した後書くべき
syntactic_sugar 糖衣構文を使うべき
todo TODOおよびFIXMEコメントは避けるべき
trailing_closure
trailing_comma ArrayやDictionary中の末尾の,は避ける/強制すべき
trailing_newline 各ファイルは末尾に空行を1行だけ持つべき
trailing_semicolon 各行の末尾にセミコロンは書くべきでない
trailing_whitespace 各行の末尾に空白類文字を書くべきでない
type_body_length 型の本体の行数は多くなりすぎないようにすべき
type_name 型名は大文字アルファベットで始まる英数字だけで構成され,かつ3~40文字であるべき
unneeded_break_in_switch
unneeded_parentheses_in_closure_argument
unused_closure_parameter 使用されないクロージャの引数は_で置き換えるべき
unused_enumerated for文でindexないしは要素が使用されないときは.enumerated()の記述は除去できるので除去すべき
unused_optional_binding let _ =によるOptional Bindingよりは!= nilでOptional判定したほうが良い
valid_ibinspectable @IBInspectable属性は明示的な型を持った変数とサポートされた型にのみ用いるべき
vertical_parameter_alignment 関数の引数を複数行に分けて書く際はそのアラインメントを揃えるべき
vertical_parameter_alignment_on_call
vertical_whitespace 空行は1行に抑えるべき
void_return -> ()よりは-> Voidを用いるべき
weak_delegate 循環参照を避けるため,Delegateはweakで保持されるべき
xctfail_message
Clone this wiki locally