7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftlintのline_lengthで引っかかって解決したので共有します。

Posted at

Swiftlintのline_lengthが複数行になったときの指定方法で引っかかったので、
どのように解決したかやその経緯を共有します。
line_lengthに限らず、Swiftlintの記述方法全般に共通する内容だと思います。

結論

まずは解決した方法を紹介します。
以下の記述でline_lengthの文字数を指定、かつコメントアウト部分は指定外になります。

.swiftlint.yml
line_length:
  warning: 180
  error: 350
  ignores_comments: true

180~349文字でwarning、350文字以上でerrorが発生し、コメントアウトは指定から除外されます。

導入

Swiftlintのインストール方法などについては省略します。
.swiftlint.ymlファイルの初期状態はSwiftlintのConfiguration以下の記述をほぼコピペしたものです。

.swiftlint.yml
disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma

// ...
// 長いので省略
// ...

reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube)

引っかかった記述方法

line_lengthのコピペ時の初期状態は以下です。

.swiftlint.yml
line_length: 110

この場合、1行に110以上の文字が記述されているとwarningが発生します。
問題は、コメントアウトした部分にもwarningが発生するためline_length以下に
ignores_comments: trueを記述しようと考えました。

(ついでに許容する文字数も110では少ないので180に変更します。)

:fist:以下、ダメだった記述例

.swiftlint.yml
line_length: 180
  ignores_comments: true
.swiftlint.yml
line_length:
 - 180
 - ignores_comments: true

:smile: 改めて、正しい記述例↓

.swiftlint.yml
line_length:
  warning: 180
  ignores_comments: true

まとめ

line_lengthの条件が複数行になる場合、warning:error:の付加が必要になります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?