src/codegeneration/TemplateLiteralTransformer.js
function toCookedString(s) {
  let sb = ['"'];
  let i = 0, k = 1, c, c2;
  while (i < s.length) {
    c = s[i++];
    switch (c) {
      case '\\':
        c2 = s[i++];
        switch (c2) {
          // Strip line continuation.
          case '\n':
          case '\u2028':
          case '\u2029':
            break;
          case '\r':
            // \ \r \n should be stripped as one
            if (s[i + 1] === '\n') {
              i++;
            }
            break;

          default:
            sb[k++] = c;
            sb[k++] = c2;
        }
        break;

      // Since we wrap the string in " we need to escape those.
      case '"':
        sb[k++] = '\\"';
        break;

      // Whitespace
      case '\n':
        sb[k++] = '\\n';
        break;
      // <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF>
      // for both TV and TRV.
      case '\r':
        if (s[i] === '\n')
          i++;
        sb[k++] = '\\n';
        break;
      case '\t':
        sb[k++] = '\\t';
        break;
      case '\f':
        sb[k++] = '\\f';
        break;
      case '\b':
        sb[k++] = '\\b';
        break;
      case '\u2028':
        sb[k++] = '\\u2028';
        break;
      case '\u2029':
        sb[k++] = '\\u2029';
        break;

      default:
        sb[k++] = c;
    }
  }

  sb[k++] = '"';
  return sb.join('');
}
function toCookedString(s) {
  let sb = ['"'];
  let i = 0, k = 1, c, c2;
  while (i < s.length) {
    c = s[i++];
    switch (c) {
      case '\\':
        c2 = s[i++];
        switch (c2) {
          // Strip line continuation.
          case '\n':
          case '\u2028':
          case '\u2029':
            break;
          case '\r':
            // \ \r \n should be stripped as one
            if (s[i + 1] === '\n') {
              i++;
            }
            break;

          default:
            sb[k++] = c;
            sb[k++] = c2;
        }
        break;

      // Since we wrap the string in " we need to escape those.
      case '"':
        sb[k++] = '\\"';
        break;

      // Whitespace
      case '\n':
        sb[k++] = '\\n';
        break;
      // <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF>
      // for both TV and TRV.
      case '\r':
        if (s[i] === '\n')
          i++;
        sb[k++] = '\\n';
        break;
      case '\t':
        sb[k++] = '\\t';
        break;
      case '\f':
        sb[k++] = '\\f';
        break;
      case '\b':
        sb[k++] = '\\b';
        break;
      case '\u2028':
        sb[k++] = '\\u2028';
        break;
      case '\u2029':
        sb[k++] = '\\u2029';
        break;

      default:
        sb[k++] = c;
    }
  }

  sb[k++] = '"';
  return sb.join('');
}
function toCookedString ( s ) { let sb = [ '"' ] ; let i = 0 , k = 1 , c , c2 ; while ( i < s . length ) { c = s [ i ++ ] ; switch ( c ) { case '\\' : c2 = s [ i ++ ] ; switch ( c2 ) { // Strip line continuation. case '\n' : case '\u2028' : case '\u2029' : break ; case '\r' : // \ \r \n should be stripped as one if ( s [ i + 1 ] === '\n' ) { i ++ ; } break ; default : sb [ k ++ ] = c ; sb [ k ++ ] = c2 ; } break ; // Since we wrap the string in " we need to escape those. case '"' : sb [ k ++ ] = '\\"' ; break ; // Whitespace case '\n' : sb [ k ++ ] = '\\n' ; break ; // <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF> // for both TV and TRV. case '\r' : if ( s [ i ] === '\n' ) i ++ ; sb [ k ++ ] = '\\n' ; break ; case '\t' : sb [ k ++ ] = '\\t' ; break ; case '\f' : sb [ k ++ ] = '\\f' ; break ; case '\b' : sb [ k ++ ] = '\\b' ; break ; case '\u2028' : sb [ k ++ ] = '\\u2028' ; break ; case '\u2029' : sb [ k ++ ] = '\\u2029' ; break ; default : sb [ k ++ ] = c ; } } sb [ k ++ ] = '"' ; return sb . join ( '' ) ; }
Takes a raw string and returns a string that is suitable for the cooked
value. This involves removing line continuations, escaping double quotes
and escaping whitespace.
@param {string} s
@return {string}
Takes a raw string and returns a string that is suitable for the cooked value . This involves removing line continuations escaping double quotes and escaping whitespace .