Class: Rubocop::Cop::Style::AccessModifierIndentation

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

Overview

A couple of checks related to the use method visibility modifiers. Modifiers should be indented as deeps are method definitions and surrounded by blank lines.

Constant Summary collapse

MSG =
'%s access modifiers like %s.'
PRIVATE_NODE =
s(:send, nil, :private)
PROTECTED_NODE =
s(:send, nil, :protected)
PUBLIC_NODE =
s(:send, nil, :public)

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#investigate(processed_source) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/style/access_modifier_indentation.rb', line 16

def investigate(processed_source)
  ast = processed_source.ast
  return unless ast
  on_node([:class, :module, :sclass, :block], ast) do |class_node|
    if class_node.type == :block && !class_constructor?(class_node)
      next
    end

    class_start_col = class_node.loc.expression.column

    # we'll have to walk all class children nodes
    # except other class/module nodes
    class_node.children.compact.each do |node|
      on_node(:send, node, [:class, :module, :sclass]) do |send_node|
        if modifier_node?(send_node)
          send_start_col = send_node.loc.expression.column

          if send_start_col != class_start_col + expected_indent_offset
            convention(send_node, :expression)
          end
        end
      end
    end
  end
end