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

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

Returns a new instance of Call.



2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
# File 'lib/syntax_tree/node.rb', line 2164

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



2159
2160
2161
# File 'lib/syntax_tree/node.rb', line 2159

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2162
2163
2164
# File 'lib/syntax_tree/node.rb', line 2162

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2156
2157
2158
# File 'lib/syntax_tree/node.rb', line 2156

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2153
2154
2155
# File 'lib/syntax_tree/node.rb', line 2153

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2150
2151
2152
# File 'lib/syntax_tree/node.rb', line 2150

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



2180
2181
2182
# File 'lib/syntax_tree/node.rb', line 2180

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

#child_nodesObject Also known as: deconstruct



2184
2185
2186
2187
2188
2189
2190
2191
# File 'lib/syntax_tree/node.rb', line 2184

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

#deconstruct_keys(keys) ⇒ Object



2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
# File 'lib/syntax_tree/node.rb', line 2195

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

#format(q) ⇒ Object



2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
# File 'lib/syntax_tree/node.rb', line 2206

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