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.



2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
# File 'lib/prism/node.rb', line 2113

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


2178
2179
2180
# File 'lib/prism/node.rb', line 2178

def locals
  @locals
end

#parametersObject (readonly)

Represents the parameters of the block.

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

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


2168
2169
2170
# File 'lib/prism/node.rb', line 2168

def parameters
  @parameters
end

Class Method Details

.typeObject

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



2253
2254
2255
# File 'lib/prism/node.rb', line 2253

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.



2259
2260
2261
2262
2263
2264
2265
2266
# File 'lib/prism/node.rb', line 2259

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



2125
2126
2127
# File 'lib/prism/node.rb', line 2125

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



2130
2131
2132
# File 'lib/prism/node.rb', line 2130

def child_nodes
  [parameters, *locals]
end

#closingObject

def closing: () -> String?



2238
2239
2240
# File 'lib/prism/node.rb', line 2238

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


2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
# File 'lib/prism/node.rb', line 2214

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]



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

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2135
2136
2137
2138
2139
2140
# File 'lib/prism/node.rb', line 2135

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



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

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? }



2156
2157
2158
# File 'lib/prism/node.rb', line 2156

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

#inspectObject

def inspect -> String



2243
2244
2245
# File 'lib/prism/node.rb', line 2243

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



2233
2234
2235
# File 'lib/prism/node.rb', line 2233

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


2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
# File 'lib/prism/node.rb', line 2188

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.



2228
2229
2230
# File 'lib/prism/node.rb', line 2228

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.



2202
2203
2204
# File 'lib/prism/node.rb', line 2202

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



2248
2249
2250
# File 'lib/prism/node.rb', line 2248

def type
  :block_parameters_node
end