Module: RuboCop::Cop::AccessModifierNode

Overview

Common functionality for checking modifier nodes.

Instance Method Summary collapse

Methods included from NodePattern::Macros

def_node_matcher, def_node_search, node_search, node_search_all, node_search_body, node_search_first

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)


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

def class_or_module_parent?(node)
  node.each_ancestor do |ancestor|
    if ancestor.block_type?
      return true if ancestor.class_constructor?
    elsif !ancestor.begin_type?
      return [:casgn, :sclass, :class, :module].include?(ancestor.type)
    end
  end
end

#modifier_node?(node) ⇒ Boolean

Returns true when the node is an access modifier.

Returns:

  • (Boolean)


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

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)


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

def modifier_structure?(node)
  private_node?(node) ||
    protected_node?(node) ||
    public_node?(node) ||
    module_function_node?(node)
end