Class: Prism::StringNode

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

Instance Method Summary collapse

Constructor Details

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

def initialize: (flags: Integer, opening_loc: Location?, content_loc: Location, closing_loc: Location?, unescaped: String, location: Location) -> void



13265
13266
13267
13268
13269
13270
13271
13272
# File 'lib/prism/node.rb', line 13265

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

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



13259
13260
13261
# File 'lib/prism/node.rb', line 13259

def closing_loc
  @closing_loc
end

#content_locObject (readonly)

attr_reader content_loc: Location



13256
13257
13258
# File 'lib/prism/node.rb', line 13256

def content_loc
  @content_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



13253
13254
13255
# File 'lib/prism/node.rb', line 13253

def opening_loc
  @opening_loc
end

#unescapedObject (readonly)

attr_reader unescaped: String



13262
13263
13264
# File 'lib/prism/node.rb', line 13262

def unescaped
  @unescaped
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



13275
13276
13277
# File 'lib/prism/node.rb', line 13275

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

#child_nodesObject Also known as: deconstruct

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



13280
13281
13282
# File 'lib/prism/node.rb', line 13280

def child_nodes
  []
end

#closingObject

def closing: () -> String?



13330
13331
13332
# File 'lib/prism/node.rb', line 13330

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



13290
13291
13292
# File 'lib/prism/node.rb', line 13290

def comment_targets
  [*opening_loc, content_loc, *closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



13285
13286
13287
# File 'lib/prism/node.rb', line 13285

def compact_child_nodes
  []
end

#contentObject

def content: () -> String



13325
13326
13327
# File 'lib/prism/node.rb', line 13325

def content
  content_loc.slice
end

#copy(**params) ⇒ Object

def copy: (**params) -> StringNode



13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
# File 'lib/prism/node.rb', line 13295

def copy(**params)
  StringNode.new(
    params.fetch(:flags) { flags },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:content_loc) { content_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:unescaped) { unescaped },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



13310
13311
13312
# File 'lib/prism/node.rb', line 13310

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

#frozen?Boolean

def frozen?: () -> bool



13315
13316
13317
# File 'lib/prism/node.rb', line 13315

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

#inspect(inspector = NodeInspector.new) ⇒ Object



13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
# File 'lib/prism/node.rb', line 13334

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  flags = [("frozen" if frozen?)].compact
  inspector << "├── flags: #{flags.empty? ? "∅" : flags.join(", ")}\n"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "├── content_loc: #{inspector.location(content_loc)}\n"
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector << "└── unescaped: #{unescaped.inspect}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



13320
13321
13322
# File 'lib/prism/node.rb', line 13320

def opening
  opening_loc&.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



13359
13360
13361
# File 'lib/prism/node.rb', line 13359

def type
  :string_node
end