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

Inherits:
Cop
  • Object
show all
Includes:
ConfigurableEnforcedStyle
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

Constants included from Util

Util::PROC_NEW_NODE

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #both_styles_detected, #correct_style_detected, #opposite_style_detected, #style, #unrecognized_style_detected

Methods inherited from Cop

#add_offence, all, #autocorrect?, #config_to_allow_offences, #config_to_allow_offences=, #cop_config, cop_name, #cop_name, cop_type, #debug?, #exclude_file?, #exclude_paths, #ignore_node, #include_file?, #include_paths, inherited, #initialize, lint?, non_rails, rails?, #relevant_file?, style?, #support_autocorrect?

Methods included from Util

block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, on_node, proc?, range_with_surrounding_space, source_range, strip_quotes

Constructor Details

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

Class Method Details

.modifier_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubocop/cop/style/access_modifier_indentation.rb', line 40

def self.modifier_node?(node)
  [PRIVATE_NODE, PROTECTED_NODE, PUBLIC_NODE].include?(node)
end

Instance Method Details

#investigate(processed_source) ⇒ Object



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/style/access_modifier_indentation.rb', line 18

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 self.class.modifier_node?(send_node)
          check(send_node, class_start_col)
        end
      end
    end
  end
end