Class: Prism::MultiTargetNode

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

Overview

Represents a multi-target expression.

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

Explore code sample This can be a part of MultiWriteNode as above, or the target of a for loop

for a, b in [[1, 2], [3, 4]]
    ^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc) ⇒ MultiTargetNode

Initialize a new MultiTargetNode node.

: (Source source, Integer node_id, Location location, Integer flags, Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode] lefts, (ImplicitRestNode | SplatNode)? rest, Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode] rights, Location? lparen_loc, Location? rparen_loc) -> void



23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
# File 'lib/prism/node.rb', line 23838

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

Class Method Details

.type ⇒ :multi_target_node

See Node.type.

: () -> :multi_target_node

Returns:

  • (:multi_target_node)


23939
23940
23941
# File 'lib/prism/node.rb', line 23939

def self.type
  :multi_target_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
# File 'lib/prism/node.rb', line 24121

def ===(other) # :nodoc:
  other.is_a?(MultiTargetNode) &&
    (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?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


23864
23865
23866
# File 'lib/prism/node.rb', line 23864

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


23871
23872
23873
# File 'lib/prism/node.rb', line 23871

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


23890
23891
23892
23893
23894
23895
23896
# File 'lib/prism/node.rb', line 23890

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(lefts)
  if (rest = self.rest); compact << rest; end
  compact.concat(rights)
  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) ⇒ MultiTargetNode

:call-seq:

copy(**fields) -> MultiTargetNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?lefts: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode], ?rest: (ImplicitRestNode | SplatNode)?, ?rights: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode], ?lparen_loc: Location?, ?rparen_loc: Location?) -> MultiTargetNode

Parameters:

Returns:



23904
23905
23906
# File 'lib/prism/node.rb', line 23904

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)
  MultiTargetNode.new(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
# File 'lib/prism/node.rb', line 23911

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location lefts rest rights lparen_loc rparen_loc lparen rparen]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :lefts                   then deconstructed[:lefts] = self.lefts
    when :rest                    then deconstructed[:rest] = self.rest
    when :rights                  then deconstructed[:rights] = self.rights
    when :lparen_loc              then deconstructed[:lparen_loc] = self.lparen_loc
    when :rparen_loc              then deconstructed[:rparen_loc] = self.rparen_loc
    when :lparen                  then deconstructed[:lparen] = self.lparen
    when :rparen                  then deconstructed[:rparen] = self.rparen
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


23879
23880
23881
23882
23883
23884
23885
# File 'lib/prism/node.rb', line 23879

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  lefts.each { |node| yield node }
  if (rest = self.rest); yield rest; end
  rights.each { |node| yield node }
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


23944
23945
23946
# File 'lib/prism/node.rb', line 23944

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#lefts ⇒ Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]

:call-seq:

lefts -> Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]

Represents the targets expressions before a splat node.

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

Explore code sample 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
    ^^^^

Explore code sample

: () -> Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]



23972
23973
23974
# File 'lib/prism/node.rb', line 23972

def lefts
  @lefts
end

#lparen ⇒ String?

:call-seq:

lparen -> String | nil

Slice the location of lparen_loc from the source.

: () -> String?

Returns:

  • (String, nil)


24104
24105
24106
# File 'lib/prism/node.rb', line 24104

def lparen
  lparen_loc&.slice
end

#lparen_loc ⇒ Location?

:category: Locations :call-seq:

lparen_loc -> Location | nil

The Location of the opening parenthesis.

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

Explore code sample

: () -> Location?

Returns:



24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
# File 'lib/prism/node.rb', line 24042

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

#rest ⇒ ImplicitRestNode, ...

:call-seq:

rest -> ImplicitRestNode | SplatNode | nil

Represents a splat node in the target expression.

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

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

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

Explore code sample If the * is omitted, this field will contain an ImplicitRestNode

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

Explore code sample

: () -> (ImplicitRestNode | SplatNode)?

Returns:



24007
24008
24009
# File 'lib/prism/node.rb', line 24007

def rest
  @rest
end

#rights ⇒ Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]

:call-seq:

rights -> Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]

Represents the targets expressions after a splat node.

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

Explore code sample

: () -> Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | RequiredParameterNode]



24024
24025
24026
# File 'lib/prism/node.rb', line 24024

def rights
  @rights
end

#rparen ⇒ String?

:call-seq:

rparen -> String | nil

Slice the location of rparen_loc from the source.

: () -> String?

Returns:

  • (String, nil)


24114
24115
24116
# File 'lib/prism/node.rb', line 24114

def rparen
  rparen_loc&.slice
end

#rparen_loc ⇒ Location?

:category: Locations :call-seq:

rparen_loc -> Location | nil

The Location of the closing parenthesis.

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

Explore code sample

: () -> Location?

Returns:



24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
# File 'lib/prism/node.rb', line 24076

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) ⇒ Relocation::Entry?

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

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



24059
24060
24061
# File 'lib/prism/node.rb', line 24059

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

#save_rparen_loc(repository) ⇒ Relocation::Entry?

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

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



24093
24094
24095
# File 'lib/prism/node.rb', line 24093

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

#type ⇒ :multi_target_node

See Node#type.

: () -> :multi_target_node

Returns:

  • (:multi_target_node)


23932
23933
23934
# File 'lib/prism/node.rb', line 23932

def type
  :multi_target_node
end