Class: SyntaxTree::BlockVar

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

Overview

BlockVar represents the parameters being declared for a block. Effectively this node is everything contained within the pipes. This includes all of the various parameter types, as well as block-local variable declarations.

method do |positional, optional = value, keyword:, █ local|
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(params:, locals:, location:, comments: []) ⇒ BlockVar

Returns a new instance of BlockVar.



1620
1621
1622
1623
1624
1625
# File 'lib/syntax_tree/node.rb', line 1620

def initialize(params:, locals:, location:, comments: [])
  @params = params
  @locals = locals
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1618
1619
1620
# File 'lib/syntax_tree/node.rb', line 1618

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



1615
1616
1617
# File 'lib/syntax_tree/node.rb', line 1615

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



1612
1613
1614
# File 'lib/syntax_tree/node.rb', line 1612

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



1627
1628
1629
# File 'lib/syntax_tree/node.rb', line 1627

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

#child_nodesObject Also known as: deconstruct



1631
1632
1633
# File 'lib/syntax_tree/node.rb', line 1631

def child_nodes
  [params, *locals]
end

#deconstruct_keys(keys) ⇒ Object



1637
1638
1639
# File 'lib/syntax_tree/node.rb', line 1637

def deconstruct_keys(keys)
  { params: params, locals: locals, location: location, comments: comments }
end

#format(q) ⇒ Object



1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
# File 'lib/syntax_tree/node.rb', line 1641

def format(q)
  q.group(0, "|", "|") do
    doc = q.format(params)
    RemoveBreaks.call(doc)

    if locals.any?
      q.text("; ")
      q.seplist(locals, -> { q.text(", ") }) { |local| q.format(local) }
    end
  end
end