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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StringLiteral.



9765
9766
9767
9768
9769
9770
# File 'lib/syntax_tree/node.rb', line 9765

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



9763
9764
9765
# File 'lib/syntax_tree/node.rb', line 9763

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9760
9761
9762
# File 'lib/syntax_tree/node.rb', line 9760

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



9754
9755
9756
# File 'lib/syntax_tree/node.rb', line 9754

def parts
  @parts
end

#quoteObject (readonly)

String

which quote was used by the string literal



9757
9758
9759
# File 'lib/syntax_tree/node.rb', line 9757

def quote
  @quote
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



9772
9773
9774
# File 'lib/syntax_tree/node.rb', line 9772

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



9778
9779
9780
# File 'lib/syntax_tree/node.rb', line 9778

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

#format(q) ⇒ Object



9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
# File 'lib/syntax_tree/node.rb', line 9782

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



9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
# File 'lib/syntax_tree/node.rb', line 9812

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



9823
9824
9825
9826
9827
9828
9829
9830
9831
# File 'lib/syntax_tree/node.rb', line 9823

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