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

Inherits:
Object
  • Object
show all
Defined in:
lib/spoom/deadcode/remover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

: (String source, Hash[Integer, Prism::Comment] comments, Prism::Node node, Array nesting) -> void



510
511
512
513
514
515
# File 'lib/spoom/deadcode/remover.rb', line 510

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

Instance Attribute Details

#comments ⇒ Object (readonly)

: Hash[Integer, Prism::Comment]



501
502
503
# File 'lib/spoom/deadcode/remover.rb', line 501

def comments
  @comments
end

#nesting ⇒ Object

: Array



507
508
509
# File 'lib/spoom/deadcode/remover.rb', line 507

def nesting
  @nesting
end

#node ⇒ Object (readonly)

: Prism::Node



504
505
506
# File 'lib/spoom/deadcode/remover.rb', line 504

def node
  @node
end

Instance Method Details

#attached_comments(node) ⇒ Object

: (Prism::Node node) -> Array



624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/spoom/deadcode/remover.rb', line 624

def attached_comments(node)
  comments = [] #: 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_sig ⇒ Object

: -> Prism::CallNode?



652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/spoom/deadcode/remover.rb', line 652

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_sigs ⇒ Object

: -> Array



639
640
641
642
643
644
645
646
647
648
649
# File 'lib/spoom/deadcode/remover.rb', line 639

def attached_sigs
  nodes = [] #: 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

: (Integer start_line, Integer end_line) -> Array



612
613
614
615
616
617
618
619
620
621
# File 'lib/spoom/deadcode/remover.rb', line 612

def comments_between_lines(start_line, end_line)
  comments = [] #: Array[Prism::Comment]

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

  comments
end

#next_node ⇒ Object

: -> Prism::Node?



562
563
564
# File 'lib/spoom/deadcode/remover.rb', line 562

def next_node
  next_nodes.first
end

#next_nodes ⇒ Object

: -> Array

Raises:



551
552
553
554
555
556
557
558
559
# File 'lib/spoom/deadcode/remover.rb', line 551

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_context ⇒ Object

: -> NodeContext

Raises:



526
527
528
529
530
531
532
# File 'lib/spoom/deadcode/remover.rb', line 526

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_node ⇒ Object

: -> Prism::Node

Raises:



518
519
520
521
522
523
# File 'lib/spoom/deadcode/remover.rb', line 518

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

  parent
end

#previous_node ⇒ Object

: -> Prism::Node?



546
547
548
# File 'lib/spoom/deadcode/remover.rb', line 546

def previous_node
  previous_nodes.last
end

#previous_nodes ⇒ Object

: -> Array

Raises:



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

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_context ⇒ Object

: -> NodeContext?



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/spoom/deadcode/remover.rb', line 567

def sclass_context
  sclass = nil #: 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

: (Prism::Node? node) -> bool

Returns:

  • (Boolean)


600
601
602
603
604
605
606
607
608
609
# File 'lib/spoom/deadcode/remover.rb', line 600

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

: (Prism::Node? node) -> bool

Returns:

  • (Boolean)


595
596
597
# File 'lib/spoom/deadcode/remover.rb', line 595

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