Class: SyntaxTree::StringEmbExpr

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of StringEmbExpr.



8323
8324
8325
8326
8327
# File 'lib/syntax_tree/node.rb', line 8323

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



8321
8322
8323
# File 'lib/syntax_tree/node.rb', line 8321

def comments
  @comments
end

#statements ⇒ Object (readonly)

[Statements] the expressions to be interpolated



8318
8319
8320
# File 'lib/syntax_tree/node.rb', line 8318

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8329
8330
8331
# File 'lib/syntax_tree/node.rb', line 8329

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

#child_nodes ⇒ Object Also known as: deconstruct



8333
8334
8335
# File 'lib/syntax_tree/node.rb', line 8333

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



8339
8340
8341
# File 'lib/syntax_tree/node.rb', line 8339

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

#format(q) ⇒ Object



8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
# File 'lib/syntax_tree/node.rb', line 8343

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.
    q.remove_breaks(q.group(0, '#{', "}") { q.format(statements) })
  else
    q.group do
      q.text('#{')
      q.indent do
        q.breakable("")
        q.format(statements)
      end
      q.breakable("")
      q.text("}")
    end
  end
end