Class: Prism::UntilNode

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

Overview

Represents the use of the ‘until` keyword, either in the block form or the modifier form.

bar until foo
^^^^^^^^^^^^^

until foo do bar end
^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, keyword_loc, closing_loc, predicate, statements, location) ⇒ UntilNode

def initialize: (flags: Integer, keyword_loc: Location, closing_loc: Location?, predicate: Node, statements: StatementsNode?, location: Location) -> void



16641
16642
16643
16644
16645
16646
16647
16648
# File 'lib/prism/node.rb', line 16641

def initialize(flags, keyword_loc, closing_loc, predicate, statements, location)
  @flags = flags
  @keyword_loc = keyword_loc
  @closing_loc = closing_loc
  @predicate = predicate
  @statements = statements
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



16632
16633
16634
# File 'lib/prism/node.rb', line 16632

def closing_loc
  @closing_loc
end

#flagsObject (readonly)

Returns the value of attribute flags.



16626
16627
16628
# File 'lib/prism/node.rb', line 16626

def flags
  @flags
end

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



16629
16630
16631
# File 'lib/prism/node.rb', line 16629

def keyword_loc
  @keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node



16635
16636
16637
# File 'lib/prism/node.rb', line 16635

def predicate
  @predicate
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



16638
16639
16640
# File 'lib/prism/node.rb', line 16638

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



16754
16755
16756
# File 'lib/prism/node.rb', line 16754

def self.type
  :until_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



16651
16652
16653
# File 'lib/prism/node.rb', line 16651

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

#begin_modifier?Boolean

def begin_modifier?: () -> bool

Returns:

  • (Boolean)


16698
16699
16700
# File 'lib/prism/node.rb', line 16698

def begin_modifier?
  flags.anybits?(LoopFlags::BEGIN_MODIFIER)
end

#child_nodesObject Also known as: deconstruct

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



16660
16661
16662
# File 'lib/prism/node.rb', line 16660

def child_nodes
  [predicate, statements]
end

#closingObject

def closing: () -> String?



16708
16709
16710
# File 'lib/prism/node.rb', line 16708

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



16673
16674
16675
# File 'lib/prism/node.rb', line 16673

def comment_targets
  [keyword_loc, *closing_loc, predicate, *statements]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



16665
16666
16667
16668
16669
16670
# File 'lib/prism/node.rb', line 16665

def compact_child_nodes
  compact = []
  compact << predicate
  compact << statements if statements
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> UntilNode



16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
# File 'lib/prism/node.rb', line 16678

def copy(**params)
  UntilNode.new(
    params.fetch(:flags) { flags },
    params.fetch(:keyword_loc) { keyword_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:statements) { statements },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



16693
16694
16695
# File 'lib/prism/node.rb', line 16693

def deconstruct_keys(keys)
  { flags: flags, keyword_loc: keyword_loc, closing_loc: closing_loc, predicate: predicate, statements: statements, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
# File 'lib/prism/node.rb', line 16713

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  flags = [("begin_modifier" if begin_modifier?)].compact
  inspector << "├── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n"
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector << "├── predicate:\n"
  inspector << inspector.child_node(predicate, "")
  if (statements = self.statements).nil?
    inspector << "└── statements: ∅\n"
  else
    inspector << "└── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("    ")).delete_prefix(inspector.prefix)
  end
  inspector.to_str
end

#keywordObject

def keyword: () -> String



16703
16704
16705
# File 'lib/prism/node.rb', line 16703

def keyword
  keyword_loc.slice
end

#set_newline_flag(newline_marked) ⇒ Object

:nodoc:



16655
16656
16657
# File 'lib/prism/node.rb', line 16655

def set_newline_flag(newline_marked) # :nodoc:
  predicate.set_newline_flag(newline_marked)
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



16744
16745
16746
# File 'lib/prism/node.rb', line 16744

def type
  :until_node
end