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.



7589
7590
7591
7592
7593
7594
# File 'lib/syntax_tree/node.rb', line 7589

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



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

def block
  @block
end

#callObject (readonly)

ARef | CallNode | Command | CommandCall | Super | ZSuper

the method call



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

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7587
7588
7589
# File 'lib/syntax_tree/node.rb', line 7587

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



7638
7639
7640
7641
# File 'lib/syntax_tree/node.rb', line 7638

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

#accept(visitor) ⇒ Object



7596
7597
7598
# File 'lib/syntax_tree/node.rb', line 7596

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

#child_nodesObject Also known as: deconstruct



7600
7601
7602
# File 'lib/syntax_tree/node.rb', line 7600

def child_nodes
  [call, block]
end

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



7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
# File 'lib/syntax_tree/node.rb', line 7604

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



7618
7619
7620
# File 'lib/syntax_tree/node.rb', line 7618

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

#format(q) ⇒ Object



7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
# File 'lib/syntax_tree/node.rb', line 7622

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



7643
7644
7645
7646
# File 'lib/syntax_tree/node.rb', line 7643

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