Class: RBS::Prototype::NodeUsage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#any_node?, #args_from_node, #block_from_body, #each_child, #each_node, #keyword_hash?, #parse_comments, #symbol_literal_node?, #untyped

Constructor Details

#initialize(node) ⇒ NodeUsage

Returns a new instance of NodeUsage.

Parameters:



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_nodesSet[node] (readonly)

Returns the value of attribute conditional_nodes.

Returns:



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

def conditional_nodes
  @conditional_nodes
end

#nodenode (readonly)

Returns the value of attribute node.

Returns:



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

def node
  @node
end

Instance Method Details

#calculate(node, conditional:) ⇒ void

This method returns an undefined value.

Parameters:

  • (node)
  • conditional: (Boolean)


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_nodevoid #each_conditional_nodeEnumerator[node, void]

Overloads:

  • #each_conditional_nodevoid

    This method returns an undefined value.

  • #each_conditional_nodeEnumerator[node, void]

    Returns:

    • (Enumerator[node, void])

Yields:

Yield Parameters:

Yield Returns:

  • (void)


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