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

Constructor Details

#initialize(parts:, quote:, location:, comments: []) ⇒ StringLiteral

Returns a new instance of StringLiteral.



9399
9400
9401
9402
9403
9404
# File 'lib/syntax_tree/node.rb', line 9399

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9397
9398
9399
# File 'lib/syntax_tree/node.rb', line 9397

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



9391
9392
9393
# File 'lib/syntax_tree/node.rb', line 9391

def parts
  @parts
end

#quoteObject (readonly)

String

which quote was used by the string literal



9394
9395
9396
# File 'lib/syntax_tree/node.rb', line 9394

def quote
  @quote
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



9406
9407
9408
# File 'lib/syntax_tree/node.rb', line 9406

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



9412
9413
9414
# File 'lib/syntax_tree/node.rb', line 9412

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

#format(q) ⇒ Object



9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
# File 'lib/syntax_tree/node.rb', line 9416

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]
    elsif quote.start_with?("%")
      [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]
    else
      [quote, quote]
    end

  q.group(0, opening_quote, closing_quote) do
    parts.each do |part|
      if part.is_a?(TStringContent)
        value = Quotes.normalize(part.value, closing_quote)
        separator = -> { q.breakable(force: true, indent: false) }
        q.seplist(value.split(/\r?\n/, -1), separator) do |text|
          q.text(text)
        end
      else
        q.format(part)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
# File 'lib/syntax_tree/node.rb', line 9446

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("string_literal")

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



9457
9458
9459
9460
9461
9462
9463
9464
9465
# File 'lib/syntax_tree/node.rb', line 9457

def to_json(*opts)
  {
    type: :string_literal,
    parts: parts,
    quote: quote,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end