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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Call.



2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
# File 'lib/syntax_tree/node.rb', line 2729

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



2721
2722
2723
# File 'lib/syntax_tree/node.rb', line 2721

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2727
2728
2729
# File 'lib/syntax_tree/node.rb', line 2727

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2724
2725
2726
# File 'lib/syntax_tree/node.rb', line 2724

def location
  @location
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2718
2719
2720
# File 'lib/syntax_tree/node.rb', line 2718

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2715
2716
2717
# File 'lib/syntax_tree/node.rb', line 2715

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2712
2713
2714
# File 'lib/syntax_tree/node.rb', line 2712

def receiver
  @receiver
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



2745
2746
2747
2748
2749
2750
2751
2752
# File 'lib/syntax_tree/node.rb', line 2745

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

#deconstruct_keys(keys) ⇒ Object



2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
# File 'lib/syntax_tree/node.rb', line 2756

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

#format(q) ⇒ Object



2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
# File 'lib/syntax_tree/node.rb', line 2767

def format(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: true)
        end

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

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

      q.format(arguments) if arguments
    end
  end
end

#pretty_print(q) ⇒ Object



2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
# File 'lib/syntax_tree/node.rb', line 2795

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("call")

    q.breakable
    q.pp(receiver)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(message)

    if arguments
      q.breakable
      q.pp(arguments)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
# File 'lib/syntax_tree/node.rb', line 2817

def to_json(*opts)
  {
    type: :call,
    receiver: receiver,
    op: operator,
    message: message,
    args: arguments,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end