Class: Reek::Context::ModuleContext

Inherits:
CodeContext show all
Defined in:
lib/reek/context/module_context.rb

Overview

A context wrapper for any module found in a syntax tree.

Direct Known Subclasses

ClassContext, GhostContext, RefinementContext

Constant Summary collapse

CONSTANT_SEXP_TYPES =
[:casgn, :class, :module].freeze

Instance Attribute Summary collapse

Attributes inherited from CodeContext

#children, #exp, #parent, #refs, #statement_counter

Instance Method Summary collapse

Methods inherited from CodeContext

#apply_current_visibility, #config_for, #configuration_via_code_commment, #each, #full_comment, #full_name, #instance_method?, #local_nodes, #matches?, #number_of_statements, #parent_config_for, #record_call_to, #record_use_of_self, #register_with_parent, #singleton_method?

Constructor Details

#initialize(exp) ⇒ ModuleContext

Returns a new instance of ModuleContext.



16
17
18
19
# File 'lib/reek/context/module_context.rb', line 16

def initialize(exp)
  super
  @visibility_tracker = VisibilityTracker.new
end

Instance Attribute Details

#visibility_trackerObject (readonly)

Returns the value of attribute visibility_tracker.



14
15
16
# File 'lib/reek/context/module_context.rb', line 14

def visibility_tracker
  @visibility_tracker
end

Instance Method Details

#append_child_context(child) ⇒ Object

Register a child context. The child’s parent context should be equal to the current context.

This makes the current context responsible for setting the child’s visibility.

Parameters:

  • child (CodeContext)

    the child context to register



28
29
30
31
# File 'lib/reek/context/module_context.rb', line 28

def append_child_context(child)
  visibility_tracker.apply_visibility(child)
  super
end

#attribute_context_classObject

Return the correct class for child attribute contexts. For ModuleContext, this is the class that represents instance attributes.



42
43
44
# File 'lib/reek/context/module_context.rb', line 42

def attribute_context_class
  AttributeContext
end

#defined_instance_methods(visibility: :any) ⇒ Object



46
47
48
49
50
# File 'lib/reek/context/module_context.rb', line 46

def defined_instance_methods(visibility: :any)
  return instance_method_children if visibility == :any

  instance_method_children.select { |child| child.visibility == visibility }
end

#descriptively_commented?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/reek/context/module_context.rb', line 69

def descriptively_commented?
  CodeComment.new(comment: exp.leading_comment).descriptive?
end

#instance_method_callsObject



52
53
54
55
56
# File 'lib/reek/context/module_context.rb', line 52

def instance_method_calls
  instance_method_children.flat_map do |context|
    context.children.grep(SendContext)
  end
end

#instance_method_childrenObject (private)



101
102
103
# File 'lib/reek/context/module_context.rb', line 101

def instance_method_children
  children.select(&:instance_method?)
end

#instance_method_names_via_to_callObject



58
59
60
# File 'lib/reek/context/module_context.rb', line 58

def instance_method_names_via_to_call
  instance_method_calls.flat_map(&:method_name_called_to_call).compact
end

#method_context_classObject

Return the correct class for child method contexts (representing nodes of type ‘:def`). For ModuleContext, this is the class that represents instance methods.



36
37
38
# File 'lib/reek/context/module_context.rb', line 36

def method_context_class
  MethodContext
end

#namespace_module?Boolean

A namespace module is a module (or class) that is only there for namespacing purposes, and thus contains only nested constants, modules or classes.

However, if the module is empty, it is not considered a namespace module.

Returns:

  • (Boolean)

    true if the module is a namespace module



83
84
85
86
87
88
# File 'lib/reek/context/module_context.rb', line 83

def namespace_module?
  return false if exp.type == :casgn

  children = exp.direct_children
  children.any? && children.all? { |child| CONSTANT_SEXP_TYPES.include? child.type }
end

#node_instance_methodsObject

Deprecated.

use ‘defined_instance_methods` instead



65
66
67
# File 'lib/reek/context/module_context.rb', line 65

def node_instance_methods
  local_nodes(:def).to_a
end

#singleton_method_childrenObject (private)



105
106
107
# File 'lib/reek/context/module_context.rb', line 105

def singleton_method_children
  children.select(&:singleton_method?)
end

#track_visibility(visibility, names) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/reek/context/module_context.rb', line 90

def track_visibility(visibility, names)
  visibility_tracker.track_visibility children: instance_method_children,
                                      visibility: visibility,
                                      names: names
  visibility_tracker.track_singleton_visibility children: singleton_method_children,
                                                visibility: visibility,
                                                names: names
end