Class: Prism::InterpolatedXStringNode

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

Overview

Represents an xstring literal that contains interpolation.

`foo #{bar} baz`
^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, opening_loc, parts, closing_loc) ⇒ InterpolatedXStringNode

Initialize a new InterpolatedXStringNode node.

: (Source source, Integer node_id, Location location, Integer flags, Location opening_loc, Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode] parts, Location closing_loc) -> void

Parameters:



20328
20329
20330
20331
20332
20333
20334
20335
20336
# File 'lib/prism/node.rb', line 20328

def initialize(source, node_id, location, flags, opening_loc, parts, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @opening_loc = opening_loc
  @parts = parts
  @closing_loc = closing_loc
end

Class Method Details

.type ⇒ :interpolated_x_string_node

See Node.type.

: () -> :interpolated_x_string_node

Returns:

  • (:interpolated_x_string_node)


20419
20420
20421
# File 'lib/prism/node.rb', line 20419

def self.type
  :interpolated_x_string_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


20509
20510
20511
20512
20513
20514
20515
# File 'lib/prism/node.rb', line 20509

def ===(other) # :nodoc:
  other.is_a?(InterpolatedXStringNode) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (parts.length == other.parts.length) &&
    parts.zip(other.parts).all? { |left, right| left === right } &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


20352
20353
20354
# File 'lib/prism/node.rb', line 20352

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


20359
20360
20361
# File 'lib/prism/node.rb', line 20359

def child_nodes
  [*parts]
end

#closing ⇒ String

:call-seq:

closing -> String

Slice the location of closing_loc from the source.

: () -> String

Returns:

  • (String)


20502
20503
20504
# File 'lib/prism/node.rb', line 20502

def closing
  closing_loc.slice
end

#closing_loc ⇒ Location

:category: Locations :call-seq:

closing_loc -> Location

Returns the Location represented by closing_loc.

: () -> Location

Returns:



20469
20470
20471
20472
20473
# File 'lib/prism/node.rb', line 20469

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


20376
20377
20378
# File 'lib/prism/node.rb', line 20376

def compact_child_nodes
  [*parts]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, parts: self.parts, closing_loc: self.closing_loc) ⇒ InterpolatedXStringNode

:call-seq:

copy(**fields) -> InterpolatedXStringNode

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

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?opening_loc: Location, ?parts: Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode], ?closing_loc: Location) -> InterpolatedXStringNode

Parameters:

Returns:



20386
20387
20388
# File 'lib/prism/node.rb', line 20386

def copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, parts: self.parts, closing_loc: self.closing_loc)
  InterpolatedXStringNode.new(source, node_id, location, flags, opening_loc, parts, closing_loc)
end

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
# File 'lib/prism/node.rb', line 20393

def deconstruct_keys(keys) # :nodoc:
  (keys || i[node_id location opening_loc parts closing_loc opening closing]).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 :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :parts                   then deconstructed[:parts] = self.parts
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    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)


20367
20368
20369
20370
20371
# File 'lib/prism/node.rb', line 20367

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

  parts.each { |node| yield node }
end

#heredoc? ⇒ Boolean?

Returns true if this node was represented as a heredoc in the source code.

: () -> bool?

Returns:

  • (Boolean, nil)


106
107
108
# File 'lib/prism/node_ext.rb', line 106

def heredoc?
  HeredocQuery.heredoc?(opening)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


20424
20425
20426
# File 'lib/prism/node.rb', line 20424

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

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


418
419
420
421
# File 'lib/prism/parse_result/newlines.rb', line 418

def newline_flag!(lines) # :nodoc:
  first = parts.first
  first.newline_flag!(lines) if first
end

#opening ⇒ String

:call-seq:

opening -> String

Slice the location of opening_loc from the source.

: () -> String

Returns:

  • (String)


20492
20493
20494
# File 'lib/prism/node.rb', line 20492

def opening
  opening_loc.slice
end

#opening_loc ⇒ Location

:category: Locations :call-seq:

opening_loc -> Location

Returns the Location represented by opening_loc.

: () -> Location

Returns:



20437
20438
20439
20440
20441
# File 'lib/prism/node.rb', line 20437

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

#parts ⇒ Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode]

:call-seq:

parts -> Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode]

Returns the parts attribute.

: () -> Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode]



20458
20459
20460
# File 'lib/prism/node.rb', line 20458

def parts
  @parts
end

#save_closing_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



20480
20481
20482
# File 'lib/prism/node.rb', line 20480

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc)
end

#save_opening_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



20448
20449
20450
# File 'lib/prism/node.rb', line 20448

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc)
end

#type ⇒ :interpolated_x_string_node

See Node#type.

: () -> :interpolated_x_string_node

Returns:

  • (:interpolated_x_string_node)


20412
20413
20414
# File 'lib/prism/node.rb', line 20412

def type
  :interpolated_x_string_node
end