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:, block:, location:) ⇒ CommandCall

Returns a new instance of CommandCall.



3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
# File 'lib/syntax_tree/node.rb', line 3505

def initialize(
  receiver:,
  operator:,
  message:,
  arguments:,
  block:,
  location:
)
  @receiver = receiver
  @operator = operator
  @message = message
  @arguments = arguments
  @block = block
  @location = location
  @comments = []
end

Instance Attribute Details

#argumentsObject (readonly)

nil | Args

the arguments going along with the message



3497
3498
3499
# File 'lib/syntax_tree/node.rb', line 3497

def arguments
  @arguments
end

#blockObject (readonly)

nil | Block

the block associated with this method call



3500
3501
3502
# File 'lib/syntax_tree/node.rb', line 3500

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3503
3504
3505
# File 'lib/syntax_tree/node.rb', line 3503

def comments
  @comments
end

#messageObject (readonly)

Const | Ident | Op

the message being send



3494
3495
3496
# File 'lib/syntax_tree/node.rb', line 3494

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator used to send the message



3491
3492
3493
# File 'lib/syntax_tree/node.rb', line 3491

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the message



3488
3489
3490
# File 'lib/syntax_tree/node.rb', line 3488

def receiver
  @receiver
end

Instance Method Details

#===(other) ⇒ Object



3605
3606
3607
3608
3609
# File 'lib/syntax_tree/node.rb', line 3605

def ===(other)
  other.is_a?(CommandCall) && receiver === other.receiver &&
    operator === other.operator && message === other.message &&
    arguments === other.arguments && block === other.block
end

#accept(visitor) ⇒ Object



3522
3523
3524
# File 'lib/syntax_tree/node.rb', line 3522

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

#arityObject



3611
3612
3613
# File 'lib/syntax_tree/node.rb', line 3611

def arity
  arguments&.arity || 0
end

#child_nodesObject Also known as: deconstruct



3526
3527
3528
# File 'lib/syntax_tree/node.rb', line 3526

def child_nodes
  [receiver, message, arguments, block]
end

#copy(receiver: nil, operator: nil, message: nil, arguments: nil, block: nil, location: nil) ⇒ Object



3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
# File 'lib/syntax_tree/node.rb', line 3530

def copy(
  receiver: nil,
  operator: nil,
  message: nil,
  arguments: nil,
  block: nil,
  location: nil
)
  node =
    CommandCall.new(
      receiver: receiver || self.receiver,
      operator: operator || self.operator,
      message: message || self.message,
      arguments: arguments || self.arguments,
      block: block || self.block,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
# File 'lib/syntax_tree/node.rb', line 3554

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

#format(q) ⇒ Object



3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
# File 'lib/syntax_tree/node.rb', line 3566

def format(q)
  q.group do
    doc =
      q.nest(0) do
        q.format(receiver)

        # If there are leading comments on the message then we know we have
        # a newline in the source that is forcing these things apart. In
        # this case we will have to use a trailing operator.
        if message.comments.any?(&:leading?)
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.indent do
            q.breakable_empty
            q.format(message)
          end
        else
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.format(message)
        end
      end

    # Format the arguments for this command call here. If there are no
    # arguments, then print nothing.
    if arguments
      parts = arguments.parts

      if parts.length == 1 && parts.first.is_a?(IfOp)
        q.if_flat { q.text(" ") }
        q.format(arguments)
      else
        q.text(" ")
        q.nest(argument_alignment(q, doc)) { q.format(arguments) }
      end
    end
  end

  q.format(block) if block
end