class Unparser::Emitter::XStr

Dynamic execute string literal emitter

def dispatch

def dispatch
  if heredoc?
    emit_heredoc
  else
    emit_xstr
  end
end

def emit_begin(component)

def emit_begin(component)
  write('#{')
  visit(Util.one(component.children)) if component.children.any?
  write('}')
end

def emit_gvar(component)

def emit_gvar(component)
  write('#')
  write(Util.one(component.children).to_s)
end

def emit_heredoc

def emit_heredoc
  write(%(<<~`HEREDOC`))
  buffer.indent
  nl
  children.each do |child|
    if n_str?(child)
      write(child.children.first)
    else
      emit_begin(child)
    end
  end
  buffer.unindent
  write("HEREDOC\n")
end

def emit_string(value)

def emit_string(value)
  write(escape_xstr(value.children.first))
end

def emit_xstr

def emit_xstr
  write('`')
  children.each do |child|
    if n_begin?(child)
      emit_begin(child)
    elsif n_gvar?(child)
      emit_gvar(child)
    else
      emit_string(child)
    end
  end
  write('`')
end

def escape_xstr(input)

def escape_xstr(input)
  input.chars.map do |char|
    if char.eql?('`')
      '\\`'
    else
      char
    end
  end.join
end

def heredoc?

def heredoc?
  children.any? { |node| node.eql?(s(:str, '')) }
end