Class: SyntaxTree::Call

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Call represents a method call.

receiver.message

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(receiver:, operator:, message:, arguments:, location:, comments: []) ⇒ Call



2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
# File 'lib/syntax_tree/node.rb', line 2662

def initialize(
  receiver:,
  operator:,
  message:,
  arguments:,
  location:,
  comments: []
)
  @receiver = receiver
  @operator = operator
  @message = message
  @arguments = arguments
  @location = location
  @comments = comments
end

Instance Attribute Details

#argumentsObject (readonly)

nil | ArgParen | Args

the arguments to the method call



2657
2658
2659
# File 'lib/syntax_tree/node.rb', line 2657

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2660
2661
2662
# File 'lib/syntax_tree/node.rb', line 2660

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2654
2655
2656
# File 'lib/syntax_tree/node.rb', line 2654

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2651
2652
2653
# File 'lib/syntax_tree/node.rb', line 2651

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2648
2649
2650
# File 'lib/syntax_tree/node.rb', line 2648

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



2678
2679
2680
# File 'lib/syntax_tree/node.rb', line 2678

def accept(visitor)
  visitor.visit_call(self)
end

#child_nodesObject Also known as: deconstruct



2682
2683
2684
2685
2686
2687
2688
2689
# File 'lib/syntax_tree/node.rb', line 2682

def child_nodes
  [
    receiver,
    (operator if operator != :"::"),
    (message if message != :call),
    arguments
  ]
end

#deconstruct_keys(_keys) ⇒ Object



2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
# File 'lib/syntax_tree/node.rb', line 2693

def deconstruct_keys(_keys)
  {
    receiver: receiver,
    operator: operator,
    message: message,
    arguments: arguments,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
# File 'lib/syntax_tree/node.rb', line 2704

def format(q)
  # If we're at the top of a call chain, then we're going to do some
  # specialized printing in case we can print it nicely. We _only_ do this
  # at the top of the chain to avoid weird recursion issues.
  if CallChainFormatter.chained?(receiver) &&
       !CallChainFormatter.chained?(q.parent)
    q.group do
      q
        .if_break { CallChainFormatter.new(self).format(q) }
        .if_flat { format_contents(q) }
    end
  else
    format_contents(q)
  end
end

#format_arguments(q) ⇒ Object

Print out the arguments to this call. If there are no arguments, then do nothing.



2722
2723
2724
2725
2726
2727
2728
2729
2730
# File 'lib/syntax_tree/node.rb', line 2722

def format_arguments(q)
  case arguments
  when ArgParen
    q.format(arguments)
  when Args
    q.text(" ")
    q.format(arguments)
  end
end

#format_contents(q) ⇒ Object



2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
# File 'lib/syntax_tree/node.rb', line 2732

def format_contents(q)
  call_operator = CallOperatorFormatter.new(operator)

  q.group do
    q.format(receiver)

    # If there are trailing comments on the call operator, then we need to
    # use the trailing form as opposed to the leading form.
    q.format(call_operator) if call_operator.comments.any?

    q.group do
      q.indent do
        if receiver.comments.any? || call_operator.comments.any?
          q.breakable_force
        end

        if call_operator.comments.empty?
          q.format(call_operator, stackable: false)
        end

        q.format(message) if message != :call
      end

      format_arguments(q)
    end
  end
end