Class: Prism::BlockParametersNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a block’s parameters declaration.

-> (a, b = 1; local) { }
   ^^^^^^^^^^^^^^^^^

foo do |a, b = 1; local|
       ^^^^^^^^^^^^^^^^^
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, parameters, locals, opening_loc, closing_loc) ⇒ BlockParametersNode

Initialize a new BlockParametersNode node.



2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
# File 'lib/prism/node.rb', line 2254

def initialize(source, node_id, location, flags, parameters, locals, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @parameters = parameters
  @locals = locals
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Instance Attribute Details

#localsObject (readonly)

Represents the local variables of the block.

-> (a, b = 1; local) { }
              ^^^^^

foo do |a, b = 1; local|
                  ^^^^^
end


2327
2328
2329
# File 'lib/prism/node.rb', line 2327

def locals
  @locals
end

#parametersObject (readonly)

Represents the parameters of the block.

-> (a, b = 1; local) { }
    ^^^^^^^^

foo do |a, b = 1; local|
        ^^^^^^^^
end


2317
2318
2319
# File 'lib/prism/node.rb', line 2317

def parameters
  @parameters
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See Node::type.



2402
2403
2404
# File 'lib/prism/node.rb', line 2402

def self.type
  :block_parameters_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



2408
2409
2410
2411
2412
2413
2414
2415
# File 'lib/prism/node.rb', line 2408

def ===(other)
  other.is_a?(BlockParametersNode) &&
    (parameters === other.parameters) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



2266
2267
2268
# File 'lib/prism/node.rb', line 2266

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



2271
2272
2273
# File 'lib/prism/node.rb', line 2271

def child_nodes
  [parameters, *locals]
end

#closingObject

def closing: () -> String?



2387
2388
2389
# File 'lib/prism/node.rb', line 2387

def closing
  closing_loc&.slice
end

#closing_locObject

Represents the closing location of the block parameters.

-> (a, b = 1; local) { }
                   ^

foo do |a, b = 1; local|
                       ^
end


2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
# File 'lib/prism/node.rb', line 2363

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



2292
2293
2294
# File 'lib/prism/node.rb', line 2292

def comment_targets
  [*parameters, *locals, *opening_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2284
2285
2286
2287
2288
2289
# File 'lib/prism/node.rb', line 2284

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << parameters if parameters
  compact.concat(locals)
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, parameters: self.parameters, locals: self.locals, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?parameters: ParametersNode?, ?locals: Array, ?opening_loc: Location?, ?closing_loc: Location?) -> BlockParametersNode



2297
2298
2299
# File 'lib/prism/node.rb', line 2297

def copy(node_id: self.node_id, location: self.location, flags: self.flags, parameters: self.parameters, locals: self.locals, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  BlockParametersNode.new(source, node_id, location, flags, parameters, locals, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, parameters: ParametersNode?, locals: Array, opening_loc: Location?, closing_loc: Location? }



2305
2306
2307
# File 'lib/prism/node.rb', line 2305

def deconstruct_keys(keys)
  { node_id: node_id, location: location, parameters: parameters, locals: locals, opening_loc: opening_loc, closing_loc: closing_loc }
end

#each_child_node {|parameters| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



2276
2277
2278
2279
2280
2281
# File 'lib/prism/node.rb', line 2276

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield parameters if parameters
  locals.each { |node| yield node }
end

#inspectObject

def inspect -> String



2392
2393
2394
# File 'lib/prism/node.rb', line 2392

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



2382
2383
2384
# File 'lib/prism/node.rb', line 2382

def opening
  opening_loc&.slice
end

#opening_locObject

Represents the opening location of the block parameters.

-> (a, b = 1; local) { }
   ^

foo do |a, b = 1; local|
       ^
end


2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
# File 'lib/prism/node.rb', line 2337

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#save_closing_loc(repository) ⇒ Object

Save the closing_loc location using the given saved source so that it can be retrieved later.



2377
2378
2379
# File 'lib/prism/node.rb', line 2377

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_opening_loc(repository) ⇒ Object

Save the opening_loc location using the given saved source so that it can be retrieved later.



2351
2352
2353
# File 'lib/prism/node.rb', line 2351

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



2397
2398
2399
# File 'lib/prism/node.rb', line 2397

def type
  :block_parameters_node
end