Class: SyntaxTree::MethodAddBlock

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

MethodAddBlock represents a method call with a block argument.

method {}

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(call:, block:, location:) ⇒ MethodAddBlock

Returns a new instance of MethodAddBlock.



7573
7574
7575
7576
7577
7578
# File 'lib/syntax_tree/node.rb', line 7573

def initialize(call:, block:, location:)
  @call = call
  @block = block
  @location = location
  @comments = []
end

Instance Attribute Details

#blockObject (readonly)

BlockNode

the block being sent with the method call



7568
7569
7570
# File 'lib/syntax_tree/node.rb', line 7568

def block
  @block
end

#callObject (readonly)

ARef | CallNode | Command | CommandCall | Super | ZSuper

the method call



7565
7566
7567
# File 'lib/syntax_tree/node.rb', line 7565

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7571
7572
7573
# File 'lib/syntax_tree/node.rb', line 7571

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



7622
7623
7624
7625
# File 'lib/syntax_tree/node.rb', line 7622

def ===(other)
  other.is_a?(MethodAddBlock) && call === other.call &&
    block === other.block
end

#accept(visitor) ⇒ Object



7580
7581
7582
# File 'lib/syntax_tree/node.rb', line 7580

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

#child_nodesObject Also known as: deconstruct



7584
7585
7586
# File 'lib/syntax_tree/node.rb', line 7584

def child_nodes
  [call, block]
end

#copy(call: nil, block: nil, location: nil) ⇒ Object



7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
# File 'lib/syntax_tree/node.rb', line 7588

def copy(call: nil, block: nil, location: nil)
  node =
    MethodAddBlock.new(
      call: call || self.call,
      block: block || self.block,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



7602
7603
7604
# File 'lib/syntax_tree/node.rb', line 7602

def deconstruct_keys(_keys)
  { call: call, block: block, location: location, comments: comments }
end

#format(q) ⇒ Object



7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
# File 'lib/syntax_tree/node.rb', line 7606

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?(call) &&
       !CallChainFormatter.chained?(q.parent)
    q.group do
      q
        .if_break { CallChainFormatter.new(self).format(q) }
        .if_flat { format_contents(q) }
    end
  else
    format_contents(q)
  end
end

#format_contents(q) ⇒ Object



7627
7628
7629
7630
# File 'lib/syntax_tree/node.rb', line 7627

def format_contents(q)
  q.format(call)
  q.format(block)
end