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
# 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])
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



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
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
# File 'lib/rbi/parser.rb', line 324

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(
      *T.unsafe(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(
      *T.unsafe(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(
      *T.unsafe(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(
      *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)
      when Send
        current_scope << Send.new(
          message,
          parse_send_args(node.arguments),
          loc: node_loc(node),
          comments: node_comments(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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbi/parser.rb', line 175

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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rbi/parser.rb', line 224

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



217
218
219
220
221
# File 'lib/rbi/parser.rb', line 217

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

#visit_constant_write_node(node) ⇒ Object



210
211
212
213
214
# File 'lib/rbi/parser.rb', line 210

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

#visit_def_node(node) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rbi/parser.rb', line 257

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/rbi/parser.rb', line 278

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



296
297
298
299
300
301
302
303
304
# File 'lib/rbi/parser.rb', line 296

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



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

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