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

Class Method 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



3084
3085
3086
3087
3088
3089
3090
3091
# File 'lib/prism/node.rb', line 3084

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



3078
3079
3080
# File 'lib/prism/node.rb', line 3078

def case_keyword_loc
  @case_keyword_loc
end

#conditionsObject (readonly)

attr_reader conditions: Array



3072
3073
3074
# File 'lib/prism/node.rb', line 3072

def conditions
  @conditions
end

#consequentObject (readonly)

attr_reader consequent: ElseNode?



3075
3076
3077
# File 'lib/prism/node.rb', line 3075

def consequent
  @consequent
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



3081
3082
3083
# File 'lib/prism/node.rb', line 3081

def end_keyword_loc
  @end_keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node?



3069
3070
3071
# File 'lib/prism/node.rb', line 3069

def predicate
  @predicate
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



3192
3193
3194
# File 'lib/prism/node.rb', line 3192

def self.type
  :case_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



3094
3095
3096
# File 'lib/prism/node.rb', line 3094

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

#case_keywordObject

def case_keyword: () -> String



3138
3139
3140
# File 'lib/prism/node.rb', line 3138

def case_keyword
  case_keyword_loc.slice
end

#child_nodesObject Also known as: deconstruct

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



3099
3100
3101
# File 'lib/prism/node.rb', line 3099

def child_nodes
  [predicate, *conditions, consequent]
end

#comment_targetsObject

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



3113
3114
3115
# File 'lib/prism/node.rb', line 3113

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3104
3105
3106
3107
3108
3109
3110
# File 'lib/prism/node.rb', line 3104

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



3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
# File 'lib/prism/node.rb', line 3118

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]



3133
3134
3135
# File 'lib/prism/node.rb', line 3133

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



3143
3144
3145
# File 'lib/prism/node.rb', line 3143

def end_keyword
  end_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
# File 'lib/prism/node.rb', line 3148

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



3182
3183
3184
# File 'lib/prism/node.rb', line 3182

def type
  :case_node
end