Class: Spoom::Deadcode::Remover::NodeContext

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/deadcode/remover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, comments, node, nesting) ⇒ NodeContext

Returns a new instance of NodeContext.



392
393
394
395
396
397
# File 'lib/spoom/deadcode/remover.rb', line 392

def initialize(source, comments, node, nesting)
  @source = source
  @comments = comments
  @node = node
  @nesting = nesting
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



376
377
378
# File 'lib/spoom/deadcode/remover.rb', line 376

def comments
  @comments
end

#nestingObject

Returns the value of attribute nesting.



382
383
384
# File 'lib/spoom/deadcode/remover.rb', line 382

def nesting
  @nesting
end

#nodeObject (readonly)

Returns the value of attribute node.



379
380
381
# File 'lib/spoom/deadcode/remover.rb', line 379

def node
  @node
end

Instance Method Details

#attached_comments(node) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/spoom/deadcode/remover.rb', line 506

def attached_comments(node)
  comments = T.let([], T::Array[Prism::Comment])

  start_line = node.location.start_line - 1
  start_line.downto(1) do |line|
    comment = @comments[line]
    break unless comment

    comments << comment
  end

  comments.reverse
end

#attached_sigObject



534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/spoom/deadcode/remover.rb', line 534

def attached_sig
  previous_nodes.reverse_each do |node|
    if node.is_a?(Prism::Comment)
      next
    elsif sorbet_signature?(node)
      return T.cast(node, Prism::CallNode)
    else
      break
    end
  end

  nil
end

#attached_sigsObject



521
522
523
524
525
526
527
528
529
530
531
# File 'lib/spoom/deadcode/remover.rb', line 521

def attached_sigs
  nodes = T.let([], T::Array[Prism::Node])

  previous_nodes.reverse_each do |prev_node|
    break unless sorbet_signature?(prev_node)

    nodes << prev_node
  end

  nodes.reverse
end

#comments_between_lines(start_line, end_line) ⇒ Object



494
495
496
497
498
499
500
501
502
503
# File 'lib/spoom/deadcode/remover.rb', line 494

def comments_between_lines(start_line, end_line)
  comments = T.let([], T::Array[Prism::Comment])

  (start_line + 1).upto(end_line - 1) do |line|
    comment = @comments[line]
    comments << comment if comment
  end

  comments
end

#next_nodeObject



444
445
446
# File 'lib/spoom/deadcode/remover.rb', line 444

def next_node
  next_nodes.first
end

#next_nodesObject

Raises:



433
434
435
436
437
438
439
440
441
# File 'lib/spoom/deadcode/remover.rb', line 433

def next_nodes
  parent = parent_node
  child_nodes = parent.child_nodes.compact

  index = child_nodes.index(node)
  raise Error, "Node #{@node} not found in nesting node #{parent}" unless index

  T.must(child_nodes.compact[(index + 1)..-1])
end

#parent_contextObject

Raises:



408
409
410
411
412
413
414
# File 'lib/spoom/deadcode/remover.rb', line 408

def parent_context
  nesting = @nesting.dup
  parent = nesting.pop
  raise Error, "No parent context for node #{@node}" unless parent

  NodeContext.new(@source, @comments, parent, nesting)
end

#parent_nodeObject

Raises:



400
401
402
403
404
405
# File 'lib/spoom/deadcode/remover.rb', line 400

def parent_node
  parent = @nesting.last
  raise Error, "No parent for node #{node}" unless parent

  parent
end

#previous_nodeObject



428
429
430
# File 'lib/spoom/deadcode/remover.rb', line 428

def previous_node
  previous_nodes.last
end

#previous_nodesObject

Raises:



417
418
419
420
421
422
423
424
425
# File 'lib/spoom/deadcode/remover.rb', line 417

def previous_nodes
  parent = parent_node
  child_nodes = parent.child_nodes.compact

  index = child_nodes.index(@node)
  raise Error, "Node #{@node} not found in parent #{parent}" unless index

  T.must(child_nodes[0...index])
end

#sclass_contextObject



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
# File 'lib/spoom/deadcode/remover.rb', line 449

def sclass_context
  sclass = T.let(nil, T.nilable(Prism::SingletonClassNode))

  nesting = @nesting.dup
  until nesting.empty? || sclass
    node = nesting.pop
    next unless node.is_a?(Prism::SingletonClassNode)

    sclass = node
  end

  return unless sclass.is_a?(Prism::SingletonClassNode)

  body = sclass.body
  return NodeContext.new(@source, @comments, sclass, nesting) unless body.is_a?(Prism::StatementsNode)

  nodes = body.child_nodes.reject do |node|
    sorbet_signature?(node) || sorbet_extend_sig?(node)
  end

  if nodes.size <= 1
    return NodeContext.new(@source, @comments, sclass, nesting)
  end

  nil
end

#sorbet_extend_sig?(node) ⇒ Boolean

Returns:

  • (Boolean)


482
483
484
485
486
487
488
489
490
491
# File 'lib/spoom/deadcode/remover.rb', line 482

def sorbet_extend_sig?(node)
  return false unless node.is_a?(Prism::CallNode)
  return false unless node.name == :extend

  args = node.arguments
  return false unless args
  return false unless args.arguments.size == 1

  args.arguments.first&.slice == "T::Sig"
end

#sorbet_signature?(node) ⇒ Boolean

Returns:

  • (Boolean)


477
478
479
# File 'lib/spoom/deadcode/remover.rb', line 477

def sorbet_signature?(node)
  node.is_a?(Prism::CallNode) && node.name == :sig
end