Class: Rubocop::Cop::Style::CaseIndentation

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/case_indentation.rb

Overview

This cop checks whether the when*s of a *case expression are indented as deep as its case keyword.

It will register a separate offence for each misaligned when.

Constant Summary collapse

MSG =
'Indent when as deep as case.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_case(case_node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/style/case_indentation.rb', line 13

def on_case(case_node)
  _condition, *whens, _else = *case_node

  case_column = case_node.location.keyword.column

  whens.each do |when_node|
    pos = when_node.loc.keyword
    add_offence(:convention, pos, MSG) if pos.column != case_column
  end
end