asInterpolation method

Interpolation asInterpolation({
  1. bool static = false,
  2. int? quote,
})

Interpolation that, when evaluated, produces the syntax of this string.

Unlike text, his doesn't resolve escapes and does include quotes for quoted strings.

If static is true, this escapes any #{ sequences in the string. If quote is passed, it uses that character as the quote mark; otherwise, it determines the best quote to add by looking at the string.

Implementation

Interpolation asInterpolation({bool static = false, int? quote}) {
  if (!hasQuotes) return text;

  quote ??= _bestQuote(text.contents.whereType<String>());
  var buffer = InterpolationBuffer();
  buffer.writeCharCode(quote);
  for (var i = 0; i < text.contents.length; i++) {
    var value = text.contents[i];
    assert(value is Expression || value is String);
    switch (value) {
      case Expression():
        buffer.add(value, text.spanForElement(i));
      case String():
        _quoteInnerText(value, quote, buffer, static: static);
    }
  }
  buffer.writeCharCode(quote);

  return buffer.interpolation(text.span);
}