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.



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

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



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

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



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

def parts
  @parts
end

#quoteObject (readonly)

nil | String

which quote was used by the string literal



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

def quote
  @quote
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



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

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



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

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

#format(q) ⇒ Object



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
10417
# File 'lib/syntax_tree/node.rb', line 10378

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