Class: RBI::Parser::TreeBuilder

Inherits:
Visitor
  • Object
show all
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.

Signature:

  • (String source, comments: Array[Prism::Comment], file: String) -> void



187
188
189
190
191
192
193
194
195
196
# File 'lib/rbi/parser.rb', line 187

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

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

  @scopes_stack = [@tree] #: Array[Tree]
  @last_node = nil #: Prism::Node?
  @last_sigs = [] #: Array[RBI::Sig]
end

Instance Attribute Details

#last_nodeObject (readonly)

Signature:

  • Prism::Node?



184
185
186
# File 'lib/rbi/parser.rb', line 184

def last_node
  @last_node
end

#treeObject (readonly)

Signature:

  • Tree



181
182
183
# File 'lib/rbi/parser.rb', line 181

def tree
  @tree
end

Instance Method Details

#visit_call_node(node) ⇒ Object

Signature:

  • (Prism::CallNode node) -> void



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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/rbi/parser.rb', line 376

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

    sigs = current_sigs
    comments = detach_comments_from_sigs(sigs) + node_comments(node)

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

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

    sigs = current_sigs
    comments = detach_comments_from_sigs(sigs) + node_comments(node)

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

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

    sigs = current_sigs
    comments = detach_comments_from_sigs(sigs) + node_comments(node)

    current_scope << AttrAccessor.new(
      *args.arguments.map { |arg| node_string!(arg).delete_prefix(":").to_sym },
      sigs: sigs,
      loc: node_loc(node),
      comments: comments,
    )
  when "enums"
    if node.block && node.arguments.nil?
      scope = TEnumBlock.new(loc: node_loc(node), comments: node_comments(node))
      current_scope << scope
      @scopes_stack << scope
      visit(node.block)
      @scopes_stack.pop
    else
      current_scope << Send.new(
        message,
        parse_send_args(node.arguments),
        loc: node_loc(node),
        comments: node_comments(node),
      )
    end
  when "extend"
    args = node.arguments

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

    current_scope << Extend.new(
      *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(
      *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(
      *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)
      when Send
        current_scope << Send.new(
          message,
          parse_send_args(node.arguments),
          loc: node_loc(node),
          comments: node_comments(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"
    unless node.receiver.nil? || self?(node.receiver) || t_sig_without_runtime?(node.receiver)
      @last_node = nil
      return
    end
    @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

Signature:

  • (Prism::ClassNode node) -> void



200
201
202
203
204
205
206
207
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
# File 'lib/rbi/parser.rb', line 200

def visit_class_node(node)
  @last_node = node
  superclass_name = node_string(node.superclass)
  scope = case superclass_name
  when /^(::)?T::Struct$/
    TStruct.new(
      node_string!(node.constant_path),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  when /^(::)?T::Enum$/
    TEnum.new(
      node_string!(node.constant_path),
      loc: node_loc(node),
      comments: node_comments(node),
    )
  else
    Class.new(
      node_string!(node.constant_path),
      superclass_name: superclass_name,
      loc: node_loc(node),
      comments: node_comments(node),
    )
  end

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

#visit_constant_assign(node) ⇒ Object

Signature:

  • ((Prism::ConstantWriteNode | Prism::ConstantPathWriteNode) node) -> void



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rbi/parser.rb', line 251

def visit_constant_assign(node)
  struct = parse_struct(node)

  current_scope << if struct
    struct
  elsif t_enum_value?(node)
    TEnumValue.new(
      case node
      when Prism::ConstantWriteNode
        node.name.to_s
      when Prism::ConstantPathWriteNode
        node_string!(node.target)
      end,
      loc: node_loc(node),
      comments: node_comments(node),
    )
  else
    adjusted_node_location = adjust_prism_location_for_heredoc(node)

    adjusted_value_location = Prism::Location.new(
      node.value.location.send(:source),
      node.value.location.start_offset,
      adjusted_node_location.end_offset - node.value.location.start_offset,
    )

    if type_variable_definition?(node.value)
      TypeMember.new(
        case node
        when Prism::ConstantWriteNode
          node.name.to_s
        when Prism::ConstantPathWriteNode
          node_string!(node.target)
        end,
        adjusted_value_location.slice,
        loc: Loc.from_prism(@file, adjusted_node_location),
        comments: node_comments(node),
      )
    else
      Const.new(
        case node
        when Prism::ConstantWriteNode
          node.name.to_s
        when Prism::ConstantPathWriteNode
          node_string!(node.target)
        end,
        adjusted_value_location.slice,
        loc: Loc.from_prism(@file, adjusted_node_location),
        comments: node_comments(node),
      )
    end
  end
end

#visit_constant_path_write_node(node) ⇒ Object

Signature:

  • (Prism::ConstantPathWriteNode node) -> void



244
245
246
247
248
# File 'lib/rbi/parser.rb', line 244

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

#visit_constant_write_node(node) ⇒ Object

Signature:

  • (Prism::ConstantWriteNode node) -> void



236
237
238
239
240
# File 'lib/rbi/parser.rb', line 236

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

#visit_def_node(node) ⇒ Object

Signature:

  • (Prism::DefNode node) -> void



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rbi/parser.rb', line 306

def visit_def_node(node)
  @last_node = node

  # We need to collect the comments with `current_sigs_comments` _before_ visiting the parameters to make sure
  # the method comments are properly associated with the sigs and not the parameters.
  sigs = current_sigs
  comments = detach_comments_from_sigs(sigs) + node_comments(node)
  params = parse_params(node.parameters)

  current_scope << Method.new(
    node.name.to_s,
    params: params,
    sigs: sigs,
    loc: node_loc(node),
    comments: comments,
    is_singleton: !!node.receiver,
  )
  @last_node = nil
end

#visit_module_node(node) ⇒ Object

Signature:

  • (Prism::ModuleNode node) -> void



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/rbi/parser.rb', line 328

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)
  scope.nodes.concat(current_sigs)
  collect_dangling_comments(node)
  @scopes_stack.pop
  @last_node = nil
end

#visit_program_node(node) ⇒ Object

Signature:

  • (Prism::ProgramNode node) -> void



347
348
349
350
351
352
353
354
355
# File 'lib/rbi/parser.rb', line 347

def visit_program_node(node)
  @last_node = node
  super
  @tree.nodes.concat(current_sigs)
  collect_orphan_comments
  separate_header_comments
  set_root_tree_loc
  @last_node = nil
end

#visit_singleton_class_node(node) ⇒ Object

Signature:

  • (Prism::SingletonClassNode node) -> void



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rbi/parser.rb', line 359

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)
  scope.nodes.concat(current_sigs)
  collect_dangling_comments(node)
  @scopes_stack.pop
  @last_node = nil
end