Class: Prism::CaseNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::CaseNode
- 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
-
#case_keyword_loc ⇒ Object
readonly
attr_reader case_keyword_loc: Location.
-
#conditions ⇒ Object
readonly
attr_reader conditions: Array.
-
#consequent ⇒ Object
readonly
attr_reader consequent: ElseNode?.
-
#end_keyword_loc ⇒ Object
readonly
attr_reader end_keyword_loc: Location.
-
#predicate ⇒ Object
readonly
attr_reader predicate: Node?.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#case_keyword ⇒ Object
def case_keyword: () -> String.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> CaseNode.
- #deconstruct_keys(keys) ⇒ Object
-
#end_keyword ⇒ Object
def end_keyword: () -> String.
-
#initialize(predicate, conditions, consequent, case_keyword_loc, end_keyword_loc, location) ⇒ CaseNode
constructor
def initialize: (predicate: Node?, conditions: Array, consequent: ElseNode?, case_keyword_loc: Location, end_keyword_loc: Location, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
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_loc ⇒ Object (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 |
#conditions ⇒ Object (readonly)
attr_reader conditions: Array
2584 2585 2586 |
# File 'lib/prism/node.rb', line 2584 def conditions @conditions end |
#consequent ⇒ Object (readonly)
attr_reader consequent: ElseNode?
2587 2588 2589 |
# File 'lib/prism/node.rb', line 2587 def consequent @consequent end |
#end_keyword_loc ⇒ Object (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 |
#predicate ⇒ Object (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_keyword ⇒ Object
def case_keyword: () -> String
2650 2651 2652 |
# File 'lib/prism/node.rb', line 2650 def case_keyword case_keyword_loc.slice end |
#child_nodes ⇒ Object 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_targets ⇒ Object
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_nodes ⇒ Object
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
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_keyword ⇒ Object
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 |
#type ⇒ Object
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 |