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.



3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
# File 'lib/syntax_tree.rb', line 3321

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



3313
3314
3315
# File 'lib/syntax_tree.rb', line 3313

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3319
3320
3321
# File 'lib/syntax_tree.rb', line 3319

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3316
3317
3318
# File 'lib/syntax_tree.rb', line 3316

def location
  @location
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



3310
3311
3312
# File 'lib/syntax_tree.rb', line 3310

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



3307
3308
3309
# File 'lib/syntax_tree.rb', line 3307

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



3304
3305
3306
# File 'lib/syntax_tree.rb', line 3304

def receiver
  @receiver
end

Instance Method Details

#child_nodesObject



3337
3338
3339
3340
3341
3342
3343
3344
# File 'lib/syntax_tree.rb', line 3337

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

#format(q) ⇒ Object



3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
# File 'lib/syntax_tree.rb', line 3346

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



3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
# File 'lib/syntax_tree.rb', line 3374

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



3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
# File 'lib/syntax_tree.rb', line 3396

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