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



8587
8588
8589
8590
8591
# File 'lib/syntax_tree/node.rb', line 8587

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



8585
8586
8587
# File 'lib/syntax_tree/node.rb', line 8585

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



8582
8583
8584
# File 'lib/syntax_tree/node.rb', line 8582

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8593
8594
8595
# File 'lib/syntax_tree/node.rb', line 8593

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

#child_nodesObject Also known as: deconstruct



8597
8598
8599
# File 'lib/syntax_tree/node.rb', line 8597

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



8603
8604
8605
# File 'lib/syntax_tree/node.rb', line 8603

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

#format(q) ⇒ Object



8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
# File 'lib/syntax_tree/node.rb', line 8607

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