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

Returns a new instance of BlockVar.



2136
2137
2138
2139
2140
2141
# File 'lib/syntax_tree/node.rb', line 2136

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



2134
2135
2136
# File 'lib/syntax_tree/node.rb', line 2134

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2131
2132
2133
# File 'lib/syntax_tree/node.rb', line 2131

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



2128
2129
2130
# File 'lib/syntax_tree/node.rb', line 2128

def params
  @params
end

Instance Method Details

#===(other) ⇒ Object



2194
2195
2196
2197
# File 'lib/syntax_tree/node.rb', line 2194

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

#accept(visitor) ⇒ Object



2143
2144
2145
# File 'lib/syntax_tree/node.rb', line 2143

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [params, *locals]
end

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



2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
# File 'lib/syntax_tree/node.rb', line 2151

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



2165
2166
2167
# File 'lib/syntax_tree/node.rb', line 2165

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

#format(q) ⇒ Object



2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
# File 'lib/syntax_tree/node.rb', line 2181

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