Class: Prism::CaseNode

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

Overview

Represents the use of a case statement.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate, conditions, consequent, case_keyword_loc, end_keyword_loc, location) ⇒ CaseNode

def initialize: (predicate: Node?, conditions: Array, consequent: ElseNode?, case_keyword_loc: Location, end_keyword_loc: Location, location: Location) -> void



2596
2597
2598
2599
2600
2601
2602
2603
# File 'lib/prism/node.rb', line 2596

def initialize(predicate, conditions, consequent, case_keyword_loc, end_keyword_loc, location)
  @predicate = predicate
  @conditions = conditions
  @consequent = consequent
  @case_keyword_loc = case_keyword_loc
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#case_keyword_locObject (readonly)

attr_reader case_keyword_loc: Location



2590
2591
2592
# File 'lib/prism/node.rb', line 2590

def case_keyword_loc
  @case_keyword_loc
end

#conditionsObject (readonly)

attr_reader conditions: Array



2584
2585
2586
# File 'lib/prism/node.rb', line 2584

def conditions
  @conditions
end

#consequentObject (readonly)

attr_reader consequent: ElseNode?



2587
2588
2589
# File 'lib/prism/node.rb', line 2587

def consequent
  @consequent
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



2593
2594
2595
# File 'lib/prism/node.rb', line 2593

def end_keyword_loc
  @end_keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node?



2581
2582
2583
# File 'lib/prism/node.rb', line 2581

def predicate
  @predicate
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2606
2607
2608
# File 'lib/prism/node.rb', line 2606

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

#case_keywordObject

def case_keyword: () -> String



2650
2651
2652
# File 'lib/prism/node.rb', line 2650

def case_keyword
  case_keyword_loc.slice
end

#child_nodesObject Also known as: deconstruct

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



2611
2612
2613
# File 'lib/prism/node.rb', line 2611

def child_nodes
  [predicate, *conditions, consequent]
end

#comment_targetsObject

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



2625
2626
2627
# File 'lib/prism/node.rb', line 2625

def comment_targets
  [*predicate, *conditions, *consequent, case_keyword_loc, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2616
2617
2618
2619
2620
2621
2622
# File 'lib/prism/node.rb', line 2616

def compact_child_nodes
  compact = []
  compact << predicate if predicate
  compact.concat(conditions)
  compact << consequent if consequent
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> CaseNode



2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
# File 'lib/prism/node.rb', line 2630

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



2645
2646
2647
# File 'lib/prism/node.rb', line 2645

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

#end_keywordObject

def end_keyword: () -> String



2655
2656
2657
# File 'lib/prism/node.rb', line 2655

def end_keyword
  end_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
# File 'lib/prism/node.rb', line 2659

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



2693
2694
2695
# File 'lib/prism/node.rb', line 2693

def type
  :case_node
end