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, #pretty_print, #to_json

Constructor Details

#initialize(call:, block:, location:, comments: []) ⇒ MethodAddBlock

Returns a new instance of MethodAddBlock.



6123
6124
6125
6126
6127
6128
# File 'lib/syntax_tree/node.rb', line 6123

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

Instance Attribute Details

#blockObject (readonly)

BraceBlock | DoBlock

the block being sent with the method call



6118
6119
6120
# File 'lib/syntax_tree/node.rb', line 6118

def block
  @block
end

#callObject (readonly)

Call | Command | CommandCall | FCall

the method call



6115
6116
6117
# File 'lib/syntax_tree/node.rb', line 6115

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6121
6122
6123
# File 'lib/syntax_tree/node.rb', line 6121

def comments
  @comments
end

Instance Method Details

#accept(visitor) ⇒ Object



6130
6131
6132
# File 'lib/syntax_tree/node.rb', line 6130

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

#child_nodesObject Also known as: deconstruct



6134
6135
6136
# File 'lib/syntax_tree/node.rb', line 6134

def child_nodes
  [call, block]
end

#deconstruct_keys(_keys) ⇒ Object



6140
6141
6142
# File 'lib/syntax_tree/node.rb', line 6140

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

#format(q) ⇒ Object



6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
# File 'lib/syntax_tree/node.rb', line 6144

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?(call)
    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



6160
6161
6162
6163
# File 'lib/syntax_tree/node.rb', line 6160

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