Class: SyntaxTree::StringEmbExpr

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

StringEmbExpr represents interpolated content. It can be contained within a couple of different parent nodes, including regular expressions, strings, and dynamic symbols.

"string #{expression}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statements:, location:, comments: []) ⇒ StringEmbExpr

Returns a new instance of StringEmbExpr.



11216
11217
11218
11219
11220
# File 'lib/syntax_tree.rb', line 11216

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11214
11215
11216
# File 'lib/syntax_tree.rb', line 11214

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11211
11212
11213
# File 'lib/syntax_tree.rb', line 11211

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



11208
11209
11210
# File 'lib/syntax_tree.rb', line 11208

def statements
  @statements
end

Instance Method Details

#child_nodesObject



11222
11223
11224
# File 'lib/syntax_tree.rb', line 11222

def child_nodes
  [statements]
end

#format(q) ⇒ Object



11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
# File 'lib/syntax_tree.rb', line 11226

def format(q)
  if location.start_line == location.end_line
    # If the contents of this embedded expression were originally on the
    # same line in the source, then we're going to leave them in place and
    # assume that's the way the developer wanted this expression
    # represented.
    doc = q.group(0, '#{', "}") { q.format(statements) }
    RemoveBreaks.call(doc)
  else
    q.group do
      q.text('#{')
      q.indent do
        q.breakable("")
        q.format(statements)
      end
      q.breakable("")
      q.text("}")
    end
  end
end

#pretty_print(q) ⇒ Object



11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
# File 'lib/syntax_tree.rb', line 11247

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

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



11258
11259
11260
11261
11262
11263
11264
11265
# File 'lib/syntax_tree.rb', line 11258

def to_json(*opts)
  {
    type: :string_embexpr,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end