Class: SyntaxTree::StringLiteral

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

StringLiteral represents a string literal.

"string"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(parts:, quote:, location:) ⇒ StringLiteral

Returns a new instance of StringLiteral.



10344
10345
10346
10347
10348
10349
# File 'lib/syntax_tree/node.rb', line 10344

def initialize(parts:, quote:, location:)
  @parts = parts
  @quote = quote
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10342
10343
10344
# File 'lib/syntax_tree/node.rb', line 10342

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



10336
10337
10338
# File 'lib/syntax_tree/node.rb', line 10336

def parts
  @parts
end

#quoteObject (readonly)

nil | String

which quote was used by the string literal



10339
10340
10341
# File 'lib/syntax_tree/node.rb', line 10339

def quote
  @quote
end

Instance Method Details

#===(other) ⇒ Object



10418
10419
10420
10421
# File 'lib/syntax_tree/node.rb', line 10418

def ===(other)
  other.is_a?(StringLiteral) && ArrayMatch.call(parts, other.parts) &&
    quote === other.quote
end

#accept(visitor) ⇒ Object



10351
10352
10353
# File 'lib/syntax_tree/node.rb', line 10351

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

#child_nodesObject Also known as: deconstruct



10355
10356
10357
# File 'lib/syntax_tree/node.rb', line 10355

def child_nodes
  parts
end

#copy(parts: nil, quote: nil, location: nil) ⇒ Object



10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
# File 'lib/syntax_tree/node.rb', line 10359

def copy(parts: nil, quote: nil, location: nil)
  node =
    StringLiteral.new(
      parts: parts || self.parts,
      quote: quote || self.quote,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



10373
10374
10375
# File 'lib/syntax_tree/node.rb', line 10373

def deconstruct_keys(_keys)
  { parts: parts, quote: quote, location: location, comments: comments }
end

#format(q) ⇒ Object



10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
# File 'lib/syntax_tree/node.rb', line 10377

def format(q)
  if parts.empty?
    q.text("#{q.quote}#{q.quote}")
    return
  end

  opening_quote, closing_quote =
    if !Quotes.locked?(self, q.quote)
      [q.quote, q.quote]
    elsif quote&.start_with?("%")
      [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]
    else
      [quote, quote]
    end

  q.text(opening_quote)
  q.group do
    parts.each do |part|
      if part.is_a?(TStringContent)
        value = Quotes.normalize(part.value, closing_quote)
        first = true

        value.each_line(chomp: true) do |line|
          if first
            first = false
          else
            q.breakable_return
          end

          q.text(line)
        end

        q.breakable_return if value.end_with?("\n")
      else
        q.format(part)
      end
    end
  end
  q.text(closing_quote)
end