Class: SyntaxTree::CommandCall

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

Overview

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

object.method argument

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: []) ⇒ CommandCall

Returns a new instance of CommandCall.



2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
# File 'lib/syntax_tree/node.rb', line 2991

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 | Args

the arguments going along with the message



2986
2987
2988
# File 'lib/syntax_tree/node.rb', line 2986

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2989
2990
2991
# File 'lib/syntax_tree/node.rb', line 2989

def comments
  @comments
end

#messageObject (readonly)

Const | Ident | Op

the message being send



2983
2984
2985
# File 'lib/syntax_tree/node.rb', line 2983

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator used to send the message



2980
2981
2982
# File 'lib/syntax_tree/node.rb', line 2980

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the message



2977
2978
2979
# File 'lib/syntax_tree/node.rb', line 2977

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



3007
3008
3009
# File 'lib/syntax_tree/node.rb', line 3007

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

#child_nodesObject Also known as: deconstruct



3011
3012
3013
# File 'lib/syntax_tree/node.rb', line 3011

def child_nodes
  [receiver, message, arguments]
end

#deconstruct_keys(_keys) ⇒ Object



3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
# File 'lib/syntax_tree/node.rb', line 3017

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

#format(q) ⇒ Object



3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
# File 'lib/syntax_tree/node.rb', line 3028

def format(q)
  q.group do
    doc =
      q.nest(0) do
        q.format(receiver)
        q.format(CallOperatorFormatter.new(operator), stackable: false)
        q.format(message)
      end

    case arguments
    in Args[parts: [IfOp]]
      q.if_flat { q.text(" ") }
      q.format(arguments)
    in Args
      q.text(" ")
      q.nest(argument_alignment(q, doc)) { q.format(arguments) }
    else
      # If there are no arguments, print nothing.
    end
  end
end