Class: Prism::ElseNode

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

Overview

Represents an ‘else` clause in a `case`, `if`, or `unless` statement.

if a then b else c end
            ^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, else_keyword_loc, statements, end_keyword_loc) ⇒ ElseNode

Initialize a new ElseNode node.



5487
5488
5489
5490
5491
5492
5493
5494
5495
# File 'lib/prism/node.rb', line 5487

def initialize(source, node_id, location, flags, else_keyword_loc, statements, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @else_keyword_loc = else_keyword_loc
  @statements = statements
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#statementsObject (readonly)

attr_reader statements: StatementsNode?



5540
5541
5542
# File 'lib/prism/node.rb', line 5540

def statements
  @statements
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



5576
5577
5578
# File 'lib/prism/node.rb', line 5576

def self.type
  :else_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



5582
5583
5584
5585
5586
5587
# File 'lib/prism/node.rb', line 5582

def ===(other)
  other.is_a?(ElseNode) &&
    (else_keyword_loc.nil? == other.else_keyword_loc.nil?) &&
    (statements === other.statements) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



5498
5499
5500
# File 'lib/prism/node.rb', line 5498

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

#child_nodesObject Also known as: deconstruct

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



5503
5504
5505
# File 'lib/prism/node.rb', line 5503

def child_nodes
  [statements]
end

#comment_targetsObject

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



5515
5516
5517
# File 'lib/prism/node.rb', line 5515

def comment_targets
  [else_keyword_loc, *statements, *end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5508
5509
5510
5511
5512
# File 'lib/prism/node.rb', line 5508

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << statements if statements
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, else_keyword_loc: self.else_keyword_loc, statements: self.statements, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?else_keyword_loc: Location, ?statements: StatementsNode?, ?end_keyword_loc: Location?) -> ElseNode



5520
5521
5522
# File 'lib/prism/node.rb', line 5520

def copy(node_id: self.node_id, location: self.location, flags: self.flags, else_keyword_loc: self.else_keyword_loc, statements: self.statements, end_keyword_loc: self.end_keyword_loc)
  ElseNode.new(source, node_id, location, flags, else_keyword_loc, statements, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, else_keyword_loc: Location, statements: StatementsNode?, end_keyword_loc: Location? }



5528
5529
5530
# File 'lib/prism/node.rb', line 5528

def deconstruct_keys(keys)
  { node_id: node_id, location: location, else_keyword_loc: else_keyword_loc, statements: statements, end_keyword_loc: end_keyword_loc }
end

#else_keywordObject

def else_keyword: () -> String



5556
5557
5558
# File 'lib/prism/node.rb', line 5556

def else_keyword
  else_keyword_loc.slice
end

#else_keyword_locObject

attr_reader else_keyword_loc: Location



5533
5534
5535
5536
5537
# File 'lib/prism/node.rb', line 5533

def else_keyword_loc
  location = @else_keyword_loc
  return location if location.is_a?(Location)
  @else_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#end_keywordObject

def end_keyword: () -> String?



5561
5562
5563
# File 'lib/prism/node.rb', line 5561

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location?



5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
# File 'lib/prism/node.rb', line 5543

def end_keyword_loc
  location = @end_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#inspectObject

def inspect -> String



5566
5567
5568
# File 'lib/prism/node.rb', line 5566

def inspect
  InspectVisitor.compose(self)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



5571
5572
5573
# File 'lib/prism/node.rb', line 5571

def type
  :else_node
end