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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Call.



2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
# File 'lib/syntax_tree/node.rb', line 2562

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



2557
2558
2559
# File 'lib/syntax_tree/node.rb', line 2557

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2560
2561
2562
# File 'lib/syntax_tree/node.rb', line 2560

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2554
2555
2556
# File 'lib/syntax_tree/node.rb', line 2554

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2551
2552
2553
# File 'lib/syntax_tree/node.rb', line 2551

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2548
2549
2550
# File 'lib/syntax_tree/node.rb', line 2548

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



2578
2579
2580
# File 'lib/syntax_tree/node.rb', line 2578

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

#child_nodesObject Also known as: deconstruct



2582
2583
2584
2585
2586
2587
2588
2589
# File 'lib/syntax_tree/node.rb', line 2582

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

#deconstruct_keys(_keys) ⇒ Object



2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
# File 'lib/syntax_tree/node.rb', line 2593

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

#format(q) ⇒ Object



2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
# File 'lib/syntax_tree/node.rb', line 2604

def format(q)
  # If we're at the top of a call chain, then we're going to do some
  # specialized printing in case we can print it nicely. We _only_ do this
  # at the top of the chain to avoid weird recursion issues.
  if !CallChainFormatter.chained?(q.parent) &&
       CallChainFormatter.chained?(receiver)
    q.group do
      q
        .if_break { CallChainFormatter.new(self).format(q) }
        .if_flat { format_contents(q) }
    end
  else
    format_contents(q)
  end
end

#format_arguments(q) ⇒ Object



2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
# File 'lib/syntax_tree/node.rb', line 2620

def format_arguments(q)
  case arguments
  in ArgParen
    q.format(arguments)
  in Args
    q.text(" ")
    q.format(arguments)
  else
    # Do nothing if there are no arguments.
  end
end

#format_contents(q) ⇒ Object



2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
# File 'lib/syntax_tree/node.rb', line 2632

def format_contents(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

      format_arguments(q)
    end
  end
end