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.



2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
# File 'lib/syntax_tree/node.rb', line 2551

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



2546
2547
2548
# File 'lib/syntax_tree/node.rb', line 2546

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2543
2544
2545
# File 'lib/syntax_tree/node.rb', line 2543

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2540
2541
2542
# File 'lib/syntax_tree/node.rb', line 2540

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2537
2538
2539
# File 'lib/syntax_tree/node.rb', line 2537

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



2567
2568
2569
# File 'lib/syntax_tree/node.rb', line 2567

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

#child_nodesObject Also known as: deconstruct



2571
2572
2573
2574
2575
2576
2577
2578
# File 'lib/syntax_tree/node.rb', line 2571

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

#deconstruct_keys(_keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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



2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
# File 'lib/syntax_tree/node.rb', line 2609

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



2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
# File 'lib/syntax_tree/node.rb', line 2621

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