Class: RBI::Parser::TreeBuilder

Inherits:
Visitor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rbi/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, comments:, file:) ⇒ TreeBuilder

Returns a new instance of TreeBuilder.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbi/parser.rb', line 163

def initialize(source, comments:, file:)
  super(source, file: file)

  @comments_by_line = T.let(comments.to_h { |c| [c.location.start_line, c] }, T::Hash[Integer, Prism::Comment])
  @tree = T.let(Tree.new, Tree)

  @scopes_stack = T.let([@tree], T::Array[Tree])
  @last_node = T.let(nil, T.nilable(Prism::Node))
  @last_sigs = T.let([], T::Array[RBI::Sig])
  @last_sigs_comments = T.let([], T::Array[Comment])
end

Instance Attribute Details

#last_nodeObject (readonly)

Returns the value of attribute last_node.



160
161
162
# File 'lib/rbi/parser.rb', line 160

def last_node
  @last_node
end

#treeObject (readonly)

Returns the value of attribute tree.



157
158
159
# File 'lib/rbi/parser.rb', line 157

def tree
  @tree
end

Instance Method Details

#visit_call_node(node) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/rbi/parser.rb', line 299

def visit_call_node(node)
  @last_node = node
  message = node.name.to_s
  case message
  when "abstract!", "sealed!", "interface!"
    current_scope << Helper.new(
      message.delete_suffix("!"),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "attr_reader"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << AttrReader.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym }),
      sigs: current_sigs,
      loc: node_loc(node),
      comments: current_sigs_comments + node_comments(node),
    )
  when "attr_writer"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << AttrWriter.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym }),
      sigs: current_sigs,
      loc: node_loc(node),
      comments: current_sigs_comments + node_comments(node),
    )
  when "attr_accessor"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << AttrAccessor.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym }),
      sigs: current_sigs,
      loc: node_loc(node),
      comments: current_sigs_comments + node_comments(node),
    )
  when "enums"
    block = node.block

    unless block.is_a?(Prism::BlockNode)
      @last_node = nil
      return
    end

    body = block.body

    unless body.is_a?(Prism::StatementsNode)
      @last_node = nil
      return
    end

    current_scope << TEnumBlock.new(
      body.body.map { |stmt| T.cast(stmt, Prism::ConstantWriteNode).name.to_s },
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "extend"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << Extend.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg) }),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "include"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << Include.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg) }),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "mixes_in_class_methods"
    args = node.arguments

    unless args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      @last_node = nil
      return
    end

    current_scope << MixesInClassMethods.new(
      *T.unsafe(args.arguments.map { |arg| node_string!(arg) }),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "private", "protected", "public"
    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode) && args.arguments.any?
      visit(node.arguments)
      last_node = @scopes_stack.last&.nodes&.last
      case last_node
      when Method, Attr
        last_node.visibility = parse_visibility(node.name.to_s, node)
      else
        raise ParseError.new(
          "Unexpected token `#{node.message}` before `#{last_node&.string&.strip}`",
          node_loc(node),
        )
      end
    else
      current_scope << parse_visibility(node.name.to_s, node)
    end
  when "prop", "const"
    parse_tstruct_field(node)
  when "requires_ancestor"
    block = node.block

    unless block.is_a?(Prism::BlockNode)
      @last_node = nil
      return
    end

    body = block.body

    unless body.is_a?(Prism::StatementsNode)
      @last_node = nil
      return
    end

    current_scope << RequiresAncestor.new(
      node_string!(body),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when "sig"
    @last_sigs << parse_sig(node)
  else
    current_scope << Send.new(
      message,
      parse_send_args(node.arguments),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  end

  @last_node = nil
end

#visit_class_node(node) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rbi/parser.rb', line 176

def visit_class_node(node)
  @last_node = node
  scope = Class.new(
    node_string!(node.constant_path),
    superclass_name: node_string(node.superclass),
    loc: node_loc(node),
    comments: node_comments(node),
  )

  current_scope << scope
  @scopes_stack << scope
  visit(node.body)
  collect_dangling_comments(node)
  @scopes_stack.pop
  @last_node = nil
end

#visit_constant_assign(node) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rbi/parser.rb', line 208

def visit_constant_assign(node)
  struct = parse_struct(node)

  current_scope << if struct
    struct
  elsif type_variable_definition?(node.value)
    TypeMember.new(
      case node
      when Prism::ConstantWriteNode
        node.name.to_s
      when Prism::ConstantPathWriteNode
        node_string!(node.target)
      end,
      node_string!(node.value),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  else
    Const.new(
      case node
      when Prism::ConstantWriteNode
        node.name.to_s
      when Prism::ConstantPathWriteNode
        node_string!(node.target)
      end,
      node_string!(node.value),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  end
end

#visit_constant_path_write_node(node) ⇒ Object



201
202
203
204
205
# File 'lib/rbi/parser.rb', line 201

def visit_constant_path_write_node(node)
  @last_node = node
  visit_constant_assign(node)
  @last_node = nil
end

#visit_constant_write_node(node) ⇒ Object



194
195
196
197
198
# File 'lib/rbi/parser.rb', line 194

def visit_constant_write_node(node)
  @last_node = node
  visit_constant_assign(node)
  @last_node = nil
end

#visit_def_node(node) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/rbi/parser.rb', line 241

def visit_def_node(node)
  @last_node = node
  current_scope << Method.new(
    node.name.to_s,
    params: parse_params(node.parameters),
    sigs: current_sigs,
    loc: node_loc(node),
    comments: current_sigs_comments + node_comments(node),
    is_singleton: !!node.receiver,
  )
  @last_node = nil
end

#visit_module_node(node) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rbi/parser.rb', line 255

def visit_module_node(node)
  @last_node = node
  scope = Module.new(
    node_string!(node.constant_path),
    loc: node_loc(node),
    comments: node_comments(node),
  )

  current_scope << scope
  @scopes_stack << scope
  visit(node.body)
  collect_dangling_comments(node)
  @scopes_stack.pop
  @last_node = nil
end

#visit_program_node(node) ⇒ Object



272
273
274
275
276
277
278
279
280
# File 'lib/rbi/parser.rb', line 272

def visit_program_node(node)
  @last_node = node
  super

  collect_orphan_comments
  separate_header_comments
  set_root_tree_loc
  @last_node = nil
end

#visit_singleton_class_node(node) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rbi/parser.rb', line 283

def visit_singleton_class_node(node)
  @last_node = node
  scope = SingletonClass.new(
    loc: node_loc(node),
    comments: node_comments(node),
  )

  current_scope << scope
  @scopes_stack << scope
  visit(node.body)
  collect_dangling_comments(node)
  @scopes_stack.pop
  @last_node = nil
end