Class: RBS::Prototype::NodeUsage

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rbs/prototype/node_usage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NodeUsage

Returns a new instance of NodeUsage.



10
11
12
13
14
15
# File 'lib/rbs/prototype/node_usage.rb', line 10

def initialize(node)
  @node = node
  @conditional_nodes = Set[].compare_by_identity

  calculate(node, conditional: false)
end

Instance Attribute Details

#conditional_nodesObject (readonly)

Returns the value of attribute conditional_nodes.



8
9
10
# File 'lib/rbs/prototype/node_usage.rb', line 8

def conditional_nodes
  @conditional_nodes
end

Instance Method Details

#calculate(node, conditional:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbs/prototype/node_usage.rb', line 25

def calculate(node, conditional:)
  if conditional
    conditional_nodes << node
  end

  case node.type
  when :IF, :UNLESS
    cond_node, true_node, false_node = node.children
    calculate(cond_node, conditional: true)
    calculate(true_node, conditional: conditional) if true_node
    calculate(false_node, conditional: conditional) if false_node
  when :AND, :OR
    left, right = node.children
    calculate(left, conditional: true)
    calculate(right, conditional: conditional)
  when :QCALL
    receiver, _, args = node.children
    calculate(receiver, conditional: true)
    calculate(args, conditional: false) if args
  when :WHILE
    cond, body = node.children
    calculate(cond, conditional: true)
    calculate(body, conditional: false) if body
  when :OP_ASGN_OR, :OP_ASGN_AND
    var, _, asgn = node.children
    calculate(var, conditional: true)
    calculate(asgn, conditional: conditional)
  when :LASGN, :IASGN, :GASGN
    _, lhs = node.children
    calculate(lhs, conditional: conditional) if lhs
  when :MASGN
    lhs, _ = node.children
    calculate(lhs, conditional: conditional)
  when :CDECL
    if node.children.size == 2
      _, lhs = node.children
      calculate(lhs, conditional: conditional)
    else
      const, _, lhs = node.children
      calculate(const, conditional: false)
      calculate(lhs, conditional: conditional)
    end
  when :SCOPE
    _, _, body = node.children
    calculate(body, conditional: conditional)
  when :CASE2
    _, *branches = node.children
    branches.each do |branch|
      if branch.type == :WHEN
        list, body = branch.children
        list.children.each do |child|
          if child
            calculate(child, conditional: true)
          end
        end
        calculate(body, conditional: conditional)
      else
        calculate(branch, conditional: conditional)
      end
    end
  when :BLOCK
    *nodes, last = node.children
    nodes.each do |no|
      calculate(no, conditional: false)
    end
    calculate(last, conditional: conditional) if last
  else
    each_child(node) do |child|
      calculate(child, conditional: false)
    end
  end
end

#each_conditional_node(&block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rbs/prototype/node_usage.rb', line 17

def each_conditional_node(&block)
  if block
    conditional_nodes.each(&block)
  else
    conditional_nodes.each
  end
end