SwiftLint-iOSプロジェクトの清潔さと順序

画像 A.

iOSプロジェクトのコードスタイルと規則への準拠を維持することがどれほど難しいかは誰もが知っていると思います。 今日は、SwiftLintユーティリティを使用してこのプロセスを自動化する方法について説明します。

SwiftLintは、Swiftコードを自動的にチェックするためのRealm開発者からのユーティリティです。 このユーティリティには、 GitHubのSwift Style Guideと常識に基づいた一連のルールが含まれています。 もちろん、独自のルールを追加できます。 SwiftLintは、Xcode、Appcode、Atomとの統合をサポートしています。

インストールとセットアップ
リポジトリから最新リリースをダウンロードしてインストールします

または、ターミナルからインストールします。

$ brew update $ brew install swiftlint 

次のようにユーティリティを更新できます。

 $ brew update $ brew upgrade swiftlint 

別のバージョンに切り替えます:

 $ brew switch swiftlint < > 

現在のバージョンを確認します。

 swiftlint version 

プロジェクトの設定、ビルドフェーズに進み、スクリプトの実行セクションに追加します。

 if which swiftlint >/dev/null; then swiftlint else echo "error: SwiftLint does not exist, download it from https://github.com/realm/SwiftLint" exit 1 fi 

ヒント:「exit 1」コマンドを使用することを強くお勧めします-これにより、チームのすべてのメンバーがSwiftLintを確実にインストールします。

最初の打ち上げ
SwiftLintがインストールされたので、各プロジェクトのビルドの最後にコードがチェックされます。 最初の実行後、ほとんどのようなものが表示されます。



心配する必要はありません。ほとんどのエラーと警告は非常にシンプルで簡単に修正できます。 SwiftLintにはすばらしい自動修正機能があるため、手動で修正するのを急がないでください。非常に安全です。 ターミナルで使用するには、プロジェクトディレクトリに移動し、コマンドを実行します。

 swiftlint autocorrect 

プロジェクトを再度組み立てています。エラーの数は何度も削減されていますが、残りのエラーは手動で修正する必要があります。
ヒント:スクリプトの実行に自動修正を追加しないでください。追加すると、プログラマーが考えないことに慣れたり、いくつかの規則を知らなかったりします。

ルール
次のコマンドを実行すると、事前定義されたルールのリストを表示できます。

 swiftlint rules 



各ルールには説明と一連のパラメーターがあります。


ルールのソースコードを参照して、理解を深めることもできます。

それとは別に、いくつかの非常に便利なルールを強調したいと思います。


構成
ほとんどの場合、ルールを無効化、構成、または追加する必要があります。 これを行うには、プロジェクトルートに.swiftlint.ymlファイルを作成します。

利用可能な構成オプション:

無効にするルールのリスト:

 disabled_rules: - colon - comma - control_statement 

有効にする必要があるオプションのルールのリスト(デフォルトでは無効):

 opt_in_rules: - empty_count - missing_docs 

サブディレクトリまたはファイルを除外する:

 excluded: - Carthage - Pods - Source/ExcludedFolder - Source/ExcludedFile.swift 

サブディレクトリまたはファイルを含める(代替を除く):

 included: - MyProject - MyProjectKeyboard - MyProjectTests 

規則パラメーター(使用可能なパラメーターは規則リストにあります):

 file_length: warning: 500 error: 600 

レポートタイプ(利用可能なパラメーター:xcode、json、csv、checkstyle、junit):

 reporter: xcode 

警告の最大数:

 warning_threshold: 15 

ヒント:警告の数が増えないように、必ずwarning_thresholdを構成ファイルに追加してください。

ネストされた構成
異なるサブディレクトリ用に複数の構成ファイル(.swiftlint.yml)を作成できます。 SwiftLintは、スキャンされたファイルがあるフォルダーにある構成を自動的に使用します。 ネストされた構成の除外されたオプションと含まれたオプションは無視されます。

ルールを追加する
SwiftLintでは、正規表現に基づいて独自のルールを追加できます。 これを行うには、.swiftlint.ymlファイルにcustom_rulesセクションを追加します。 ルールには次のパラメーターを指定できます。


サフィックス-idを使用した命名規則のチェック。 (例:userId-true、userID-不適切):

 custom_rules: id_suffix_naming: name: "Wrong name" regex: "(ID)" match_kinds: - comment - identifier message: "Use 'Id' instead 'ID'" severity: error 



コードでルールを無効にする
コード部分で検証を無効にする場合は、次の構成を使用します。

 // swiftlint:disable <rule1> [<rule2> <rule3>...] ......... // swiftlint:enable <rule1> [<rule2> <rule3>...] func printName() { // swiftlint:disable force_cast let name = loadName() as! String // swiftlint:enable force_cast print(name) } 

プロジェクトのビルド速度
明らかに、SwiftLintを使用すると、プロジェクトのビルドに余分な時間がかかります。 ビルド速度が著しく低下した場合は、変更されたファイルのみをチェックすることを検討する必要があります。 これを行うには、ビルドフェーズの検証スクリプトを次のように置き換えます。

スクリプト
 count=0 for file_path in $(git ls-files -om --exclude-from=.gitignore | grep ".swift$"); do export SCRIPT_INPUT_FILE_$count=$file_path count=$((count + 1)) done for file_path in $(git diff --cached --name-only | grep ".swift$"); do export SCRIPT_INPUT_FILE_$count=$file_path count=$((count + 1)) done export SCRIPT_INPUT_FILE_COUNT=$count swiftlint lint --use-script-input-files 


有名企業でのSwiftLint構成のいくつかの例:

キックスターター
 disabled_rules: - function_parameter_count - nesting - variable_name - weak_delegate - trailing_comma opt_in_rules: - empty_count - force_unwrapping - private_outlet line_length: 110 type_body_length: warning: 300 error: 400 excluded: - Carthage/ - Frameworks/ - Kickstarter-iOS.playground/ - Kickstarter-tvOS.playground/ - Library/Strings.swift - bin/strings.swift reporter: "xcode" custom_rules: localized_lensing: name: "Localized Lensing" regex: "\.~\s+Strings\s*\." message: "Capture calls to `Strings` functions using `%~ { _ in Strings... }`" severity: error 


Firefox
 disabled_rules: # rule identifiers to exclude from running - legacy_constructor - variable_name - legacy_cggeometry_functions - legacy_constant - todo - trailing_newline - empty_count - force_cast - type_name - function_body_length - missing_docs - conditional_binding_cascade - valid_docs - cyclomatic_complexity - type_body_length - function_parameter_count - force_try - control_statement - trailing_whitespace - leading_whitespace - operator_whitespace - file_length - mark opt_in_rules: # some rules are only opt-in - closing_brace - opening_brace - return_arrow_whitespace - trailing_semicolon - statement_position # Find all the available rules by running: # swiftlint rules included: # paths to include during linting. `--path` is ignored if present. excluded: # paths to ignore during linting. Takes precedence over `included`. - Carthage - Pods - Source/ExcludedFolder - Source/ExcludedFile.swift - ThirdParty - FxA - FxAClient - build # configurable rules can be customized from this configuration file # binary rules can set their severity level trailing_semicolon: error empty_count: error closing_brace: error opening_brace: error return_arrow_whitespace: error statement_position: error colon: error comma: error line_length: 1000 reporter: "json" # reporter type (xcode, json, csv, checkstyle) 


レルム
 included: - Realm/ObjectServerTests - RealmSwift - Realm/Swift variable_name: min_length: # not possible to disable this partial rule, so set it to zero warning: 0 error: 0 disabled_rules: - file_length - force_cast - force_try - function_body_length - todo - type_body_length - line_length - vertical_whitespace - syntactic_sugar 


ティンコフ
warning_threshold:15

除外:
-ポッド
-MBUITest

disabled_rules:
-trailing_whitespace

line_length:
警告:150

function_parameter_count:
警告:10
エラー:15

file_length:
警告:500

type_body_length:
警告:400
エラー:450

SwiftLintにより、ios開発チームはコードレビューに費やす時間を削減し、単一のコードスタイルに固執し、コード品質を向上させることができました。 プロジェクトでまだSwiftLintを使用していない場合は、必ず試してみてください。

便利なリンク:

» SwiftLintリポジトリ
» SwiftLintルールリスト
» 変更されたファイルのみのチェックについて
» GitHubのSwiftスタイルガイド
» テーラー-SwiftLintの代替

Source: https://habr.com/ru/post/J317892/


All Articles