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

Instance Method Summary collapse

Constructor Details

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

def initialize: (parameters: ParametersNode?, locals: Array, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



1546
1547
1548
1549
1550
1551
1552
# File 'lib/prism/node.rb', line 1546

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

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



1543
1544
1545
# File 'lib/prism/node.rb', line 1543

def closing_loc
  @closing_loc
end

#localsObject (readonly)

attr_reader locals: Array



1537
1538
1539
# File 'lib/prism/node.rb', line 1537

def locals
  @locals
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



1540
1541
1542
# File 'lib/prism/node.rb', line 1540

def opening_loc
  @opening_loc
end

#parametersObject (readonly)

attr_reader parameters: ParametersNode?



1534
1535
1536
# File 'lib/prism/node.rb', line 1534

def parameters
  @parameters
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1555
1556
1557
# File 'lib/prism/node.rb', line 1555

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

#child_nodesObject Also known as: deconstruct

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



1560
1561
1562
# File 'lib/prism/node.rb', line 1560

def child_nodes
  [parameters, *locals]
end

#closingObject

def closing: () -> String?



1602
1603
1604
# File 'lib/prism/node.rb', line 1602

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



1573
1574
1575
# File 'lib/prism/node.rb', line 1573

def comment_targets
  [*parameters, *locals, *opening_loc, *closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1565
1566
1567
1568
1569
1570
# File 'lib/prism/node.rb', line 1565

def compact_child_nodes
  compact = []
  compact << parameters if parameters
  compact.concat(locals)
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> BlockParametersNode



1578
1579
1580
1581
1582
1583
1584
1585
1586
# File 'lib/prism/node.rb', line 1578

def copy(**params)
  BlockParametersNode.new(
    params.fetch(:parameters) { parameters },
    params.fetch(:locals) { locals },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



1592
1593
1594
# File 'lib/prism/node.rb', line 1592

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

#inspect(inspector = NodeInspector.new) ⇒ Object



1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
# File 'lib/prism/node.rb', line 1606

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (parameters = self.parameters).nil?
    inspector << "├── parameters: ∅\n"
  else
    inspector << "├── parameters:\n"
    inspector << parameters.inspect(inspector.child_inspector("│   ")).delete_prefix(inspector.prefix)
  end
  inspector << "├── locals: #{inspector.list("#{inspector.prefix}│   ", locals)}"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



1597
1598
1599
# File 'lib/prism/node.rb', line 1597

def opening
  opening_loc&.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



1634
1635
1636
# File 'lib/prism/node.rb', line 1634

def type
  :block_parameters_node
end