Module: Steep::NodeHelper

Instance Method Summary collapse

Instance Method Details

#clone_node(node) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/steep/node_helper.rb', line 261

def clone_node(node)
  children = node.children.map do |child|
    if child.is_a?(Parser::AST::Node)
      clone_node(child)
    else
      child.dup
    end
  end

  node.updated(nil, children)
end

#deconstruct_case_node(node) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/steep/node_helper.rb', line 97

def deconstruct_case_node(node)
  case node.type
  when :case
    cond, *whens, else_ = node.children
    [
      cond,
      whens,
      else_,
      _ = node.loc
    ]
  end
end

#deconstruct_case_node!(node) ⇒ Object



110
111
112
# File 'lib/steep/node_helper.rb', line 110

def deconstruct_case_node!(node)
  deconstruct_case_node(node) or raise
end

#deconstruct_if_node(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/steep/node_helper.rb', line 51

def deconstruct_if_node(node)
  if node.type == :if
    [
      node.children[0],
      node.children[1],
      node.children[2],
      _ = node.location
    ]
  end
end

#deconstruct_if_node!(node) ⇒ Object



62
63
64
# File 'lib/steep/node_helper.rb', line 62

def deconstruct_if_node!(node)
  deconstruct_if_node(node) or raise
end

#deconstruct_resbody_node(node) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/steep/node_helper.rb', line 172

def deconstruct_resbody_node(node)
  case node.type
  when :resbody
    [
      node.children[0],
      node.children[1],
      node.children[2],
      _  = node.loc
    ]
  end
end

#deconstruct_resbody_node!(node) ⇒ Object



184
185
186
# File 'lib/steep/node_helper.rb', line 184

def deconstruct_resbody_node!(node)
  deconstruct_resbody_node(node) or raise
end

#deconstruct_rescue_node(node) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/steep/node_helper.rb', line 146

def deconstruct_rescue_node(node)
  case node.type
  when :rescue
    body, *resbodies, else_ = node.children

    [
      body,
      resbodies,
      else_,
      _ = node.loc
    ]
  end
end

#deconstruct_rescue_node!(node) ⇒ Object



160
161
162
# File 'lib/steep/node_helper.rb', line 160

def deconstruct_rescue_node!(node)
  deconstruct_rescue_node(node) or raise
end

#deconstruct_send_node(node) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/steep/node_helper.rb', line 196

def deconstruct_send_node(node)
  case node.type
  when :send, :csend
    receiver, selector, *args = node.children
    [
      receiver,
      selector,
      args,
      _  = node.loc
    ]
  end
end

#deconstruct_send_node!(node) ⇒ Object



209
210
211
# File 'lib/steep/node_helper.rb', line 209

def deconstruct_send_node!(node)
  deconstruct_send_node(node) or raise(node.inspect)
end

#deconstruct_sendish_and_block_nodes(*nodes) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/steep/node_helper.rb', line 238

def deconstruct_sendish_and_block_nodes(*nodes)
  send_node, block_node = nodes.take(2)

  if send_node
    case send_node.type
    when :send, :csend, :super
      if block_node
        case block_node.type
        when :block, :numblock
          if send_node.equal?(block_node.children[0])
            return [send_node, block_node]
          end
        end
      end

      [send_node, nil]
    when :zsuper
      # zsuper doesn't receive block
      [send_node, nil]
    end
  end
end

#deconstruct_when_node(node) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/steep/node_helper.rb', line 122

def deconstruct_when_node(node)
  case node.type
  when :when
    *conds, body = node.children
    [
      conds,
      body,
      _ = node.loc
    ]
  end
end

#deconstruct_when_node!(node) ⇒ Object



134
135
136
# File 'lib/steep/node_helper.rb', line 134

def deconstruct_when_node!(node)
  deconstruct_when_node(node) or raise
end

#deconstruct_whileish_node(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/steep/node_helper.rb', line 74

def deconstruct_whileish_node(node)
  case node.type
  when :while, :until, :while_post, :until_post
    [
      node.children[0],
      node.children[1],
      _ = node.location
    ]
  end
end

#deconstruct_whileish_node!(node) ⇒ Object



85
86
87
# File 'lib/steep/node_helper.rb', line 85

def deconstruct_whileish_node!(node)
  deconstruct_whileish_node(node) or raise
end

#each_child_node(node, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/steep/node_helper.rb', line 3

def each_child_node(node, &block)
  if block
    node.children.each do |child|
      if child.is_a?(Parser::AST::Node)
        yield child
      end
    end
  else
    enum_for :each_child_node, node
  end
end

#each_descendant_node(node, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/steep/node_helper.rb', line 15

def each_descendant_node(node, &block)
  if block
    each_child_node(node) do |child|
      yield child
      each_descendant_node(child, &block)
    end
  else
    enum_for :each_descendant_node, node
  end
end

#private_send?(node) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/steep/node_helper.rb', line 221

def private_send?(node)
  case node.type
  when :block, :numblock
    private_send?(node.children[0])
  when :send, :csend
    receiver, = deconstruct_send_node!(node)

    if receiver && receiver.type != :self
      return false
    end

    true
  else
    raise "Unexpected node is given: #{node.inspect}"
  end
end

#test_case_node(node) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/steep/node_helper.rb', line 114

def test_case_node(node)
  if (a, b, c, d = deconstruct_case_node(node))
    yield a, b, c, d
  else
    false
  end
end

#test_if_node(node) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/steep/node_helper.rb', line 66

def test_if_node(node)
  if (a, b, c, d = deconstruct_if_node(node))
    yield(a, b, c, d)
  else
    false
  end
end

#test_resbody_node(node) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/steep/node_helper.rb', line 188

def test_resbody_node(node)
  if (a, b, c, d = deconstruct_resbody_node(node))
    yield a, b, c, d
  else
    false
  end
end

#test_rescue_node(node) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/steep/node_helper.rb', line 164

def test_rescue_node(node)
  if (a, b, c, d = deconstruct_rescue_node(node))
    yield a, b, c, d
  else
    false
  end
end

#test_send_node(node) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/steep/node_helper.rb', line 213

def test_send_node(node)
  if (a, b, c, d = deconstruct_send_node(node))
    yield a, b, c, d
  else
    false
  end
end

#test_when_node(node) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/steep/node_helper.rb', line 138

def test_when_node(node)
  if (a, b, c = deconstruct_when_node(node))
    yield a, b, c
  else
    false
  end
end

#test_whileish_node(node) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/steep/node_helper.rb', line 89

def test_whileish_node(node)
  if (a, b, c = deconstruct_whileish_node(node))
    yield(a, b, c)
  else
    false
  end
end

#value_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/steep/node_helper.rb', line 26

def value_node?(node)
  case node.type
  when :self
    true
  when :true, :false, :str, :sym, :int, :float, :nil
    true
  when :lvar
    true
  when :const
    each_child_node(node).all? {|child| child.type == :cbase || value_node?(child) }
  when :array
    each_child_node(node).all? {|child| value_node?(child) }
  when :hash
    each_child_node(node).all? do |pair|
      each_child_node(pair).all? {|child| value_node?(child) }
    end
  when :dstr
    each_child_node(node).all? {|child| value_node?(child)}
  when :begin
    each_child_node(node).all? {|child| value_node?(child) }
  else
    false
  end
end