コード例 #1
0
ファイル: types.js プロジェクト: ArtanisAce/babel
export function StringLiteral(node: Object, parent: Object) {
  let raw = this.getPossibleRaw(node);
  if (raw != null) {
    this.token(raw);
    return;
  }

  let val = JSON.stringify(node.value);

  // escape illegal js but valid json unicode characters
  val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
    return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
  });

  if (this.format.quotes === "single" && !t.isJSX(parent)) {
    // remove double quotes
    val = val.slice(1, -1);

    // unescape double quotes
    val = val.replace(/\\"/g, '"');

    // escape single quotes
    val = val.replace(/'/g, "\\'");

    // add single quotes
    val = `'${val}'`;
  }

  return this.token(val);
}
コード例 #2
0
ファイル: types.js プロジェクト: shumov/babel
export function _stringLiteral(val: string, parent: Object): string {
  val = JSON.stringify(val);

  // escape illegal js but valid json unicode characters
  val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
    return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
  });

  const hasJSXParent = t.isJSX(parent);

  if (this.format.quotes === "single" && (!hasJSXParent || (hasJSXParent && this.format.jsxQuotes !== "double"))) {
    // remove double quotes
    val = val.slice(1, -1);

    // unescape double quotes
    val = val.replace(/\\"/g, '"');

    // escape single quotes
    val = val.replace(/'/g, "\\'");

    // add single quotes
    val = `'${val}'`;
  }

  return val;
}
コード例 #3
0
ファイル: types.js プロジェクト: BrandsMatic/babel
export function StringLiteral(node: Object, parent: Object) {
  const raw = this.getPossibleRaw(node);
  if (!this.format.minified && raw != null) {
    this.token(raw);
    return;
  }

  // ensure the output is ASCII-safe
  const opts = {
    quotes: t.isJSX(parent) ? "double" : this.format.quotes,
    wrap: true
  };
  if (this.format.jsonCompatibleStrings) {
    opts.json = true;
  }
  const val = jsesc(node.value, opts);

  return this.token(val);
}