Class: Linecounter::StructureAnalyzer::StructureVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/linecounter/structure_analyzer.rb

Overview

Walks the AST tracking visibility per class/module scope, so each method, macro, and constant is attributed accurately at the structure level only (declarations inside method bodies are not miscounted).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStructureVisitor

Returns a new instance of StructureVisitor.



87
88
89
90
91
92
93
94
95
96
# File 'lib/linecounter/structure_analyzer.rb', line 87

def initialize
  super
  @type_counts = Hash.new(0)
  @item_counts = Hash.new(0)
  @item_loc_sums = Hash.new(0)
  @visibility = [:public]
  @method_depth = 0
  @singleton_depth = 0
  @forced_visibility = nil
end

Instance Attribute Details

#item_countsObject (readonly)

Returns the value of attribute item_counts.



85
86
87
# File 'lib/linecounter/structure_analyzer.rb', line 85

def item_counts
  @item_counts
end

#item_loc_sumsObject (readonly)

Returns the value of attribute item_loc_sums.



85
86
87
# File 'lib/linecounter/structure_analyzer.rb', line 85

def item_loc_sums
  @item_loc_sums
end

#type_countsObject (readonly)

Returns the value of attribute type_counts.



85
86
87
# File 'lib/linecounter/structure_analyzer.rb', line 85

def type_counts
  @type_counts
end

Instance Method Details

#visit_call_node(node) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/linecounter/structure_analyzer.rb', line 129

def visit_call_node(node)
  return super unless node.receiver.nil?

  name = node.name
  if VISIBILITY_NAMES.include?(name)
    handle_visibility(node, name)
  else
    record(macro_key(name), node) if @method_depth.zero?
    super
  end
end

#visit_class_node(node) ⇒ Object



98
99
100
# File 'lib/linecounter/structure_analyzer.rb', line 98

def visit_class_node(node)
  with_scope { super }
end

#visit_constant_path_write_node(node) ⇒ Object



124
125
126
127
# File 'lib/linecounter/structure_analyzer.rb', line 124

def visit_constant_path_write_node(node)
  record(:constant_assignment, node) if @method_depth.zero?
  super
end

#visit_constant_write_node(node) ⇒ Object



119
120
121
122
# File 'lib/linecounter/structure_analyzer.rb', line 119

def visit_constant_write_node(node)
  record(:constant_assignment, node) if @method_depth.zero?
  super
end

#visit_def_node(node) ⇒ Object



112
113
114
115
116
117
# File 'lib/linecounter/structure_analyzer.rb', line 112

def visit_def_node(node)
  classify_def(node) if @method_depth.zero?
  @method_depth += 1
  super
  @method_depth -= 1
end

#visit_module_node(node) ⇒ Object



102
103
104
# File 'lib/linecounter/structure_analyzer.rb', line 102

def visit_module_node(node)
  with_scope { super }
end

#visit_singleton_class_node(node) ⇒ Object



106
107
108
109
110
# File 'lib/linecounter/structure_analyzer.rb', line 106

def visit_singleton_class_node(node)
  @singleton_depth += 1
  with_scope { super }
  @singleton_depth -= 1
end