Class: Prism::ForNode

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

Overview

Represents the use of the ‘for` keyword.

for i in a end
^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc, location) ⇒ ForNode

def initialize: (index: Node, collection: Node, statements: StatementsNode?, for_keyword_loc: Location, in_keyword_loc: Location, do_keyword_loc: Location?, end_keyword_loc: Location, location: Location) -> void



6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
# File 'lib/prism/node.rb', line 6466

def initialize(index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc, location)
  @index = index
  @collection = collection
  @statements = statements
  @for_keyword_loc = for_keyword_loc
  @in_keyword_loc = in_keyword_loc
  @do_keyword_loc = do_keyword_loc
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#collectionObject (readonly)

attr_reader collection: Node



6448
6449
6450
# File 'lib/prism/node.rb', line 6448

def collection
  @collection
end

#do_keyword_locObject (readonly)

attr_reader do_keyword_loc: Location?



6460
6461
6462
# File 'lib/prism/node.rb', line 6460

def do_keyword_loc
  @do_keyword_loc
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



6463
6464
6465
# File 'lib/prism/node.rb', line 6463

def end_keyword_loc
  @end_keyword_loc
end

#for_keyword_locObject (readonly)

attr_reader for_keyword_loc: Location



6454
6455
6456
# File 'lib/prism/node.rb', line 6454

def for_keyword_loc
  @for_keyword_loc
end

#in_keyword_locObject (readonly)

attr_reader in_keyword_loc: Location



6457
6458
6459
# File 'lib/prism/node.rb', line 6457

def in_keyword_loc
  @in_keyword_loc
end

#indexObject (readonly)

attr_reader index: Node



6445
6446
6447
# File 'lib/prism/node.rb', line 6445

def index
  @index
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



6451
6452
6453
# File 'lib/prism/node.rb', line 6451

def statements
  @statements
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



6587
6588
6589
# File 'lib/prism/node.rb', line 6587

def self.type
  :for_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



6478
6479
6480
# File 'lib/prism/node.rb', line 6478

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

#child_nodesObject Also known as: deconstruct

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



6483
6484
6485
# File 'lib/prism/node.rb', line 6483

def child_nodes
  [index, collection, statements]
end

#comment_targetsObject

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



6497
6498
6499
# File 'lib/prism/node.rb', line 6497

def comment_targets
  [index, collection, *statements, for_keyword_loc, in_keyword_loc, *do_keyword_loc, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6488
6489
6490
6491
6492
6493
6494
# File 'lib/prism/node.rb', line 6488

def compact_child_nodes
  compact = []
  compact << index
  compact << collection
  compact << statements if statements
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ForNode



6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
# File 'lib/prism/node.rb', line 6502

def copy(**params)
  ForNode.new(
    params.fetch(:index) { index },
    params.fetch(:collection) { collection },
    params.fetch(:statements) { statements },
    params.fetch(:for_keyword_loc) { for_keyword_loc },
    params.fetch(:in_keyword_loc) { in_keyword_loc },
    params.fetch(:do_keyword_loc) { do_keyword_loc },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



6519
6520
6521
# File 'lib/prism/node.rb', line 6519

def deconstruct_keys(keys)
  { index: index, collection: collection, statements: statements, for_keyword_loc: for_keyword_loc, in_keyword_loc: in_keyword_loc, do_keyword_loc: do_keyword_loc, end_keyword_loc: end_keyword_loc, location: location }
end

#do_keywordObject

def do_keyword: () -> String?



6534
6535
6536
# File 'lib/prism/node.rb', line 6534

def do_keyword
  do_keyword_loc&.slice
end

#end_keywordObject

def end_keyword: () -> String



6539
6540
6541
# File 'lib/prism/node.rb', line 6539

def end_keyword
  end_keyword_loc.slice
end

#for_keywordObject

def for_keyword: () -> String



6524
6525
6526
# File 'lib/prism/node.rb', line 6524

def for_keyword
  for_keyword_loc.slice
end

#in_keywordObject

def in_keyword: () -> String



6529
6530
6531
# File 'lib/prism/node.rb', line 6529

def in_keyword
  in_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
# File 'lib/prism/node.rb', line 6544

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── index:\n"
  inspector << inspector.child_node(index, "")
  inspector << "├── collection:\n"
  inspector << inspector.child_node(collection, "")
  if (statements = self.statements).nil?
    inspector << "├── statements: ∅\n"
  else
    inspector << "├── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── for_keyword_loc: #{inspector.location(for_keyword_loc)}\n"
  inspector << "├── in_keyword_loc: #{inspector.location(in_keyword_loc)}\n"
  inspector << "├── do_keyword_loc: #{inspector.location(do_keyword_loc)}\n"
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
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



6577
6578
6579
# File 'lib/prism/node.rb', line 6577

def type
  :for_node
end