class SyntaxTree::CommandCall

CommandCall represents a method call on an object with arguments and no parentheses.

object.method argument

Attributes

arguments[R]
nil | Args | ArgParen

the arguments going along with the message

block[R]
nil | BlockNode

the block associated with this method call

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

message[R]
:call | Const | Ident | Op

the message being send

operator[R]
nil | :“::” | Op | Period

the operator used to send the message

receiver[R]
nil | Node

the receiver of the message

Public Class Methods

new( receiver:, operator:, message:, arguments:, block:, location: ) click to toggle source
# File lib/syntax_tree/node.rb, line 3582
def initialize(
  receiver:,
  operator:,
  message:,
  arguments:,
  block:,
  location:
)
  @receiver = receiver
  @operator = operator
  @message = message
  @arguments = arguments
  @block = block
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 3686
def ===(other)
  other.is_a?(CommandCall) && receiver === other.receiver &&
    operator === other.operator && message === other.message &&
    arguments === other.arguments && block === other.block
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 3599
def accept(visitor)
  visitor.visit_command_call(self)
end
arity() click to toggle source
# File lib/syntax_tree/node.rb, line 3692
def arity
  arguments&.arity || 0
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 3603
def child_nodes
  [receiver, message, arguments, block]
end
Also aliased as: deconstruct
copy( receiver: nil, operator: nil, message: nil, arguments: nil, block: nil, location: nil ) click to toggle source
# File lib/syntax_tree/node.rb, line 3607
def copy(
  receiver: nil,
  operator: nil,
  message: nil,
  arguments: nil,
  block: nil,
  location: nil
)
  node =
    CommandCall.new(
      receiver: receiver || self.receiver,
      operator: operator || self.operator,
      message: message || self.message,
      arguments: arguments || self.arguments,
      block: block || self.block,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 3631
def deconstruct_keys(_keys)
  {
    receiver: receiver,
    operator: operator,
    message: message,
    arguments: arguments,
    block: block,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 3643
def format(q)
  message = self.message
  arguments = self.arguments
  block = self.block

  q.group do
    doc =
      q.nest(0) do
        q.format(receiver)

        # If there are leading comments on the message then we know we have
        # a newline in the source that is forcing these things apart. In
        # this case we will have to use a trailing operator.
        if message != :call && message.comments.any?(&:leading?)
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.indent do
            q.breakable_empty
            q.format(message)
          end
        else
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.format(message)
        end
      end

    # Format the arguments for this command call here. If there are no
    # arguments, then print nothing.
    if arguments
      parts = arguments.parts

      if parts.length == 1 && parts.first.is_a?(IfOp)
        q.if_flat { q.text(" ") }
        q.format(arguments)
      else
        q.text(" ")
        q.nest(argument_alignment(q, doc)) { q.format(arguments) }
      end
    end
  end

  q.format(block) if block
end