Class: Prism::ArrayNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c

Overview

Represents an array literal. This can be a regular array using brackets or a special array using % like %w or %i.

[1, 2, 3]
^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, elements, opening_loc, closing_loc) ⇒ ArrayNode

Initialize a new ArrayNode node.

: (Source source, Integer node_id, Location location, Integer flags, Array elements, Location? opening_loc, Location? closing_loc) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • elements (Array[Prism::node])
  • opening_loc (Location, nil)
  • closing_loc (Location, nil)


1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'lib/prism/node.rb', line 1447

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

Class Method Details

.type ⇒ :array_node

See Node.type.

: () -> :array_node

Returns:

  • (:array_node)


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

def self.type
  :array_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


1664
1665
1666
1667
1668
1669
1670
1671
# File 'lib/prism/node.rb', line 1664

def ===(other) # :nodoc:
  other.is_a?(ArrayNode) &&
    (flags === other.flags) &&
    (elements.length == other.elements.length) &&
    elements.zip(other.elements).all? { |left, right| left === right } &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


1471
1472
1473
# File 'lib/prism/node.rb', line 1471

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


1478
1479
1480
# File 'lib/prism/node.rb', line 1478

def child_nodes
  [*elements]
end

#closing ⇒ String?

:call-seq:

closing -> String | nil

Slice the location of closing_loc from the source.

: () -> String?

Returns:

  • (String, nil)


1657
1658
1659
# File 'lib/prism/node.rb', line 1657

def closing
  closing_loc&.slice
end

#closing_loc ⇒ Location?

:category: Locations :call-seq:

closing_loc -> Location | nil

Represents the optional source Location for the closing token.

[1,2,3]                 # "]"
%w[foo bar baz]         # "]"
%I(apple orange banana) # ")"
foo = 1, 2, 3           # nil

Explore code sample

: () -> Location?

Returns:



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
# File 'lib/prism/node.rb', line 1619

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


1495
1496
1497
# File 'lib/prism/node.rb', line 1495

def compact_child_nodes
  [*elements]
end

#contains_splat? ⇒ Boolean

:category: Flags if array contains splat nodes

: () -> bool

Returns:

  • (Boolean)


1553
1554
1555
# File 'lib/prism/node.rb', line 1553

def contains_splat?
  flags.anybits?(ArrayNodeFlags::CONTAINS_SPLAT)
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ ArrayNode

:call-seq:

copy(**fields) -> ArrayNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?elements: Array, ?opening_loc: Location?, ?closing_loc: Location?) -> ArrayNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • elements: (Array[Prism::node]) (defaults to: self.elements)
  • opening_loc: (Location, nil) (defaults to: self.opening_loc)
  • closing_loc: (Location, nil) (defaults to: self.closing_loc)

Returns:



1505
1506
1507
# File 'lib/prism/node.rb', line 1505

def copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  ArrayNode.new(source, node_id, location, flags, elements, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
# File 'lib/prism/node.rb', line 1512

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location elements opening_loc closing_loc opening closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :elements                then deconstructed[:elements] = self.elements
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


1486
1487
1488
1489
1490
# File 'lib/prism/node.rb', line 1486

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  elements.each { |node| yield node }
end

#elements ⇒ Array[Prism::node]

:call-seq:

elements -> Array[Node]

Represent the list of zero or more non-void expressions within the array.

: () -> Array

Returns:

  • (Array[Prism::node])


1563
1564
1565
# File 'lib/prism/node.rb', line 1563

def elements
  @elements
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


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

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


358
359
360
361
362
363
364
365
# File 'lib/prism/parse_result/newlines.rb', line 358

def newline_flag!(lines) # :nodoc:
  first = elements.first
  if first && !static_literal?
    first.newline_flag!(lines)
  else
    super
  end
end

#opening ⇒ String?

:call-seq:

opening -> String | nil

Slice the location of opening_loc from the source.

: () -> String?

Returns:

  • (String, nil)


1647
1648
1649
# File 'lib/prism/node.rb', line 1647

def opening
  opening_loc&.slice
end

#opening_loc ⇒ Location?

:category: Locations :call-seq:

opening_loc -> Location | nil

Represents the optional source Location for the opening token.

[1,2,3]                 # "["
%w[foo bar baz]         # "%w["
%I(apple orange banana) # "%I("
foo = 1, 2, 3           # nil

Explore code sample

: () -> Location?

Returns:



1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
# File 'lib/prism/node.rb', line 1583

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) ⇒ Relocation::Entry?

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

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



1636
1637
1638
# File 'lib/prism/node.rb', line 1636

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

#save_opening_loc(repository) ⇒ Relocation::Entry?

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

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



1600
1601
1602
# File 'lib/prism/node.rb', line 1600

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

#type ⇒ :array_node

See Node#type.

: () -> :array_node

Returns:

  • (:array_node)


1531
1532
1533
# File 'lib/prism/node.rb', line 1531

def type
  :array_node
end