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.



10676
10677
10678
10679
10680
# File 'lib/syntax_tree.rb', line 10676

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



10674
10675
10676
# File 'lib/syntax_tree.rb', line 10674

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10671
10672
10673
# File 'lib/syntax_tree.rb', line 10671

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



10668
10669
10670
# File 'lib/syntax_tree.rb', line 10668

def statements
  @statements
end

Instance Method Details

#child_nodesObject



10682
10683
10684
# File 'lib/syntax_tree.rb', line 10682

def child_nodes
  [statements]
end

#format(q) ⇒ Object



10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
# File 'lib/syntax_tree.rb', line 10686

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



10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
# File 'lib/syntax_tree.rb', line 10707

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



10718
10719
10720
10721
10722
10723
10724
10725
# File 'lib/syntax_tree.rb', line 10718

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