Class: Prism::ForNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::ForNode
- 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
-
#collection ⇒ Object
readonly
attr_reader collection: Node.
-
#do_keyword_loc ⇒ Object
readonly
attr_reader do_keyword_loc: Location?.
-
#end_keyword_loc ⇒ Object
readonly
attr_reader end_keyword_loc: Location.
-
#for_keyword_loc ⇒ Object
readonly
attr_reader for_keyword_loc: Location.
-
#in_keyword_loc ⇒ Object
readonly
attr_reader in_keyword_loc: Location.
-
#index ⇒ Object
readonly
attr_reader index: Node.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
Class Method Summary collapse
-
.type ⇒ Object
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.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> ForNode.
- #deconstruct_keys(keys) ⇒ Object
-
#do_keyword ⇒ Object
def do_keyword: () -> String?.
-
#end_keyword ⇒ Object
def end_keyword: () -> String.
-
#for_keyword ⇒ Object
def for_keyword: () -> String.
-
#in_keyword ⇒ Object
def in_keyword: () -> String.
-
#initialize(index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc, location) ⇒ ForNode
constructor
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.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
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
#collection ⇒ Object (readonly)
attr_reader collection: Node
6448 6449 6450 |
# File 'lib/prism/node.rb', line 6448 def collection @collection end |
#do_keyword_loc ⇒ Object (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_loc ⇒ Object (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_loc ⇒ Object (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_loc ⇒ Object (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 |
#index ⇒ Object (readonly)
attr_reader index: Node
6445 6446 6447 |
# File 'lib/prism/node.rb', line 6445 def index @index end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
6451 6452 6453 |
# File 'lib/prism/node.rb', line 6451 def statements @statements end |
Class Method Details
.type ⇒ Object
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_nodes ⇒ Object 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_targets ⇒ Object
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_nodes ⇒ Object
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
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_keyword ⇒ Object
def do_keyword: () -> String?
6534 6535 6536 |
# File 'lib/prism/node.rb', line 6534 def do_keyword do_keyword_loc&.slice end |
#end_keyword ⇒ Object
def end_keyword: () -> String
6539 6540 6541 |
# File 'lib/prism/node.rb', line 6539 def end_keyword end_keyword_loc.slice end |
#for_keyword ⇒ Object
def for_keyword: () -> String
6524 6525 6526 |
# File 'lib/prism/node.rb', line 6524 def for_keyword for_keyword_loc.slice end |
#in_keyword ⇒ Object
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 |
#type ⇒ Object
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 |