Class: Prism::CaseNode

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

Overview

Represents the use of a case statement.

case true
when false
end
^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc) ⇒ CaseNode

Initialize a new CaseNode node.



3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
# File 'lib/prism/node.rb', line 3916

def initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @predicate = predicate
  @conditions = conditions
  @else_clause = else_clause
  @case_keyword_loc = case_keyword_loc
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#conditionsObject (readonly)

Represents the conditions of the case statement.

case true; when false; end
           ^^^^^^^^^^


3984
3985
3986
# File 'lib/prism/node.rb', line 3984

def conditions
  @conditions
end

#else_clauseObject (readonly)

Represents the else clause of the case statement.

case true; when false; else; end
                       ^^^^


3990
3991
3992
# File 'lib/prism/node.rb', line 3990

def else_clause
  @else_clause
end

#predicateObject (readonly)

Represents the predicate of the case statement. This can be either nil or any [non-void expressions](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

case true; when false; end
     ^^^^


3978
3979
3980
# File 'lib/prism/node.rb', line 3978

def predicate
  @predicate
end

Class Method Details

.typeObject

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



4045
4046
4047
# File 'lib/prism/node.rb', line 4045

def self.type
  :case_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.



4051
4052
4053
4054
4055
4056
4057
4058
4059
# File 'lib/prism/node.rb', line 4051

def ===(other)
  other.is_a?(CaseNode) &&
    (predicate === other.predicate) &&
    (conditions.length == other.conditions.length) &&
    conditions.zip(other.conditions).all? { |left, right| left === right } &&
    (else_clause === other.else_clause) &&
    (case_keyword_loc.nil? == other.case_keyword_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



3929
3930
3931
# File 'lib/prism/node.rb', line 3929

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

#case_keywordObject

def case_keyword: () -> String



4025
4026
4027
# File 'lib/prism/node.rb', line 4025

def case_keyword
  case_keyword_loc.slice
end

#case_keyword_locObject

Represents the location of the case keyword.

case true; when false; end
^^^^


3996
3997
3998
3999
4000
# File 'lib/prism/node.rb', line 3996

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



3934
3935
3936
# File 'lib/prism/node.rb', line 3934

def child_nodes
  [predicate, *conditions, else_clause]
end

#comment_targetsObject

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



3957
3958
3959
# File 'lib/prism/node.rb', line 3957

def comment_targets
  [*predicate, *conditions, *else_clause, case_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3948
3949
3950
3951
3952
3953
3954
# File 'lib/prism/node.rb', line 3948

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << predicate if predicate
  compact.concat(conditions)
  compact << else_clause if else_clause
  compact
end

#consequentObject

Returns the else clause of the case node. This method is deprecated in favor of #else_clause.



479
480
481
482
# File 'lib/prism/node_ext.rb', line 479

def consequent
  deprecated("else_clause")
  else_clause
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, predicate: self.predicate, conditions: self.conditions, else_clause: self.else_clause, case_keyword_loc: self.case_keyword_loc, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?predicate: Prism::node?, ?conditions: Array, ?else_clause: ElseNode?, ?case_keyword_loc: Location, ?end_keyword_loc: Location) -> CaseNode



3962
3963
3964
# File 'lib/prism/node.rb', line 3962

def copy(node_id: self.node_id, location: self.location, flags: self.flags, predicate: self.predicate, conditions: self.conditions, else_clause: self.else_clause, case_keyword_loc: self.case_keyword_loc, end_keyword_loc: self.end_keyword_loc)
  CaseNode.new(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, predicate: Prism::node?, conditions: Array, else_clause: ElseNode?, case_keyword_loc: Location, end_keyword_loc: Location }



3970
3971
3972
# File 'lib/prism/node.rb', line 3970

def deconstruct_keys(keys)
  { node_id: node_id, location: location, predicate: predicate, conditions: conditions, else_clause: else_clause, case_keyword_loc: case_keyword_loc, end_keyword_loc: end_keyword_loc }
end

#each_child_node {|predicate| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



3939
3940
3941
3942
3943
3944
3945
# File 'lib/prism/node.rb', line 3939

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield predicate if predicate
  conditions.each { |node| yield node }
  yield else_clause if else_clause
end

#end_keywordObject

def end_keyword: () -> String



4030
4031
4032
# File 'lib/prism/node.rb', line 4030

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

Represents the location of the end keyword.

case true; when false; end
                       ^^^


4012
4013
4014
4015
4016
# File 'lib/prism/node.rb', line 4012

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

#inspectObject

def inspect -> String



4035
4036
4037
# File 'lib/prism/node.rb', line 4035

def inspect
  InspectVisitor.compose(self)
end

#save_case_keyword_loc(repository) ⇒ Object

Save the case_keyword_loc location using the given saved source so that it can be retrieved later.



4004
4005
4006
# File 'lib/prism/node.rb', line 4004

def save_case_keyword_loc(repository)
  repository.enter(node_id, :case_keyword_loc)
end

#save_end_keyword_loc(repository) ⇒ Object

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.



4020
4021
4022
# File 'lib/prism/node.rb', line 4020

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end

#typeObject

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



4040
4041
4042
# File 'lib/prism/node.rb', line 4040

def type
  :case_node
end