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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value) ⇒ MultiWriteNode

Initialize a new MultiWriteNode node.



14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
# File 'lib/prism/node.rb', line 14164

def initialize(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @lefts = lefts
  @rest = rest
  @rights = rights
  @lparen_loc = lparen_loc
  @rparen_loc = rparen_loc
  @operator_loc = operator_loc
  @value = value
end

Instance Attribute Details

#leftsObject (readonly)

Represents the targets expressions before a splat node.

a, b, * = 1, 2, 3, 4, 5
^^^^

The splat node can be absent, in that case all target expressions are in the left field.

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


14235
14236
14237
# File 'lib/prism/node.rb', line 14235

def lefts
  @lefts
end

#restObject (readonly)

Represents a splat node in the target expression.

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

The variable can be empty, this results in a SplatNode with a nil expression field.

a, b, * = 1, 2, 3, 4
      ^

If the ‘*` is omitted, this field will contain an ImplicitRestNode

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


14251
14252
14253
# File 'lib/prism/node.rb', line 14251

def rest
  @rest
end

#rightsObject (readonly)

Represents the targets expressions after a splat node.

a, *, b, c = 1, 2, 3, 4, 5
      ^^^^


14257
14258
14259
# File 'lib/prism/node.rb', line 14257

def rights
  @rights
end

#valueObject (readonly)

The value to write to the targets. It can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

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


14323
14324
14325
# File 'lib/prism/node.rb', line 14323

def value
  @value
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See Node::type.



14351
14352
14353
# File 'lib/prism/node.rb', line 14351

def self.type
  :multi_write_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
# File 'lib/prism/node.rb', line 14357

def ===(other)
  other.is_a?(MultiWriteNode) &&
    (lefts.length == other.lefts.length) &&
    lefts.zip(other.lefts).all? { |left, right| left === right } &&
    (rest === other.rest) &&
    (rights.length == other.rights.length) &&
    rights.zip(other.rights).all? { |left, right| left === right } &&
    (lparen_loc.nil? == other.lparen_loc.nil?) &&
    (rparen_loc.nil? == other.rparen_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



14179
14180
14181
# File 'lib/prism/node.rb', line 14179

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



14184
14185
14186
# File 'lib/prism/node.rb', line 14184

def child_nodes
  [*lefts, rest, *rights, value]
end

#comment_targetsObject

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



14209
14210
14211
# File 'lib/prism/node.rb', line 14209

def comment_targets
  [*lefts, *rest, *rights, *lparen_loc, *rparen_loc, operator_loc, value] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14199
14200
14201
14202
14203
14204
14205
14206
# File 'lib/prism/node.rb', line 14199

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(lefts)
  compact << rest if rest
  compact.concat(rights)
  compact << value
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, lefts: self.lefts, rest: self.rest, rights: self.rights, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, operator_loc: self.operator_loc, value: self.value) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?lefts: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode], ?rest: ImplicitRestNode | SplatNode | nil, ?rights: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode], ?lparen_loc: Location?, ?rparen_loc: Location?, ?operator_loc: Location, ?value: Prism::node) -> MultiWriteNode



14214
14215
14216
# File 'lib/prism/node.rb', line 14214

def copy(node_id: self.node_id, location: self.location, flags: self.flags, lefts: self.lefts, rest: self.rest, rights: self.rights, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, operator_loc: self.operator_loc, value: self.value)
  MultiWriteNode.new(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, lefts: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode], rest: ImplicitRestNode | SplatNode | nil, rights: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode], lparen_loc: Location?, rparen_loc: Location?, operator_loc: Location, value: Prism::node }



14222
14223
14224
# File 'lib/prism/node.rb', line 14222

def deconstruct_keys(keys)
  { node_id: node_id, location: location, lefts: lefts, rest: rest, rights: rights, lparen_loc: lparen_loc, rparen_loc: rparen_loc, operator_loc: operator_loc, value: value }
end

#each_child_node {|rest| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



14189
14190
14191
14192
14193
14194
14195
14196
# File 'lib/prism/node.rb', line 14189

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  lefts.each { |node| yield node }
  yield rest if rest
  rights.each { |node| yield node }
  yield value
end

#inspectObject

def inspect -> String



14341
14342
14343
# File 'lib/prism/node.rb', line 14341

def inspect
  InspectVisitor.compose(self)
end

#lparenObject

def lparen: () -> String?



14326
14327
14328
# File 'lib/prism/node.rb', line 14326

def lparen
  lparen_loc&.slice
end

#lparen_locObject

The location of the opening parenthesis.

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


14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
# File 'lib/prism/node.rb', line 14263

def lparen_loc
  location = @lparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @lparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#operatorObject

def operator: () -> String



14336
14337
14338
# File 'lib/prism/node.rb', line 14336

def operator
  operator_loc.slice
end

#operator_locObject

The location of the operator.

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


14307
14308
14309
14310
14311
# File 'lib/prism/node.rb', line 14307

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#rparenObject

def rparen: () -> String?



14331
14332
14333
# File 'lib/prism/node.rb', line 14331

def rparen
  rparen_loc&.slice
end

#rparen_locObject

The location of the closing parenthesis.

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


14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
# File 'lib/prism/node.rb', line 14285

def rparen_loc
  location = @rparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @rparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#save_lparen_loc(repository) ⇒ Object

Save the lparen_loc location using the given saved source so that it can be retrieved later.



14277
14278
14279
# File 'lib/prism/node.rb', line 14277

def save_lparen_loc(repository)
  repository.enter(node_id, :lparen_loc) unless @lparen_loc.nil?
end

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



14315
14316
14317
# File 'lib/prism/node.rb', line 14315

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#save_rparen_loc(repository) ⇒ Object

Save the rparen_loc location using the given saved source so that it can be retrieved later.



14299
14300
14301
# File 'lib/prism/node.rb', line 14299

def save_rparen_loc(repository)
  repository.enter(node_id, :rparen_loc) unless @rparen_loc.nil?
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



14346
14347
14348
# File 'lib/prism/node.rb', line 14346

def type
  :multi_write_node
end