Code coverage report for 6to5/transformers/jsx/index.js

Statements: 92.59% (50 / 54)      Branches: 86.67% (13 / 15)      Functions: 81.82% (9 / 11)      Lines: 94.12% (48 / 51)      Ignored: none     

All files » 6to5/transformers/jsx/ » index.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      1 1 1   1 1   1 143     143 1   1 1       143 142       1 45 44   1       1       1   2 2       1         1 6     1   7 7 7 7       1   21   21 2     21 21 6   15     21       1   21 21 21   21 12 7 2     21 21      
// Based upon the excellent jsx-transpiler by Ingvar Stepanyan (RReverser)
// https://github.com/RReverser/jsx-transpiler
 
var esutils = require("esutils");
var b       = require("recast").types.builders;
var _       = require("lodash");
 
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
var KNOWN_TAGS           = require("./known-tags");
 
exports.Program = function (node, parent, file) {
  var jsx = "React.DOM";
 
  // looking for namespace annotation
  _.each(node.comments, function (comment) {
    Iif (!comment.possiblyLeading) return;
 
    var matches = JSX_ANNOTATION_REGEX.exec(comment.value);
    Eif (matches) jsx = matches[1];
  });
 
  // prebuilding AST node
  file.jsx = jsx.split(".").map(b.identifier).reduce(function (object, property) {
    return b.memberExpression(object, property, false);
  });
};
 
exports.XJSIdentifier = function (node) {
  if (esutils.keyword.isIdentifierName(node.name)) {
    node.type = "Identifier";
  } else {
    return b.literal(node.name);
  }
};
 
exports.XJSNamespacedName = function () {
  throw new Error("Namespace tags are not supported. ReactJSX is not XML.");
};
 
exports.XJSMemberExpression = {
  exit: function (node) {
    node.computed = node.property.type === "Literal";
    node.type = "MemberExpression";
  }
};
 
exports.XJSEmptyExpression = function (node) {
  node.value = null;
  node.type = "Literal";
};
 
exports.XJSExpressionContainer = function (node) {
  return node.expression;
};
 
exports.XJSAttribute = {
  exit: function (node) {
    var value = node.value || b.literal(true);
    var propNode = b.property("init", node.name, value);
    propNode.loc = node.loc;
    return propNode;
  }
};
 
exports.XJSOpeningElement = {
  exit: function (node, parent, file) {
    var tagExpr = node.name;
 
    if (_.contains(KNOWN_TAGS, tagExpr.name)) {
      tagExpr = b.memberExpression(file.jsx, tagExpr, false);
    }
 
    var props = node.attributes;
    if (props.length) {
      props = b.objectExpression(props);
    } else {
      props = b.literal(null);
    }
 
    return b.callExpression(tagExpr, [props]);
  }
};
 
exports.XJSElement = {
  exit: function (node) {
    var callExpr = node.openingElement;
    var children = node.children;
    var args     = callExpr.arguments;
 
    switch (children.length) {
      case 0: break;
      case 1: args.push(children[0]); break;
      default: args.push(b.arrayExpression(children));
    }
 
    callExpr.loc = node.loc;
    return callExpr;
  }
};