Class: Prism::MultiWriteNode

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

Overview

Represents a write to a multi-target expression.

a, b, c = 1, 2, 3
^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets, lparen_loc, rparen_loc, operator_loc, value, location) ⇒ MultiWriteNode

def initialize: (targets: Array, lparen_loc: Location?, rparen_loc: Location?, operator_loc: Location, value: Node, location: Location) -> void



10120
10121
10122
10123
10124
10125
10126
10127
# File 'lib/prism/node.rb', line 10120

def initialize(targets, lparen_loc, rparen_loc, operator_loc, value, location)
  @targets = targets
  @lparen_loc = lparen_loc
  @rparen_loc = rparen_loc
  @operator_loc = operator_loc
  @value = value
  @location = location
end

Instance Attribute Details

#lparen_locObject (readonly)

attr_reader lparen_loc: Location?



10108
10109
10110
# File 'lib/prism/node.rb', line 10108

def lparen_loc
  @lparen_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



10114
10115
10116
# File 'lib/prism/node.rb', line 10114

def operator_loc
  @operator_loc
end

#rparen_locObject (readonly)

attr_reader rparen_loc: Location?



10111
10112
10113
# File 'lib/prism/node.rb', line 10111

def rparen_loc
  @rparen_loc
end

#targetsObject (readonly)

attr_reader targets: Array



10105
10106
10107
# File 'lib/prism/node.rb', line 10105

def targets
  @targets
end

#valueObject (readonly)

attr_reader value: Node



10117
10118
10119
# File 'lib/prism/node.rb', line 10117

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



10130
10131
10132
# File 'lib/prism/node.rb', line 10130

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

#child_nodesObject Also known as: deconstruct

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



10135
10136
10137
# File 'lib/prism/node.rb', line 10135

def child_nodes
  [*targets, value]
end

#comment_targetsObject

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



10145
10146
10147
# File 'lib/prism/node.rb', line 10145

def comment_targets
  [*targets, *lparen_loc, *rparen_loc, operator_loc, value]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



10140
10141
10142
# File 'lib/prism/node.rb', line 10140

def compact_child_nodes
  [*targets, value]
end

#copy(**params) ⇒ Object

def copy: (**params) -> MultiWriteNode



10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
# File 'lib/prism/node.rb', line 10150

def copy(**params)
  MultiWriteNode.new(
    params.fetch(:targets) { targets },
    params.fetch(:lparen_loc) { lparen_loc },
    params.fetch(:rparen_loc) { rparen_loc },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



10165
10166
10167
# File 'lib/prism/node.rb', line 10165

def deconstruct_keys(keys)
  { targets: targets, lparen_loc: lparen_loc, rparen_loc: rparen_loc, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
# File 'lib/prism/node.rb', line 10184

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── targets: #{inspector.list("#{inspector.prefix}", targets)}"
  inspector << "├── lparen_loc: #{inspector.location(lparen_loc)}\n"
  inspector << "├── rparen_loc: #{inspector.location(rparen_loc)}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#lparenObject

def lparen: () -> String?



10170
10171
10172
# File 'lib/prism/node.rb', line 10170

def lparen
  lparen_loc&.slice
end

#operatorObject

def operator: () -> String



10180
10181
10182
# File 'lib/prism/node.rb', line 10180

def operator
  operator_loc.slice
end

#rparenObject

def rparen: () -> String?



10175
10176
10177
# File 'lib/prism/node.rb', line 10175

def rparen
  rparen_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



10209
10210
10211
# File 'lib/prism/node.rb', line 10209

def type
  :multi_write_node
end