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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of CommandCall.



2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
# File 'lib/syntax_tree/node.rb', line 2933

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



2928
2929
2930
# File 'lib/syntax_tree/node.rb', line 2928

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2931
2932
2933
# File 'lib/syntax_tree/node.rb', line 2931

def comments
  @comments
end

#messageObject (readonly)

Const | Ident | Op

the message being send



2925
2926
2927
# File 'lib/syntax_tree/node.rb', line 2925

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator used to send the message



2922
2923
2924
# File 'lib/syntax_tree/node.rb', line 2922

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the message



2919
2920
2921
# File 'lib/syntax_tree/node.rb', line 2919

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [receiver, message, arguments]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
# File 'lib/syntax_tree/node.rb', line 2970

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