Class: Prism::StringNode

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

Overview

Represents a string literal, a string contained within a %w list, or plain string content within an interpolated string.

"foo"
^^^^^

%w[foo]
   ^^^

"foo #{bar} baz"
 ^^^^      ^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, opening_loc, content_loc, closing_loc, unescaped) ⇒ StringNode

Initialize a new StringNode node.

: (Source source, Integer node_id, Location location, Integer flags, Location? opening_loc, Location content_loc, Location? closing_loc, String unescaped) -> void

Parameters:



31002
31003
31004
31005
31006
31007
31008
31009
31010
31011
# File 'lib/prism/node.rb', line 31002

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

Class Method Details

.type ⇒ :string_node

See Node.type.

: () -> :string_node

Returns:

  • (:string_node)


31095
31096
31097
# File 'lib/prism/node.rb', line 31095

def self.type
  :string_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


31259
31260
31261
31262
31263
31264
31265
31266
# File 'lib/prism/node.rb', line 31259

def ===(other) # :nodoc:
  other.is_a?(StringNode) &&
    (flags === other.flags) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (content_loc.nil? == other.content_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?) &&
    (unescaped === other.unescaped)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


31027
31028
31029
# File 'lib/prism/node.rb', line 31027

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


31034
31035
31036
# File 'lib/prism/node.rb', line 31034

def child_nodes
  []
end

#closing ⇒ String?

:call-seq:

closing -> String | nil

Slice the location of closing_loc from the source.

: () -> String?

Returns:

  • (String, nil)


31252
31253
31254
# File 'lib/prism/node.rb', line 31252

def closing
  closing_loc&.slice
end

#closing_loc ⇒ Location?

:category: Locations :call-seq:

closing_loc -> Location | nil

Returns the Location represented by closing_loc.

: () -> Location?

Returns:



31194
31195
31196
31197
31198
31199
31200
31201
31202
31203
31204
# File 'lib/prism/node.rb', line 31194

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


31050
31051
31052
# File 'lib/prism/node.rb', line 31050

def compact_child_nodes
  []
end

#content ⇒ String

:call-seq:

content -> String

Slice the location of content_loc from the source.

: () -> String

Returns:

  • (String)


31242
31243
31244
# File 'lib/prism/node.rb', line 31242

def content
  content_loc.slice
end

#content_loc ⇒ Location

:category: Locations :call-seq:

content_loc -> Location

Returns the Location represented by content_loc.

: () -> Location

Returns:



31172
31173
31174
31175
31176
# File 'lib/prism/node.rb', line 31172

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

#copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, content_loc: self.content_loc, closing_loc: self.closing_loc, unescaped: self.unescaped) ⇒ StringNode

:call-seq:

copy(**fields) -> StringNode

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

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?opening_loc: Location?, ?content_loc: Location, ?closing_loc: Location?, ?unescaped: String) -> StringNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • opening_loc: (Location, nil) (defaults to: self.opening_loc)
  • content_loc: (Location) (defaults to: self.content_loc)
  • closing_loc: (Location, nil) (defaults to: self.closing_loc)
  • unescaped: (String) (defaults to: self.unescaped)

Returns:



31060
31061
31062
# File 'lib/prism/node.rb', line 31060

def copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, content_loc: self.content_loc, closing_loc: self.closing_loc, unescaped: self.unescaped)
  StringNode.new(source, node_id, location, flags, opening_loc, content_loc, closing_loc, unescaped)
end

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


31067
31068
31069
31070
31071
31072
31073
31074
31075
31076
31077
31078
31079
31080
31081
31082
31083
# File 'lib/prism/node.rb', line 31067

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


31042
31043
31044
31045
# File 'lib/prism/node.rb', line 31042

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

end

#forced_binary_encoding? ⇒ Boolean

:category: Flags internal bytes forced the encoding to binary

: () -> bool

Returns:

  • (Boolean)


31118
31119
31120
# File 'lib/prism/node.rb', line 31118

def forced_binary_encoding?
  flags.anybits?(StringFlags::FORCED_BINARY_ENCODING)
end

#forced_utf8_encoding? ⇒ Boolean

:category: Flags internal bytes forced the encoding to UTF-8

: () -> bool

Returns:

  • (Boolean)


31110
31111
31112
# File 'lib/prism/node.rb', line 31110

def forced_utf8_encoding?
  flags.anybits?(StringFlags::FORCED_UTF8_ENCODING)
end

#frozen? ⇒ Boolean

:category: Flags frozen by virtue of a frozen_string_literal: true comment or --enable-frozen-string-literal

: () -> bool

Returns:

  • (Boolean)


31126
31127
31128
# File 'lib/prism/node.rb', line 31126

def frozen?
  flags.anybits?(StringFlags::FROZEN)
end

#heredoc? ⇒ Boolean?

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

: () -> bool?

Returns:

  • (Boolean, nil)


115
116
117
# File 'lib/prism/node_ext.rb', line 115

def heredoc?
  HeredocQuery.heredoc?(opening)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


31100
31101
31102
# File 'lib/prism/node.rb', line 31100

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

#mutable? ⇒ Boolean

:category: Flags mutable by virtue of a frozen_string_literal: false comment or --disable-frozen-string-literal

: () -> bool

Returns:

  • (Boolean)


31134
31135
31136
# File 'lib/prism/node.rb', line 31134

def mutable?
  flags.anybits?(StringFlags::MUTABLE)
end

#opening ⇒ String?

:call-seq:

opening -> String | nil

Slice the location of opening_loc from the source.

: () -> String?

Returns:

  • (String, nil)


31232
31233
31234
# File 'lib/prism/node.rb', line 31232

def opening
  opening_loc&.slice
end

#opening_loc ⇒ Location?

:category: Locations :call-seq:

opening_loc -> Location | nil

Returns the Location represented by opening_loc.

: () -> Location?

Returns:



31145
31146
31147
31148
31149
31150
31151
31152
31153
31154
31155
# File 'lib/prism/node.rb', line 31145

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
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:



31211
31212
31213
# File 'lib/prism/node.rb', line 31211

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_content_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



31183
31184
31185
# File 'lib/prism/node.rb', line 31183

def save_content_loc(repository)
  repository.enter(node_id, :content_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:



31162
31163
31164
# File 'lib/prism/node.rb', line 31162

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#to_interpolated ⇒ InterpolatedStringNode

Occasionally it's helpful to treat a string as if it were interpolated so that there's a consistent interface for working with strings.

: () -> InterpolatedStringNode



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/prism/node_ext.rb', line 123

def to_interpolated
  InterpolatedStringNode.new(
    source,
    -1,
    location,
    frozen? ? InterpolatedStringNodeFlags::FROZEN : 0,
    opening_loc,
    [copy(location: content_loc, opening_loc: nil, closing_loc: nil)],
    closing_loc
  )
end

#type ⇒ :string_node

See Node#type.

: () -> :string_node

Returns:

  • (:string_node)


31088
31089
31090
# File 'lib/prism/node.rb', line 31088

def type
  :string_node
end

#unescaped ⇒ String

:call-seq:

unescaped -> String

Returns the unescaped attribute.

: () -> String

Returns:

  • (String)


31220
31221
31222
# File 'lib/prism/node.rb', line 31220

def unescaped
  @unescaped
end