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, #pretty_print, #to_json

Constructor Details

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



10189
10190
10191
10192
10193
10194
# File 'lib/syntax_tree/node.rb', line 10189

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



10187
10188
10189
# File 'lib/syntax_tree/node.rb', line 10187

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



10181
10182
10183
# File 'lib/syntax_tree/node.rb', line 10181

def parts
  @parts
end

#quoteObject (readonly)

String

which quote was used by the string literal



10184
10185
10186
# File 'lib/syntax_tree/node.rb', line 10184

def quote
  @quote
end

Instance Method Details

#===(other) ⇒ Object



10263
10264
10265
10266
# File 'lib/syntax_tree/node.rb', line 10263

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

#accept(visitor) ⇒ Object



10196
10197
10198
# File 'lib/syntax_tree/node.rb', line 10196

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

#child_nodesObject Also known as: deconstruct



10200
10201
10202
# File 'lib/syntax_tree/node.rb', line 10200

def child_nodes
  parts
end

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



10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
# File 'lib/syntax_tree/node.rb', line 10204

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



10218
10219
10220
# File 'lib/syntax_tree/node.rb', line 10218

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

#format(q) ⇒ Object



10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
# File 'lib/syntax_tree/node.rb', line 10222

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