5
4

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.

RuboCop | Style/TrailingBlankLines

Posted at

RuboCop | Style/TrailingBlankLines

概要

RuboCopの「Style/TrailingBlankLines」警告について。

ファイルの末尾の行に余分な空白があるかチェックをする。
デフォルトでは末尾に1行の空行がある状態を正常とします。

TrailingBlankLines

設定値一覧

設定対象 対象 内容 デフォルト
EnforcedStyle final_newline 最後に新たな行を必要とするか
EnforcedStyle final_blank_line 最後に空白行がある場合もエラーとする --

検証プログラム

※Markdown変換時に空行が消えてしまうため、本来はコード中にはない全角空白を追加してあります

trailing_blank_lines1.rb
print 'hoge'
 
trailing_blank_lines2.rb
print 'hoge'
 
 
trailing_blank_lines3.rb
print 'hoge'

実行結果 デフォルト の場合

.rubocop.yml
TrailingBlankLines:
  EnforcedStyle: final_newline
$ rubocop trailing_blank_lines1.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

$ rubocop trailing_blank_lines2.rb
Inspecting 1 file
C

Offenses:

trailing_blank_lines2.rb:2:1: C: 1 trailing blank lines detected.

1 file inspected, 1 offense detected

$ rubocop trailing_blank_lines3.rb
Inspecting 1 file
C

Offenses:

trailing_blank_lines3.rb:1:13: C: Final newline missing.
print 'hoge'


1 file inspected, 1 offense detected

実行結果 final_blank_line に設定した場合

.rubocop.yml
TrailingBlankLines:
  EnforcedStyle: final_blank_line
$ rubocop trailing_blank_lines1.rb
Inspecting 1 file
C

Offenses:

trailing_blank_lines1.rb:2:1: C: Trailing blank line missing.

1 file inspected, 1 offense detected
$ rubocop trailing_blank_lines2.rb
Inspecting 1 file
.

1 file inspected, no offenses detected
$ rubocop trailing_blank_lines3.rb
Inspecting 1 file
C

Offenses:

trailing_blank_lines3.rb:1:13: C: Final newline missing.
print 'hoge'


1 file inspected, 1 offense detected

対象コード

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/trailing_blank_lines.rb
https://github.com/bbatsov/rubocop/blob/master/spec/rubocop/cop/style/trailing_blank_lines_spec.rb

補足

この警告は rubocop -a で修正可能です。

RuboCopまとめ記事

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?