Class: Prism::InNode

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

Overview

Represents the use of the ‘in` keyword in a case statement.

case a; in b then c end
        ^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, statements, in_loc, then_loc, location) ⇒ InNode

def initialize: (pattern: Node, statements: StatementsNode?, in_loc: Location, then_loc: Location?, location: Location) -> void



7055
7056
7057
7058
7059
7060
7061
# File 'lib/prism/node.rb', line 7055

def initialize(pattern, statements, in_loc, then_loc, location)
  @pattern = pattern
  @statements = statements
  @in_loc = in_loc
  @then_loc = then_loc
  @location = location
end

Instance Attribute Details

#in_locObject (readonly)

attr_reader in_loc: Location



7049
7050
7051
# File 'lib/prism/node.rb', line 7049

def in_loc
  @in_loc
end

#patternObject (readonly)

attr_reader pattern: Node



7043
7044
7045
# File 'lib/prism/node.rb', line 7043

def pattern
  @pattern
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



7046
7047
7048
# File 'lib/prism/node.rb', line 7046

def statements
  @statements
end

#then_locObject (readonly)

attr_reader then_loc: Location?



7052
7053
7054
# File 'lib/prism/node.rb', line 7052

def then_loc
  @then_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



7064
7065
7066
# File 'lib/prism/node.rb', line 7064

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

#child_nodesObject Also known as: deconstruct

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



7069
7070
7071
# File 'lib/prism/node.rb', line 7069

def child_nodes
  [pattern, statements]
end

#comment_targetsObject

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



7082
7083
7084
# File 'lib/prism/node.rb', line 7082

def comment_targets
  [pattern, *statements, in_loc, *then_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7074
7075
7076
7077
7078
7079
# File 'lib/prism/node.rb', line 7074

def compact_child_nodes
  compact = []
  compact << pattern
  compact << statements if statements
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> InNode



7087
7088
7089
7090
7091
7092
7093
7094
7095
# File 'lib/prism/node.rb', line 7087

def copy(**params)
  InNode.new(
    params.fetch(:pattern) { pattern },
    params.fetch(:statements) { statements },
    params.fetch(:in_loc) { in_loc },
    params.fetch(:then_loc) { then_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



7101
7102
7103
# File 'lib/prism/node.rb', line 7101

def deconstruct_keys(keys)
  { pattern: pattern, statements: statements, in_loc: in_loc, then_loc: then_loc, location: location }
end

#inObject

def in: () -> String



7106
7107
7108
# File 'lib/prism/node.rb', line 7106

def in
  in_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
# File 'lib/prism/node.rb', line 7115

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── pattern:\n"
  inspector << inspector.child_node(pattern, "│   ")
  if (statements = self.statements).nil?
    inspector << "├── statements: ∅\n"
  else
    inspector << "├── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("│   ")).delete_prefix(inspector.prefix)
  end
  inspector << "├── in_loc: #{inspector.location(in_loc)}\n"
  inspector << "└── then_loc: #{inspector.location(then_loc)}\n"
  inspector.to_str
end

#thenObject

def then: () -> String?



7111
7112
7113
# File 'lib/prism/node.rb', line 7111

def then
  then_loc&.slice
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



7144
7145
7146
# File 'lib/prism/node.rb', line 7144

def type
  :in_node
end