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.



1668
1669
1670
1671
1672
1673
# File 'lib/syntax_tree/node.rb', line 1668

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



1666
1667
1668
# File 'lib/syntax_tree/node.rb', line 1666

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



1663
1664
1665
# File 'lib/syntax_tree/node.rb', line 1663

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



1660
1661
1662
# File 'lib/syntax_tree/node.rb', line 1660

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



1675
1676
1677
# File 'lib/syntax_tree/node.rb', line 1675

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

#child_nodesObject Also known as: deconstruct



1679
1680
1681
# File 'lib/syntax_tree/node.rb', line 1679

def child_nodes
  [params, *locals]
end

#deconstruct_keys(keys) ⇒ Object



1685
1686
1687
# File 'lib/syntax_tree/node.rb', line 1685

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

#format(q) ⇒ Object



1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
# File 'lib/syntax_tree/node.rb', line 1689

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