eclintで既存ファイルからのEditorConfig作成&既存ファイルへのEditorConfig設定適用をしてみた

共同編集時にソースコードのフォーマットを維持したい場合欠かせないEditorConfigの、既存コードへの適用及び既存コードから設定を書き出すeclintについて使い方を書いてみました。
2020.07.13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

Pull-Requestにてファイル末に改行指定がないことを指摘され、IntelliJにはEditorConfigを用いた空行自動挿入の仕組みがあることを知ったものの、意図した通りには動いていないように見えました。

CommandLine-Toolや関連リポジトリを辿った結果、動作で腑に落ちない点が解消できたので、関連ライブラリ交えて使い方をまとめました。

EditorConfigの導入

IntelliJ等、仕組みとして備わっているエディタについては特に気にする必要もありません。EclipseやVim、Visual Studio Codeについてはプラグインの追加が必要です。

EditorConfigの使い方

既存ファイルに対してフォーマット整形する類のものではありません。要するに新規作成したファイルに対して自動で設定が行われます。

Existing files are not reformatted by the plugin (only newly input lines are formatted in the format given in the .editorconfig files) but if you would like to use a separate tool to do this then see the next question.

既存ファイルにEditorConfigのチェックを通す

eclintを利用します。

% npm install -g eclint
/path/to/.anyenv/envs/nodenv/versions/10.15.3/bin/eclint -> /path/to/.anyenv/envs/nodenv/versions/10.15.3/lib/node_modules/eclint/bin/eclint.js
+ eclint@2.8.1
added 232 packages from 387 contributors in 10.999s

.editorconfigが存在する場合は適用された上でチェックされ、更にコマンドラインでオプションとして追加指定した場合は指定で上書きされた状態でチェックされます。

% eclint check --help
eclint check [globs...]

Validate that file(s) adhere to .editorconfig settings

  オプション:
  --help                          ヘルプを表示                            [真偽]
  --version                       バージョンを表示                        [真偽]
  --indent_style, -i              Indentation Style
                                   [選択してください: "tab", "space", undefined]
  --indent_size, -s               Indentation Size (in single-spaced characters)
                                                                          [数値]
  --tab_width, -t                 Width of a single tabstop character     [数値]
  --end_of_line, -e               Line ending file format (Unix, DOS, Mac)
                               [選択してください: "lf", "crlf", "cr", undefined]
  --charset, -c                   File character encoding
      [選択してください: "latin1", "utf-8", "utf-8-bom", "utf-16le", "utf-16be",
                                                                      undefined]
  --trim_trailing_whitespace, -w  Denotes whether whitespace is allowed at the
                                  end of lines                            [真偽]
  --insert_final_newline, -n      Denotes whether file should end with a newline
                                                                          [真偽]
  --max_line_length, -m           Forces hard line wrapping after the amount of
                                  characters specified                    [数値]
  --block_comment_start           Block comments start with             [文字列]
  --block_comment                 Lines in block comment start with     [文字列]
  --block_comment_end             Block comments end with               [文字列]

1ファイル指定の場合は以下の指定になります。

% eclint check path/to/file
path/to/file
    26:120 ❌ invalid line length: 122, exceeds: 120

複数ファイルを同時に行う場合はアスタリスクを利用します。

% eclint check */**/*
path/to/file1
    26:120 ❌ invalid line length: 122, exceeds: 120
path/to/file2
    26:120 ❌ invalid line length: 122, exceeds: 120

チェックした上で修正も入れたい場合にはeclint fixを実施します。指定可能なオプションはeclint checkとほぼ変わりません。

注意事項としてはドキュメントにもある通り、修正される内容を十分に確認の上で行ってください。

Warning! Fixing your files will change their contents. Ensure that your files are under version control and that you have committed your changes before attempting to fix any issues with them. You can also run the check command to know which files will change before you fix them.

既存ファイルを元に設定を出力する

作成済みのソースコードを元に設定ファイル出力も可能です。複数の設定が入り混じっている場合は、スコアとしての確認も可能です。

% eclint infer --score path/to/file | jq -r '.'
{
  "charset": {
    "": 1
  },
  "indent_style": {
    "space": 1
  },
  "indent_size": {
    "2": 1
  },
  "trim_trailing_whitespace": {
    "true": 15
  },
  "end_of_line": {
    "lf": 15
  },
  "insert_final_newline": {
    "true": 1
  },
  "max_line_length": 51
}

ini形式での設定確認

% eclint infer --ini path/to/file
# EditorConfig is awesome: http://EditorConfig.org
[*]
charset =
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = true
max_line_length = 60

設定ファイルとして出力する

% eclint infer --ini path/to/file > .editorconfig

あとがき

.editorconfigを作ったものの、保存しても既存ファイルに適用された様子がなく、何が原因かを調べる中で見つけたライブラリでした。

差分確認は必須ですが、すでに存在するファイルへの一括適用等も簡単に行えるため、後から設定した場合にはおすすめです。

参考リンク