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.



6287
6288
6289
6290
6291
6292
# File 'lib/syntax_tree/node.rb', line 6287

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



6282
6283
6284
# File 'lib/syntax_tree/node.rb', line 6282

def block
  @block
end

#callObject (readonly)

Call | Command | CommandCall | FCall

the method call



6279
6280
6281
# File 'lib/syntax_tree/node.rb', line 6279

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6285
6286
6287
# File 'lib/syntax_tree/node.rb', line 6285

def comments
  @comments
end

Instance Method Details

#accept(visitor) ⇒ Object



6294
6295
6296
# File 'lib/syntax_tree/node.rb', line 6294

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

#child_nodesObject Also known as: deconstruct



6298
6299
6300
# File 'lib/syntax_tree/node.rb', line 6298

def child_nodes
  [call, block]
end

#deconstruct_keys(_keys) ⇒ Object



6304
6305
6306
# File 'lib/syntax_tree/node.rb', line 6304

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

#format(q) ⇒ Object



6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
# File 'lib/syntax_tree/node.rb', line 6308

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



6324
6325
6326
6327
# File 'lib/syntax_tree/node.rb', line 6324

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