Class: Prism::BlockArgumentNode

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

Overview

Represents a block argument using ‘&`.

bar(&args)
^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, expression, operator_loc) ⇒ BlockArgumentNode

Initialize a new BlockArgumentNode node.



1660
1661
1662
1663
1664
1665
1666
1667
# File 'lib/prism/node.rb', line 1660

def initialize(source, node_id, location, flags, expression, operator_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @expression = expression
  @operator_loc = operator_loc
end

Instance Attribute Details

#expressionObject (readonly)

The expression that is being passed as a block argument. This can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

foo(&args)
    ^^^^^


1708
1709
1710
# File 'lib/prism/node.rb', line 1708

def expression
  @expression
end

Class Method Details

.typeObject

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



1742
1743
1744
# File 'lib/prism/node.rb', line 1742

def self.type
  :block_argument_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.



1748
1749
1750
1751
1752
# File 'lib/prism/node.rb', line 1748

def ===(other)
  other.is_a?(BlockArgumentNode) &&
    (expression === other.expression) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



1670
1671
1672
# File 'lib/prism/node.rb', line 1670

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



1675
1676
1677
# File 'lib/prism/node.rb', line 1675

def child_nodes
  [expression]
end

#comment_targetsObject

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



1687
1688
1689
# File 'lib/prism/node.rb', line 1687

def comment_targets
  [*expression, operator_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1680
1681
1682
1683
1684
# File 'lib/prism/node.rb', line 1680

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << expression if expression
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, operator_loc: self.operator_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?expression: Prism::node?, ?operator_loc: Location) -> BlockArgumentNode



1692
1693
1694
# File 'lib/prism/node.rb', line 1692

def copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, operator_loc: self.operator_loc)
  BlockArgumentNode.new(source, node_id, location, flags, expression, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, expression: Prism::node?, operator_loc: Location }



1700
1701
1702
# File 'lib/prism/node.rb', line 1700

def deconstruct_keys(keys)
  { node_id: node_id, location: location, expression: expression, operator_loc: operator_loc }
end

#inspectObject

def inspect -> String



1732
1733
1734
# File 'lib/prism/node.rb', line 1732

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



1727
1728
1729
# File 'lib/prism/node.rb', line 1727

def operator
  operator_loc.slice
end

#operator_locObject

Represents the location of the ‘&` operator.

foo(&args)
    ^


1714
1715
1716
1717
1718
# File 'lib/prism/node.rb', line 1714

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_operator_loc(repository) ⇒ Object

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



1722
1723
1724
# File 'lib/prism/node.rb', line 1722

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

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



1737
1738
1739
# File 'lib/prism/node.rb', line 1737

def type
  :block_argument_node
end