Code coverage report for 6to5/lib/6to5/transformation/transformers/react.js

Statements: 96.83% (122 / 126)      Branches: 92.86% (65 / 70)      Functions: 100% (11 / 11)      Lines: 99.16% (118 / 119)      Ignored: none     

All files » 6to5/lib/6to5/transformation/transformers/ » react.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240          1 1   1 60 57   3       1 1     1   4 4       1 10     1   14 14       1 25     1   28 28 28   28 28 23 5 2     28 24 12   12       28 28 8 8   8 12   9 9     8 18 18 4 4   14       8   8 4   4 2     4           20     28   28 4 2                   24     26       1   28 28   28 26   26 13   13 24   24 24     24     24 11       24 11     24 2       13 13       13     28 9     28           1 50314   5923 5923     4547 4547     5 5     5 5     5 5   5 5   5 5 5 1 1       5 4       1     55630   55630 17975 17975 37655 14099 14099 23556 23556 23556     55630 7983     55630 50314      
// Based upon the excellent jsx-transpiler by Ingvar Stepanyan (RReverser)
// https://github.com/RReverser/jsx-transpiler
 
// jsx
 
var esutils = require("esutils");
var t       = require("../../types");
 
exports.XJSIdentifier = function (node) {
  if (esutils.keyword.isIdentifierName(node.name)) {
    node.type = "Identifier";
  } else {
    return t.literal(node.name);
  }
};
 
exports.XJSNamespacedName = function (node, parent, file) {
  throw file.errorWithNode(node, "Namespace tags are not supported. ReactJSX is not XML.");
};
 
exports.XJSMemberExpression = {
  exit: function (node) {
    node.computed = t.isLiteral(node.property);
    node.type = "MemberExpression";
  }
};
 
exports.XJSExpressionContainer = function (node) {
  return node.expression;
};
 
exports.XJSAttribute = {
  exit: function (node) {
    var value = node.value || t.literal(true);
    return t.inherits(t.property("init", node.name, value), node);
  }
};
 
var isTag = function(tagName) {
  return (/^[a-z]|\-/).test(tagName);
};
 
exports.XJSOpeningElement = {
  exit: function (node, parent, file) {
    var reactCompat = file.opts.reactCompat;
    var tagExpr = node.name;
    var args = [];
 
    var tagName;
    if (t.isIdentifier(tagExpr)) {
      tagName = tagExpr.name;
    } else if (t.isLiteral(tagExpr)) {
      tagName = tagExpr.value;
    }
 
    if (!reactCompat) {
      if (tagName && isTag(tagName)) {
        args.push(t.literal(tagName));
      } else {
        args.push(tagExpr);
      }
    }
 
    var props = node.attributes;
    if (props.length) {
      var _props = [];
      var objs = [];
 
      var pushProps = function () {
        if (!_props.length) return;
 
        objs.push(t.objectExpression(_props));
        _props = [];
      };
 
      while (props.length) {
        var prop = props.shift();
        if (t.isXJSSpreadAttribute(prop)) {
          pushProps();
          objs.push(prop.argument);
        } else {
          _props.push(prop);
        }
      }
 
      pushProps();
 
      if (objs.length === 1) {
        props = objs[0];
      } else {
        if (!t.isObjectExpression(objs[0])) {
          objs.unshift(t.objectExpression([]));
        }
 
        props = t.callExpression(
          t.memberExpression(t.identifier("React"), t.identifier("__spread")),
          objs
        );
      }
    } else {
      props = t.literal(null);
    }
 
    args.push(props);
 
    if (reactCompat) {
      if (tagName && isTag(tagName)) {
        return t.callExpression(
          t.memberExpression(
            t.memberExpression(t.identifier("React"), t.identifier("DOM")),
            tagExpr,
            t.isLiteral(tagExpr)
          ),
          args
        );
      }
    } else {
      tagExpr = t.memberExpression(t.identifier("React"), t.identifier("createElement"));
    }
 
    return t.callExpression(tagExpr, args);
  }
};
 
exports.XJSElement = {
  exit: function (node) {
    var callExpr = node.openingElement;
    var i;
 
    for (i in node.children) {
      var child = node.children[i];
 
      if (t.isLiteral(child)) {
        var lines = child.value.split(/\r\n|\n|\r/);
 
        for (i in lines) {
          var line = lines[i];
 
          var isFirstLine = i === "0";
          var isLastLine = +i === lines.length - 1;
 
          // replace rendered whitespace tabs with spaces
          var trimmedLine = line.replace(/\t/g, " ");
 
          // trim whitespace touching a newline
          if (!isFirstLine) {
            trimmedLine = trimmedLine.replace(/^[ ]+/, "");
          }
 
          // trim whitespace touching an endline
          if (!isLastLine) {
            trimmedLine = trimmedLine.replace(/[ ]+$/, "");
          }
 
          if (trimmedLine) {
            callExpr.arguments.push(t.literal(trimmedLine));
          }
        }
 
        continue;
      } else Iif (t.isXJSEmptyExpression(child)) {
        continue;
      }
 
      callExpr.arguments.push(child);
    }
 
    if (callExpr.arguments.length >= 3) {
      callExpr._prettyCall = true;
    }
 
    return t.inherits(callExpr, node);
  }
};
 
// display names
 
var addDisplayName = function (id, call) {
  if (!call || !t.isCallExpression(call)) return;
 
  var callee = call.callee;
  if (!t.isMemberExpression(callee)) return;
 
  // not React
  var obj = callee.object;
  if (!t.isIdentifier(obj, { name: "React" })) return;
 
  // not createClass
  var prop = callee.property;
  Iif (!t.isIdentifier(prop, { name: "createClass" })) return;
 
  // no arguments
  var args = call.arguments;
  Iif (args.length !== 1) return;
 
  // not an object
  var first = args[0];
  Iif (!t.isObjectExpression(first)) return;
 
  var props = first.properties;
  var safe = true;
 
  for (var i in props) {
    prop = props[i];
    if (t.isIdentifier(prop.key, { name: "displayName" })) {
      safe = false;
      break;
    }
  }
 
  if (safe) {
    props.unshift(t.property("init", t.identifier("displayName"), t.literal(id)));
  }
};
 
exports.AssignmentExpression =
exports.Property =
exports.VariableDeclarator = function (node) {
  var left, right;
 
  if (t.isAssignmentExpression(node)) {
    left = node.left;
    right = node.right;
  } else if (t.isProperty(node)) {
    left = node.key;
    right = node.value;
  } else Eif (t.isVariableDeclarator(node)) {
    left = node.id;
    right = node.init;
  }
 
  if (t.isMemberExpression(left)) {
    left = left.property;
  }
 
  if (t.isIdentifier(left)) {
    addDisplayName(left.name, right);
  }
};