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.freeze

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:) ⇒ BlockVar



2090
2091
2092
2093
2094
2095
# File 'lib/syntax_tree/node.rb', line 2090

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2088
2089
2090
# File 'lib/syntax_tree/node.rb', line 2088

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2085
2086
2087
# File 'lib/syntax_tree/node.rb', line 2085

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2082
2083
2084
# File 'lib/syntax_tree/node.rb', line 2082

def params
  @params
end

Instance Method Details

#===(other) ⇒ Object



2148
2149
2150
2151
# File 'lib/syntax_tree/node.rb', line 2148

def ===(other)
  other.is_a?(BlockVar) && params === other.params &&
    ArrayMatch.call(locals, other.locals)
end

#accept(visitor) ⇒ Object



2097
2098
2099
# File 'lib/syntax_tree/node.rb', line 2097

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

#child_nodesObject Also known as: deconstruct



2101
2102
2103
# File 'lib/syntax_tree/node.rb', line 2101

def child_nodes
  [params, *locals]
end

#copy(params: nil, locals: nil, location: nil) ⇒ Object



2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
# File 'lib/syntax_tree/node.rb', line 2105

def copy(params: nil, locals: nil, location: nil)
  node =
    BlockVar.new(
      params: params || self.params,
      locals: locals || self.locals,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



2119
2120
2121
# File 'lib/syntax_tree/node.rb', line 2119

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

#format(q) ⇒ Object



2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
# File 'lib/syntax_tree/node.rb', line 2135

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