Class: Rubocop::Cop::Style::AccessControl

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/access_control.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

INDENT_MSG =
'Indent %s as deep as method definitions.'
BLANK_MSG =
'Keep a blank line before and after %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?, #autocorrect_action, #convention, #cop_config, #cop_name, cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, rails?, style?, #warning

Constructor Details

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

Instance Method Details

#investigate(processed_source) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/style/access_control.rb', line 17

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
          selector = send_node.loc.selector.source

          if send_start_col - 2 != class_start_col
            convention(send_node, :expression,
                       format(INDENT_MSG, selector))
          end

          send_line = send_node.loc.line

          unless processed_source[send_line].chomp.empty? &&
              processed_source[send_line - 2].chomp.empty?
            convention(send_node, :expression,
                       format(BLANK_MSG, selector))
          end
        end
      end
    end
  end
end