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.



2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
# File 'lib/syntax_tree/node.rb', line 2537

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



2532
2533
2534
# File 'lib/syntax_tree/node.rb', line 2532

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2535
2536
2537
# File 'lib/syntax_tree/node.rb', line 2535

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2529
2530
2531
# File 'lib/syntax_tree/node.rb', line 2529

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2526
2527
2528
# File 'lib/syntax_tree/node.rb', line 2526

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2523
2524
2525
# File 'lib/syntax_tree/node.rb', line 2523

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



2557
2558
2559
2560
2561
2562
2563
2564
# File 'lib/syntax_tree/node.rb', line 2557

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

#deconstruct_keys(_keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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



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

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



2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
# File 'lib/syntax_tree/node.rb', line 2607

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