Class: Linecounter::BranchAnalyzer::CountingVisitor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCountingVisitor

Returns a new instance of CountingVisitor.



27
28
29
30
# File 'lib/linecounter/branch_analyzer.rb', line 27

def initialize
  super
  @counts = Hash.new(0)
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



25
26
27
# File 'lib/linecounter/branch_analyzer.rb', line 25

def counts
  @counts
end

Instance Method Details

#visit_and_node(node) ⇒ Object



94
95
96
97
# File 'lib/linecounter/branch_analyzer.rb', line 94

def visit_and_node(node)
  @counts[:and] += 1
  super
end

#visit_begin_node(node) ⇒ Object



87
88
89
90
91
92
# File 'lib/linecounter/branch_analyzer.rb', line 87

def visit_begin_node(node)
  # Only explicit `begin ... end`; method/block-level rescue produces an
  # implicit BeginNode (no begin keyword) that we don't count.
  @counts[:begin] += 1 if node.begin_keyword_loc
  super
end

#visit_case_match_node(node) ⇒ Object



52
53
54
55
# File 'lib/linecounter/branch_analyzer.rb', line 52

def visit_case_match_node(node)
  @counts[:case] += 1
  super
end

#visit_case_node(node) ⇒ Object



47
48
49
50
# File 'lib/linecounter/branch_analyzer.rb', line 47

def visit_case_node(node)
  @counts[:case] += 1
  super
end

#visit_ensure_node(node) ⇒ Object



82
83
84
85
# File 'lib/linecounter/branch_analyzer.rb', line 82

def visit_ensure_node(node)
  @counts[:ensure] += 1
  super
end

#visit_if_node(node) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/linecounter/branch_analyzer.rb', line 32

def visit_if_node(node)
  key =
    if node.if_keyword_loc.nil? then :ternary
    elsif node.if_keyword_loc.slice == "elsif" then :elsif
    else :if
    end
  @counts[key] += 1
  super
end

#visit_in_node(node) ⇒ Object



62
63
64
65
# File 'lib/linecounter/branch_analyzer.rb', line 62

def visit_in_node(node)
  @counts[:when] += 1
  super
end

#visit_or_node(node) ⇒ Object



99
100
101
102
# File 'lib/linecounter/branch_analyzer.rb', line 99

def visit_or_node(node)
  @counts[:or] += 1
  super
end

#visit_rescue_node(node) ⇒ Object



77
78
79
80
# File 'lib/linecounter/branch_analyzer.rb', line 77

def visit_rescue_node(node)
  @counts[:rescue] += 1
  super
end

#visit_return_node(node) ⇒ Object



104
105
106
107
# File 'lib/linecounter/branch_analyzer.rb', line 104

def visit_return_node(node)
  @counts[:return] += 1
  super
end

#visit_unless_node(node) ⇒ Object



42
43
44
45
# File 'lib/linecounter/branch_analyzer.rb', line 42

def visit_unless_node(node)
  @counts[:unless] += 1
  super
end

#visit_until_node(node) ⇒ Object



72
73
74
75
# File 'lib/linecounter/branch_analyzer.rb', line 72

def visit_until_node(node)
  @counts[:until] += 1
  super
end

#visit_when_node(node) ⇒ Object



57
58
59
60
# File 'lib/linecounter/branch_analyzer.rb', line 57

def visit_when_node(node)
  @counts[:when] += 1
  super
end

#visit_while_node(node) ⇒ Object



67
68
69
70
# File 'lib/linecounter/branch_analyzer.rb', line 67

def visit_while_node(node)
  @counts[:while] += 1
  super
end