Class: Rubocop::Cop::AccessControl

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

Constant Summary collapse

INDENT_MSG =
'Indent private and protected as deep as method defs.'
BLANK_MSG =
'Keep a blank line before and after private/protected.'
PRIVATE_NODE =
s(:send, nil, :private)
PROTECTED_NODE =
s(:send, nil, :protected)

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #name, #on_comment

Constructor Details

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

Instance Method Details

#inspect(source, tokens, ast, comments) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/access_control.rb', line 12

def inspect(source, tokens, ast, comments)
  on_node([:class, :module, :sclass], ast) do |class_node|
    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 [PRIVATE_NODE, PROTECTED_NODE].include?(send_node)
          send_start_col = send_node.loc.expression.column

          if send_start_col - 2 != class_start_col
            add_offence(:convention,
                        send_node.loc.line,
                        INDENT_MSG)
          end

          send_line = send_node.loc.line

          unless source[send_line].empty? && source[send_line - 2].empty?
            add_offence(:convention, send_line, BLANK_MSG)
          end
        end
      end
    end
  end
end