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

Statements: 97% (97 / 100)      Branches: 92.59% (50 / 54)      Functions: 100% (13 / 13)      Lines: 100% (93 / 93)      Ignored: none     

All files » 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          1 1 1   1 50 49   1       1 1     1   3 3       1 10     1   12 12       1   24 24   24 24 21 3 1     24 12   12     24 24 6 6   6 10   7 7     6 16 16 4 4   12       6   6 2   4 2     4           18     24   24 24       1   24   24 21     24 13     24           1 660   95 95     48 48     5 5     5 5     5 5   5 5   5 5 1       5 4       1     666   666 165 165 501 83 83 418 418 418     666 127     666 660      
// 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");
var _       = require("lodash");
 
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.property("init", node.name, value);
  }
};
 
exports.XJSOpeningElement = {
  exit: function (node) {
    var tagExpr = node.name;
    var args = [];
 
    var tagName;
    if (t.isIdentifier(tagExpr)) {
      tagName = tagExpr.name;
    } else if (t.isLiteral(tagExpr)) {
      tagName = tagExpr.value;
    }
 
    if (tagName && (/[a-z]/.exec(tagName[0]) || _.contains(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);
 
    tagExpr = t.memberExpression(t.identifier("React"), t.identifier("createElement"));
    return t.callExpression(tagExpr, args);
  }
};
 
exports.XJSElement = {
  exit: function (node) {
    var callExpr = node.openingElement;
 
    var childrenToRender = node.children.filter(function(child) {
      return !(t.isLiteral(child) && _.isString(child.value) && child.value.match(/^[ \t]*[\r\n][ \t\r\n]*$/));
    });
 
    _.each(childrenToRender, function (child) {
      callExpr.arguments.push(child);
    });
 
    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;
 
  _.each(props, function (prop) {
    if (t.isIdentifier(prop.key, { name: "displayName" })) {
      return safe = false;
    }
  });
 
  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);
  }
};