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

Defined Under Namespace

Classes: Separator

Constant Summary collapse

SEPARATOR =

We’ll keep a single instance of this separator around for all block vars to cut down on allocations.

Separator.new

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(params:, locals:, location:, comments: []) ⇒ BlockVar

Returns a new instance of BlockVar.



1748
1749
1750
1751
1752
1753
# File 'lib/syntax_tree/node.rb', line 1748

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



1746
1747
1748
# File 'lib/syntax_tree/node.rb', line 1746

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



1743
1744
1745
# File 'lib/syntax_tree/node.rb', line 1743

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



1740
1741
1742
# File 'lib/syntax_tree/node.rb', line 1740

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



1755
1756
1757
# File 'lib/syntax_tree/node.rb', line 1755

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

#child_nodesObject Also known as: deconstruct



1759
1760
1761
# File 'lib/syntax_tree/node.rb', line 1759

def child_nodes
  [params, *locals]
end

#deconstruct_keys(_keys) ⇒ Object



1765
1766
1767
# File 'lib/syntax_tree/node.rb', line 1765

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

#format(q) ⇒ Object



1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
# File 'lib/syntax_tree/node.rb', line 1781

def format(q)
  q.text("|")
  q.group do
    q.remove_breaks(q.format(params))

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