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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of StringEmbExpr.



7562
7563
7564
7565
7566
# File 'lib/syntax_tree/node.rb', line 7562

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



7560
7561
7562
# File 'lib/syntax_tree/node.rb', line 7560

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



7557
7558
7559
# File 'lib/syntax_tree/node.rb', line 7557

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7568
7569
7570
# File 'lib/syntax_tree/node.rb', line 7568

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

#child_nodesObject Also known as: deconstruct



7572
7573
7574
# File 'lib/syntax_tree/node.rb', line 7572

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



7578
7579
7580
# File 'lib/syntax_tree/node.rb', line 7578

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

#format(q) ⇒ Object



7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
# File 'lib/syntax_tree/node.rb', line 7582

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