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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(params:, locals:, location:) ⇒ BlockVar

Returns a new instance of BlockVar.



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

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



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

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



2141
2142
2143
# File 'lib/syntax_tree/node.rb', line 2141

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



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

def params
  @params
end

Instance Method Details

#===(other) ⇒ Object



2204
2205
2206
2207
# File 'lib/syntax_tree/node.rb', line 2204

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

#accept(visitor) ⇒ Object



2153
2154
2155
# File 'lib/syntax_tree/node.rb', line 2153

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

#arg0?Boolean

When a single required parameter is declared for a block, it gets automatically expanded if the values being yielded into it are an array.

Returns:

  • (Boolean)


2211
2212
2213
2214
2215
# File 'lib/syntax_tree/node.rb', line 2211

def arg0?
  params.requireds.length == 1 && params.optionals.empty? &&
    params.rest.nil? && params.posts.empty? && params.keywords.empty? &&
    params.keyword_rest.nil? && params.block.nil?
end

#child_nodesObject Also known as: deconstruct



2157
2158
2159
# File 'lib/syntax_tree/node.rb', line 2157

def child_nodes
  [params, *locals]
end

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



2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
# File 'lib/syntax_tree/node.rb', line 2161

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



2175
2176
2177
# File 'lib/syntax_tree/node.rb', line 2175

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

#format(q) ⇒ Object



2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
# File 'lib/syntax_tree/node.rb', line 2191

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