Class: SyntaxTree::CallNode

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

Overview

CallNode 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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(receiver:, operator:, message:, arguments:, location:) ⇒ CallNode

Returns a new instance of CallNode.



2962
2963
2964
2965
2966
2967
2968
2969
# File 'lib/syntax_tree/node.rb', line 2962

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

Instance Attribute Details

#argumentsObject (readonly)

nil | ArgParen | Args

the arguments to the method call



2957
2958
2959
# File 'lib/syntax_tree/node.rb', line 2957

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2960
2961
2962
# File 'lib/syntax_tree/node.rb', line 2960

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2954
2955
2956
# File 'lib/syntax_tree/node.rb', line 2954

def message
  @message
end

#operatorObject (readonly)

nil | :“::” | Op | Period

the operator being used to send the message



2951
2952
2953
# File 'lib/syntax_tree/node.rb', line 2951

def operator
  @operator
end

#receiverObject (readonly)

nil | Node

the receiver of the method call



2948
2949
2950
# File 'lib/syntax_tree/node.rb', line 2948

def receiver
  @receiver
end

Instance Method Details

#===(other) ⇒ Object



3057
3058
3059
3060
3061
# File 'lib/syntax_tree/node.rb', line 3057

def ===(other)
  other.is_a?(CallNode) && receiver === other.receiver &&
    operator === other.operator && message === other.message &&
    arguments === other.arguments
end

#accept(visitor) ⇒ Object



2971
2972
2973
# File 'lib/syntax_tree/node.rb', line 2971

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

#arityObject



3103
3104
3105
# File 'lib/syntax_tree/node.rb', line 3103

def arity
  arguments&.arity || 0
end

#child_nodesObject Also known as: deconstruct



2975
2976
2977
2978
2979
2980
2981
2982
# File 'lib/syntax_tree/node.rb', line 2975

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

#copy(receiver: nil, operator: nil, message: nil, arguments: nil, location: nil) ⇒ Object



2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
# File 'lib/syntax_tree/node.rb', line 2984

def copy(
  receiver: nil,
  operator: nil,
  message: nil,
  arguments: nil,
  location: nil
)
  node =
    CallNode.new(
      receiver: receiver || self.receiver,
      operator: operator || self.operator,
      message: message || self.message,
      arguments: arguments || self.arguments,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
# File 'lib/syntax_tree/node.rb', line 3006

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

#format(q) ⇒ Object



3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
# File 'lib/syntax_tree/node.rb', line 3017

def format(q)
  if receiver
    # 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
  else
    q.format(message)

    # Note that this explicitly leaves parentheses in place even if they are
    # empty. There are two reasons we would need to do this. The first is if
    # we're calling something that looks like a constant, as in:
    #
    #     Foo()
    #
    # In this case if we remove the parentheses then this becomes a constant
    # reference and not a method call. The second is if we're calling a
    # method that is the same name as a local variable that is in scope, as
    # in:
    #
    #     foo = foo()
    #
    # In this case we have to keep the parentheses or else it treats this
    # like assigning nil to the local variable. Note that we could attempt
    # to be smarter about this by tracking the local variables that are in
    # scope, but for now it's simpler and more efficient to just leave the
    # parentheses in place.
    q.format(arguments) if arguments
  end
end

#format_arguments(q) ⇒ Object

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



3065
3066
3067
3068
3069
3070
3071
3072
3073
# File 'lib/syntax_tree/node.rb', line 3065

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

#format_contents(q) ⇒ Object



3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
# File 'lib/syntax_tree/node.rb', line 3075

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