Code coverage report for 6to5/transformation/modules/system.js

Statements: 100% (50 / 50)      Branches: 100% (10 / 10)      Functions: 100% (10 / 10)      Lines: 100% (49 / 49)      Ignored: none     

All files » 6to5/transformation/modules/ » system.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 1031   1 1 1 1 1 1   1 12 12   12   12     1   1   1 1 1   1       1   1       1     1 36     1 2     1 39 39 36   3       1 12 12   12           1 12   12               12 12     12   358   358 6 6         12 8 12       12   12    
module.exports = SystemFormatter;
 
var DefaultFormatter = require("./_default");
var AMDFormatter     = require("./amd");
var traverse         = require("../../traverse");
var util             = require("../../util");
var t                = require("../../types");
var _                = require("lodash");
 
function SystemFormatter(file) {
  this.exportIdentifier  = file.generateUidIdentifier("export");
  this.noInteropRequire = true;
 
  AMDFormatter.apply(this, arguments);
 
  this.moduleNameLiteral = t.literal(this.getModuleName());
}
 
util.inherits(SystemFormatter, AMDFormatter);
 
SystemFormatter.prototype.getModuleName = DefaultFormatter.prototype.getModuleName;
 
SystemFormatter.prototype._exportsWildcard = function (objectIdentifier) {
  var leftIdentifier = t.identifier("i");
  var valIdentifier  = t.memberExpression(objectIdentifier, leftIdentifier, true);
 
  var left = t.variableDeclaration("var", [
    t.variableDeclarator(leftIdentifier)
  ]);
 
  var right = objectIdentifier;
 
  var block = t.blockStatement([
    this.buildExportCall(leftIdentifier, valIdentifier)
  ]);
 
  return t.forInStatement(left, right, block);
};
 
SystemFormatter.prototype._exportsAssign = function (id, init) {
  return this.buildExportCall(t.literal(id.name), init, true);
};
 
SystemFormatter.prototype.remapExportAssignment = function (node) {
  return this.buildExportCall(t.literal(node.left.name), node);
};
 
SystemFormatter.prototype.buildExportCall = function (id, init, isStatement) {
  var call = t.callExpression(this.exportIdentifier, [id, init]);
  if (isStatement) {
    return t.expressionStatement(call);
  } else {
    return call;
  }
};
 
SystemFormatter.prototype.buildRunnerSetters = function () {
  return t.arrayExpression(_.map(this.ids, function (uid) {
    var moduleIdentifier = t.identifier("m");
 
    return t.functionExpression(null, [moduleIdentifier], t.blockStatement([
      t.assignmentExpression("=", uid, moduleIdentifier)
    ]));
  }));
};
 
SystemFormatter.prototype.transform = function (ast) {
  var program = ast.program;
 
  var runner = util.template("system", {
    MODULE_NAME: this.moduleNameLiteral,
    MODULE_DEPENDENCIES: t.arrayExpression(this.buildDependencyLiterals()),
    EXPORT_IDENTIFIER: this.exportIdentifier,
    SETTERS: this.buildRunnerSetters(),
    EXECUTE: t.functionExpression(null, [], t.blockStatement(program.body))
  }, true);
 
  var handlerBody = runner.expression.arguments[2].body.body;
  var returnStatement = handlerBody.pop();
 
  // hoist up function declarations for circular references
  traverse(program, {
    enter: function (node) {
      if (t.isFunction(node)) this.stop();
 
      if (t.isFunctionDeclaration(node) || node._blockHoist) {
        handlerBody.push(node);
        this.remove();
      }
    }
  });
 
  if (!_.isEmpty(this.ids)) {
    handlerBody.push(t.variableDeclaration("var", _.map(this.ids, function (uid) {
      return t.variableDeclarator(uid);
    })));
  }
 
  handlerBody.push(returnStatement);
 
  program.body = [runner];
};