Module: RuboCop::Cop::AccessModifierNode

Extended by:
AST::Sexp
Included in:
Lint::UselessAccessModifier, 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

Instance Method Details

#class_constructor?(block_node) ⇒ Boolean

Returns true when the block node looks like Class or Module.new do ... .

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/rubocop/cop/mixin/access_modifier_node.rb', line 41

def class_constructor?(block_node)
  send_node = block_node.children.first
  receiver_node, method_name, *_ = *send_node
  return false unless method_name == :new
  %w(Class Module).include?(Util.const_name(receiver_node))
end

#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 |a|
    if a.type == :block
      return true if class_constructor?(a)
    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)


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,
   PROTECTED_NODE,
   PUBLIC_NODE,
   MODULE_FUNCTION_NODE].include?(node)
end