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/CaseIndentation Indent One Step

Posted at

RuboCop | Style/CaseIndentation Indent One Step

概要

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

when のインデントを case と end どちらに合わせるかを設定します。

設定値一覧

設定対象 設定値 内容 デフォルト
Indent One Step false whenをcaseかendに揃える。(どちらに揃えるかはSupportedStyles次第)
Indent One Step true whenをcaseかendより1段階インデントする(どちらに揃えるかはSupportedStyles次第) --

CaseIndentation Indent One Step

各設定値(false, true)での検証結果をまとめます。

検証プログラム

indent_one_step.rb

i = 1
rets = case i
       when 1
         print '1'
       when 2
         print '2'
       when 3..4
         print '3..4'
       end

print rets

rets = case i
       when 1
         print '1'
       when 2
         print '2'
       when 3..4
         print '3..4'
end

print rets

rets = case i
         when 1
           print '1'
         when 2
           print '2'
         when 3..4
           print '3..4'
       end

print rets

rets = case i
         when 1
           print '1'
         when 2
           print '2'
         when 3..4
           print '3..4'
end

print rets

実行結果 case の場合

.rubocop.yml

※明示的に設定しているが、デフォルト値なので何も設定しなくてもよい

CaseIndentation:
  IndentWhenRelativeTo: 'case'
  IndentOneStep: false

whenがcaseと揃っていない場合に警告が出ます。

$ rubocop indent_one_step.rb
Inspecting 1 file
C

Offenses:

indent_one_step.rb:25:10: C: Indent when as deep as case.
         when 1
         ^^^^
indent_one_step.rb:27:10: C: Indent when as deep as case.
         when 2
         ^^^^
indent_one_step.rb:29:10: C: Indent when as deep as case.
         when 3..4
         ^^^^
indent_one_step.rb:36:10: C: Indent when as deep as case.
         when 1
         ^^^^
indent_one_step.rb:38:10: C: Indent when as deep as case.
         when 2
         ^^^^
indent_one_step.rb:40:10: C: Indent when as deep as case.
         when 3..4
         ^^^^

1 file inspected, 6 offenses detected

実行結果 end の場合

.rubocop.yml

CaseIndentation:
  IndentWhenRelativeTo: 'case'
  IndentOneStep: true

whenがcaseから一段階インデントされていない場合に警告が出ます。

$ rubocop indent_one_step.rb
Inspecting 1 file
C

Offenses:

indent_one_step.rb:3:8: C: Indent when one step more than case.
       when 1
       ^^^^
indent_one_step.rb:5:8: C: Indent when one step more than case.
       when 2
       ^^^^
indent_one_step.rb:7:8: C: Indent when one step more than case.
       when 3..4
       ^^^^
indent_one_step.rb:14:8: C: Indent when one step more than case.
       when 1
       ^^^^
indent_one_step.rb:16:8: C: Indent when one step more than case.
       when 2
       ^^^^
indent_one_step.rb:18:8: C: Indent when one step more than case.
       when 3..4
       ^^^^

1 file inspected, 6 offenses detected
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?