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

Returns a new instance of StringLiteral.



10154
10155
10156
10157
10158
10159
# File 'lib/syntax_tree/node.rb', line 10154

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



10152
10153
10154
# File 'lib/syntax_tree/node.rb', line 10152

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

string literal



10146
10147
10148
# File 'lib/syntax_tree/node.rb', line 10146

def parts
  @parts
end

#quoteObject (readonly)

String

which quote was used by the string literal



10149
10150
10151
# File 'lib/syntax_tree/node.rb', line 10149

def quote
  @quote
end

Instance Method Details

#===(other) ⇒ Object



10228
10229
10230
10231
# File 'lib/syntax_tree/node.rb', line 10228

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

#accept(visitor) ⇒ Object



10161
10162
10163
# File 'lib/syntax_tree/node.rb', line 10161

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

#child_nodesObject Also known as: deconstruct



10165
10166
10167
# File 'lib/syntax_tree/node.rb', line 10165

def child_nodes
  parts
end

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



10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
# File 'lib/syntax_tree/node.rb', line 10169

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



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

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

#format(q) ⇒ Object



10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
# File 'lib/syntax_tree/node.rb', line 10187

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