Class: Prism::WhenNode

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

Overview

Represents the use of the ‘when` keyword within a case statement.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) ⇒ WhenNode

Initialize a new WhenNode node.



17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
# File 'lib/prism/node.rb', line 17883

def initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @keyword_loc = keyword_loc
  @conditions = conditions
  @then_keyword_loc = then_keyword_loc
  @statements = statements
end

Instance Attribute Details

#conditionsObject (readonly)

attr_reader conditions: Array



17944
17945
17946
# File 'lib/prism/node.rb', line 17944

def conditions
  @conditions
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



17966
17967
17968
# File 'lib/prism/node.rb', line 17966

def statements
  @statements
end

Class Method Details

.typeObject

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



17989
17990
17991
# File 'lib/prism/node.rb', line 17989

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



17995
17996
17997
17998
17999
18000
18001
18002
# File 'lib/prism/node.rb', line 17995

def ===(other)
  other.is_a?(WhenNode) &&
    (keyword_loc.nil? == other.keyword_loc.nil?) &&
    (conditions.length == other.conditions.length) &&
    conditions.zip(other.conditions).all? { |left, right| left === right } &&
    (then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
    (statements === other.statements)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



17895
17896
17897
# File 'lib/prism/node.rb', line 17895

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

#child_nodesObject Also known as: deconstruct

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



17900
17901
17902
# File 'lib/prism/node.rb', line 17900

def child_nodes
  [*conditions, statements]
end

#comment_targetsObject

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



17913
17914
17915
# File 'lib/prism/node.rb', line 17913

def comment_targets
  [keyword_loc, *conditions, *then_keyword_loc, *statements] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



17905
17906
17907
17908
17909
17910
# File 'lib/prism/node.rb', line 17905

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

#copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?keyword_loc: Location, ?conditions: Array, ?then_keyword_loc: Location?, ?statements: StatementsNode?) -> WhenNode



17918
17919
17920
# File 'lib/prism/node.rb', line 17918

def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements)
  WhenNode.new(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, keyword_loc: Location, conditions: Array, then_keyword_loc: Location?, statements: StatementsNode? }



17926
17927
17928
# File 'lib/prism/node.rb', line 17926

def deconstruct_keys(keys)
  { node_id: node_id, location: location, keyword_loc: keyword_loc, conditions: conditions, then_keyword_loc: then_keyword_loc, statements: statements }
end

#inspectObject

def inspect -> String



17979
17980
17981
# File 'lib/prism/node.rb', line 17979

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



17969
17970
17971
# File 'lib/prism/node.rb', line 17969

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



17931
17932
17933
17934
17935
# File 'lib/prism/node.rb', line 17931

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

#save_keyword_loc(repository) ⇒ Object

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



17939
17940
17941
# File 'lib/prism/node.rb', line 17939

def save_keyword_loc(repository)
  repository.enter(node_id, :keyword_loc)
end

#save_then_keyword_loc(repository) ⇒ Object

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



17961
17962
17963
# File 'lib/prism/node.rb', line 17961

def save_then_keyword_loc(repository)
  repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil?
end

#then_keywordObject

def then_keyword: () -> String?



17974
17975
17976
# File 'lib/prism/node.rb', line 17974

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
# File 'lib/prism/node.rb', line 17947

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

#typeObject

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



17984
17985
17986
# File 'lib/prism/node.rb', line 17984

def type
  :when_node
end