Class: SyntaxTree::Call

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

Overview

Call represents a method call.

receiver.message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Call.



3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
# File 'lib/syntax_tree.rb', line 3200

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



3192
3193
3194
# File 'lib/syntax_tree.rb', line 3192

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3198
3199
3200
# File 'lib/syntax_tree.rb', line 3198

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3195
3196
3197
# File 'lib/syntax_tree.rb', line 3195

def location
  @location
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



3189
3190
3191
# File 'lib/syntax_tree.rb', line 3189

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



3186
3187
3188
# File 'lib/syntax_tree.rb', line 3186

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



3183
3184
3185
# File 'lib/syntax_tree.rb', line 3183

def receiver
  @receiver
end

Instance Method Details

#child_nodesObject



3216
3217
3218
3219
3220
3221
3222
3223
# File 'lib/syntax_tree.rb', line 3216

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

#format(q) ⇒ Object



3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
# File 'lib/syntax_tree.rb', line 3225

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

#pretty_print(q) ⇒ Object



3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
# File 'lib/syntax_tree.rb', line 3253

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("call")

    q.breakable
    q.pp(receiver)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(message)

    if arguments
      q.breakable
      q.pp(arguments)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
# File 'lib/syntax_tree.rb', line 3275

def to_json(*opts)
  {
    type: :call,
    receiver: receiver,
    op: operator,
    message: message,
    args: arguments,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end