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.



1762
1763
1764
1765
1766
1767
1768
1769
# File 'lib/prism/node.rb', line 1762

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)
    ^^^^^


1817
1818
1819
# File 'lib/prism/node.rb', line 1817

def expression
  @expression
end

Class Method Details

.typeObject

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



1851
1852
1853
# File 'lib/prism/node.rb', line 1851

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.



1857
1858
1859
1860
1861
# File 'lib/prism/node.rb', line 1857

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



1772
1773
1774
# File 'lib/prism/node.rb', line 1772

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



1777
1778
1779
# File 'lib/prism/node.rb', line 1777

def child_nodes
  [expression]
end

#comment_targetsObject

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



1796
1797
1798
# File 'lib/prism/node.rb', line 1796

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1789
1790
1791
1792
1793
# File 'lib/prism/node.rb', line 1789

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



1801
1802
1803
# File 'lib/prism/node.rb', line 1801

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 }



1809
1810
1811
# File 'lib/prism/node.rb', line 1809

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

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

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

Yields:



1782
1783
1784
1785
1786
# File 'lib/prism/node.rb', line 1782

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield expression if expression
end

#inspectObject

def inspect -> String



1841
1842
1843
# File 'lib/prism/node.rb', line 1841

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



1836
1837
1838
# File 'lib/prism/node.rb', line 1836

def operator
  operator_loc.slice
end

#operator_locObject

Represents the location of the ‘&` operator.

foo(&args)
    ^


1823
1824
1825
1826
1827
# File 'lib/prism/node.rb', line 1823

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.



1831
1832
1833
# File 'lib/prism/node.rb', line 1831

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

#typeObject

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



1846
1847
1848
# File 'lib/prism/node.rb', line 1846

def type
  :block_argument_node
end