Module: RuboCop::Cop::AccessModifierNode

Extended by:
Sexp
Included in:
Style::AccessModifierIndentation, Style::EmptyLinesAroundAccessModifier, Style::IndentationConsistency, Style::IndentationWidth
Defined in:
lib/rubocop/cop/mixin/access_modifier_node.rb

Overview

Common functionality for checking modifier nodes.

Constant Summary collapse

PRIVATE_NODE =
s(:send, nil, :private)
PROTECTED_NODE =
s(:send, nil, :protected)
PUBLIC_NODE =
s(:send, nil, :public)
MODULE_FUNCTION_NODE =
s(:send, nil, :module_function)

Instance Method Summary collapse

Methods included from Sexp

s

Instance Method Details

#class_or_module_parent?(node) ⇒ Boolean

Returns true when the parent of what looks like an access modifier is a Class or Module. Filters out simple method calls to similarly named private, protected or public.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/mixin/access_modifier_node.rb', line 31

def class_or_module_parent?(node)
  node.each_ancestor do |a|
    if a.type == :block
      return true if a.class_constructor?
    elsif a.type != :begin
      return [:casgn, :sclass, :class, :module].include?(a.type)
    end
  end
end

#modifier_node?(node) ⇒ Boolean

Returns true when the node is an access modifier.

Returns:

  • (Boolean)


16
17
18
# File 'lib/rubocop/cop/mixin/access_modifier_node.rb', line 16

def modifier_node?(node)
  modifier_structure?(node) && class_or_module_parent?(node)
end

#modifier_structure?(node) ⇒ Boolean

Returns true when the node looks like an access modifier.

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/rubocop/cop/mixin/access_modifier_node.rb', line 21

def modifier_structure?(node)
  [PRIVATE_NODE,
   PROTECTED_NODE,
   PUBLIC_NODE,
   MODULE_FUNCTION_NODE].include?(node)
end