Class: Prism::StringNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/node_ext.rb,
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"
 ^^^^      ^^^^

Instance Attribute Summary collapse

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.


15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
# File 'lib/prism/node.rb', line 15305

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

Instance Attribute Details

#unescapedObject (readonly)

attr_reader unescaped: String


15403
15404
15405
# File 'lib/prism/node.rb', line 15403

def unescaped
  @unescaped
end

Class Method Details

.typeObject

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


15431
15432
15433
# File 'lib/prism/node.rb', line 15431

def self.type
  :string_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.


15437
15438
15439
15440
15441
15442
15443
15444
# File 'lib/prism/node.rb', line 15437

def ===(other)
  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

def accept: (Visitor visitor) -> void


15317
15318
15319
# File 'lib/prism/node.rb', line 15317

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

#child_nodesObject Also known as: deconstruct

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


15322
15323
15324
# File 'lib/prism/node.rb', line 15322

def child_nodes
  []
end

#closingObject

def closing: () -> String?


15416
15417
15418
# File 'lib/prism/node.rb', line 15416

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?


15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
# File 'lib/prism/node.rb', line 15390

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

#comment_targetsObject

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


15332
15333
15334
# File 'lib/prism/node.rb', line 15332

def comment_targets
  [*opening_loc, content_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array


15327
15328
15329
# File 'lib/prism/node.rb', line 15327

def compact_child_nodes
  []
end

#contentObject

def content: () -> String


15411
15412
15413
# File 'lib/prism/node.rb', line 15411

def content
  content_loc.slice
end

#content_locObject

attr_reader content_loc: Location


15383
15384
15385
15386
15387
# File 'lib/prism/node.rb', line 15383

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) ⇒ Object

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


15337
15338
15339
# File 'lib/prism/node.rb', line 15337

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) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, opening_loc: Location?, content_loc: Location, closing_loc: Location?, unescaped: String }


15345
15346
15347
# File 'lib/prism/node.rb', line 15345

def deconstruct_keys(keys)
  { node_id: node_id, location: location, opening_loc: opening_loc, content_loc: content_loc, closing_loc: closing_loc, unescaped: unescaped }
end

#forced_binary_encoding?Boolean

def forced_binary_encoding?: () -> bool

Returns:

  • (Boolean)

15355
15356
15357
# File 'lib/prism/node.rb', line 15355

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

#forced_utf8_encoding?Boolean

def forced_utf8_encoding?: () -> bool

Returns:

  • (Boolean)

15350
15351
15352
# File 'lib/prism/node.rb', line 15350

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

#frozen?Boolean

def frozen?: () -> bool

Returns:

  • (Boolean)

15360
15361
15362
# File 'lib/prism/node.rb', line 15360

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

#inspectObject

def inspect -> String


15421
15422
15423
# File 'lib/prism/node.rb', line 15421

def inspect
  InspectVisitor.compose(self)
end

#mutable?Boolean

def mutable?: () -> bool

Returns:

  • (Boolean)

15365
15366
15367
# File 'lib/prism/node.rb', line 15365

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

#openingObject

def opening: () -> String?


15406
15407
15408
# File 'lib/prism/node.rb', line 15406

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?


15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
# File 'lib/prism/node.rb', line 15370

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

#to_interpolatedObject

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


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/prism/node_ext.rb', line 72

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

#typeObject

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


15426
15427
15428
# File 'lib/prism/node.rb', line 15426

def type
  :string_node
end