Class: Prism::IfNode

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

Overview

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

bar if foo
^^^^^^^^^^

if foo then bar end
^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(if_keyword_loc, predicate, statements, consequent, end_keyword_loc, location) ⇒ IfNode

def initialize: (if_keyword_loc: Location?, predicate: Node, statements: StatementsNode?, consequent: Node?, end_keyword_loc: Location?, location: Location) -> void



6774
6775
6776
6777
6778
6779
6780
6781
# File 'lib/prism/node.rb', line 6774

def initialize(if_keyword_loc, predicate, statements, consequent, end_keyword_loc, location)
  @if_keyword_loc = if_keyword_loc
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#consequentObject (readonly)

attr_reader consequent: Node?



6768
6769
6770
# File 'lib/prism/node.rb', line 6768

def consequent
  @consequent
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location?



6771
6772
6773
# File 'lib/prism/node.rb', line 6771

def end_keyword_loc
  @end_keyword_loc
end

#if_keyword_locObject (readonly)

attr_reader if_keyword_loc: Location?



6759
6760
6761
# File 'lib/prism/node.rb', line 6759

def if_keyword_loc
  @if_keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node



6762
6763
6764
# File 'lib/prism/node.rb', line 6762

def predicate
  @predicate
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



6765
6766
6767
# File 'lib/prism/node.rb', line 6765

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



6784
6785
6786
# File 'lib/prism/node.rb', line 6784

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

#child_nodesObject Also known as: deconstruct

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



6793
6794
6795
# File 'lib/prism/node.rb', line 6793

def child_nodes
  [predicate, statements, consequent]
end

#comment_targetsObject

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



6807
6808
6809
# File 'lib/prism/node.rb', line 6807

def comment_targets
  [*if_keyword_loc, predicate, *statements, *consequent, *end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6798
6799
6800
6801
6802
6803
6804
# File 'lib/prism/node.rb', line 6798

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

#copy(**params) ⇒ Object

def copy: (**params) -> IfNode



6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
# File 'lib/prism/node.rb', line 6812

def copy(**params)
  IfNode.new(
    params.fetch(:if_keyword_loc) { if_keyword_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:statements) { statements },
    params.fetch(:consequent) { consequent },
    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]



6827
6828
6829
# File 'lib/prism/node.rb', line 6827

def deconstruct_keys(keys)
  { if_keyword_loc: if_keyword_loc, predicate: predicate, statements: statements, consequent: consequent, end_keyword_loc: end_keyword_loc, location: location }
end

#end_keywordObject

def end_keyword: () -> String?



6837
6838
6839
# File 'lib/prism/node.rb', line 6837

def end_keyword
  end_keyword_loc&.slice
end

#if_keywordObject

def if_keyword: () -> String?



6832
6833
6834
# File 'lib/prism/node.rb', line 6832

def if_keyword
  if_keyword_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
# File 'lib/prism/node.rb', line 6841

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── if_keyword_loc: #{inspector.location(if_keyword_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
  if (consequent = self.consequent).nil?
    inspector << "├── consequent: ∅\n"
  else
    inspector << "├── consequent:\n"
    inspector << consequent.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

#set_newline_flag(newline_marked) ⇒ Object



6788
6789
6790
# File 'lib/prism/node.rb', line 6788

def set_newline_flag(newline_marked)
  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



6876
6877
6878
# File 'lib/prism/node.rb', line 6876

def type
  :if_node
end