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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Call.



2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'lib/syntax_tree/node.rb', line 2485

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



2480
2481
2482
# File 'lib/syntax_tree/node.rb', line 2480

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2483
2484
2485
# File 'lib/syntax_tree/node.rb', line 2483

def comments
  @comments
end

#messageObject (readonly)

:call | Backtick | Const | Ident | Op

the message being sent



2477
2478
2479
# File 'lib/syntax_tree/node.rb', line 2477

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used to send the message



2474
2475
2476
# File 'lib/syntax_tree/node.rb', line 2474

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the method call



2471
2472
2473
# File 'lib/syntax_tree/node.rb', line 2471

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



2501
2502
2503
# File 'lib/syntax_tree/node.rb', line 2501

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

#child_nodesObject Also known as: deconstruct



2505
2506
2507
2508
2509
2510
2511
2512
# File 'lib/syntax_tree/node.rb', line 2505

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

#deconstruct_keys(keys) ⇒ Object



2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
# File 'lib/syntax_tree/node.rb', line 2516

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

#format(q) ⇒ Object



2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
# File 'lib/syntax_tree/node.rb', line 2527

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 { q.if_break { CallChainFormatter.new(self).format(q) }.if_flat { format_contents(q) } }
  else
    format_contents(q)
  end
end

#format_arguments(q) ⇒ Object



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

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



2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
# File 'lib/syntax_tree/node.rb', line 2550

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