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

Statements: 100% (118 / 118)      Branches: 100% (60 / 60)      Functions: 100% (10 / 10)      Lines: 100% (113 / 113)      Ignored: none     

All files » 6to5/transformation/transformers/ » classes.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 2101 1 1 1   1 28         1 6     1 2 3   2     1 34 34   34 34   34 8 2 6 1 1       34       34 34 34   34   34   34 8   8 8     34                 33   17   16 16       1 34 34 34 34 34 34   34 34 34   34   34 26 26   26   26 10 9 9   1     16 16   16   16 7 7     16       33 4         33 33   33 8       8     33 3     33 10   10 10   10           1 20   20 1 19   9   9   3   6     6 3     6 6   10   8   2       1 26   26   26 293 20 273 19 19 9     6 6         1 9 9 9 9   9    
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
var _        = require("lodash");
 
exports.ClassDeclaration = function (node, parent, file, scope) {
  return t.variableDeclaration("var", [
    t.variableDeclarator(node.id, buildClass(node, file, scope))
  ]);
};
 
exports.ClassExpression = function (node, parent, file, scope) {
  return buildClass(node, file, scope);
};
 
var getMemberExpressionObject = function (node) {
  while (t.isMemberExpression(node)) {
    node = node.object;
  }
  return node;
};
 
var buildClass = function (node, file, scope) {
  var superName = node.superClass;
  var className = node.id || t.identifier(file.generateUid("class", scope));
 
  var superClassArgument = node.superClass;
  var superClassCallee   = node.superClass;
 
  if (superName) {
    if (t.isMemberExpression(superName)) {
      superClassArgument = superClassCallee = getMemberExpressionObject(superName);
    } else if (!t.isIdentifier(superName)) {
      superClassArgument = superName;
      superClassCallee   = superName = t.identifier(file.generateUid("ref", scope));
    }
  }
 
  var container = util.template("class", {
    CLASS_NAME: className
  });
 
  var block       = container.callee.expression.body;
  var body        = block.body;
  var constructor = body[0].declarations[0].init;
 
  if (node.id) constructor.id = className;
 
  var returnStatement = body.pop();
 
  if (superName) {
    body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
 
    container.arguments.push(superClassArgument);
    container.callee.expression.params.push(superClassCallee);
  }
 
  buildClassBody({
    file:        file,
    body:        body,
    node:        node,
    className:   className,
    superName:   superName,
    constructor: constructor,
  });
 
  if (body.length === 1) {
    // only a constructor so no need for a closure container
    return constructor;
  } else {
    body.push(returnStatement);
    return container;
  }
};
 
var buildClassBody = function (opts) {
  var file        = opts.file;
  var body        = opts.body;
  var node        = opts.node;
  var constructor = opts.constructor;
  var className   = opts.className;
  var superName   = opts.superName;
 
  var instanceMutatorMap = {};
  var staticMutatorMap   = {};
  var hasConstructor     = false;
 
  var classBody = node.body.body;
 
  _.each(classBody, function (node) {
    var methodName = node.key;
    var method     = node.value;
 
    replaceInstanceSuperReferences(superName, node);
 
    if (node.key.name === "constructor") {
      if (node.kind === "") {
        hasConstructor = true;
        addConstructor(constructor, method);
      } else {
        throw file.errorWithNode(node, "illegal kind for constructor method");
      }
    } else {
      var mutatorMap = instanceMutatorMap;
      if (node.static) mutatorMap = staticMutatorMap;
 
      var kind = node.kind;
 
      if (kind === "") {
        kind = "value";
        util.pushMutatorMap(mutatorMap, methodName, "writable", t.identifier("true"));
      }
 
      util.pushMutatorMap(mutatorMap, methodName, kind, node);
    }
  });
 
  if (!hasConstructor && superName) {
    constructor.body.body.push(util.template("class-super-constructor-call", {
      SUPER_NAME: superName
    }, true));
  }
 
  var instanceProps;
  var staticProps;
 
  if (!_.isEmpty(instanceMutatorMap)) {
    var protoId = util.template("prototype-identifier", {
      CLASS_NAME: className
    });
 
    instanceProps = util.buildDefineProperties(instanceMutatorMap, protoId);
  }
 
  if (!_.isEmpty(staticMutatorMap)) {
    staticProps = util.buildDefineProperties(staticMutatorMap, className);
  }
 
  if (instanceProps || staticProps) {
    staticProps = staticProps || t.literal(null);
 
    var args = [className, staticProps];
    if (instanceProps) args.push(instanceProps);
 
    body.push(t.expressionStatement(
      t.callExpression(file.addDeclaration("class-props"), args)
    ));
  }
};
 
var superIdentifier = function (superName, methodNode, node, parent) {
  var methodName = methodNode.key;
 
  if (parent.property === node) {
    return;
  } else if (t.isCallExpression(parent, { callee: node })) {
    // super(); -> ClassName.prototype.MethodName.call(this);
    parent.arguments.unshift(t.thisExpression());
 
    if (methodName.name === "constructor") {
      // constructor() { super(); }
      return t.memberExpression(superName, t.identifier("call"));
    } else {
      node = superName;
 
      // foo() { super(); }
      if (!methodNode.static) {
        node = t.memberExpression(node, t.identifier("prototype"));
      }
 
      node = t.memberExpression(node, methodName, methodNode.computed);
      return t.memberExpression(node, t.identifier("call"));
    }
  } else if (t.isMemberExpression(parent) && !methodNode.static) {
    // super.test -> ClassName.prototype.test
    return t.memberExpression(superName, t.identifier("prototype"));
  } else {
    return superName;
  }
};
 
var replaceInstanceSuperReferences = function (superName, methodNode) {
  var method = methodNode.value;
 
  superName = superName || t.identifier("Function");
 
  traverse(method, function (node, parent) {
    if (t.isIdentifier(node, { name: "super" })) {
      return superIdentifier(superName, methodNode, node, parent);
    } else if (t.isCallExpression(node)) {
      var callee = node.callee;
      if (!t.isMemberExpression(callee)) return;
      if (callee.object.name !== "super") return;
 
      //  super.test(); -> ClassName.prototype.MethodName.call(this);
      callee.property.name = callee.property.name + ".call";
      node.arguments.unshift(t.thisExpression());
    }
  });
};
 
var addConstructor = function (construct, method) {
  construct.defaults = method.defaults;
  construct.params   = method.params;
  construct.body     = method.body;
  construct.rest     = method.rest;
 
  t.inherits(construct, method);
};