Code coverage report for 6to5/lib/6to5/transformation/modules/umd.js

Statements: 100% (39 / 39)      Branches: 100% (10 / 10)      Functions: 100% (3 / 3)      Lines: 100% (35 / 35)      Ignored: none     

All files » 6to5/lib/6to5/transformation/modules/ » umd.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 651   1 1 1 1   1 26     1   1 26 26       26 26 24         26 26 26 26   26       26 26 26 26   26 26 26   26 26 26 24     26 26   26               26 26    
module.exports = UMDFormatter;
 
var AMDFormatter = require("./amd");
var util         = require("../../util");
var t            = require("../../types");
var _            = require("lodash");
 
function UMDFormatter() {
  AMDFormatter.apply(this, arguments);
}
 
util.inherits(UMDFormatter, AMDFormatter);
 
UMDFormatter.prototype.transform = function (ast) {
  var program = ast.program;
  var body    = program.body;
 
  // build an array of module names
 
  var names = [];
  for (var name in this.ids) {
    names.push(t.literal(name));
  }
 
  // factory
 
  var ids  = _.values(this.ids);
  var args = [t.identifier("exports")];
  if (this.passModuleArg) args.push(t.identifier("module"));
  args = args.concat(ids);
 
  var factory = t.functionExpression(null, args, t.blockStatement(body));
 
  // runner
 
  var defineArgs = [t.literal("exports")];
  if (this.passModuleArg) defineArgs.push(t.literal("module"));
  defineArgs = defineArgs.concat(names);
  defineArgs = [t.arrayExpression(defineArgs)];
 
  var testExports = util.template("test-exports");
  var testModule = util.template("test-module");
  var commonTests = this.passModuleArg ? t.logicalExpression("&&", testExports, testModule) : testExports;
 
  var commonArgs = [t.identifier("exports")];
  if (this.passModuleArg) commonArgs.push(t.identifier("module"));
  commonArgs = commonArgs.concat(names.map(function (name) {
    return t.callExpression(t.identifier("require"), [name]);
  }));
 
  var moduleName = this.getModuleName();
  if (moduleName) defineArgs.unshift(t.literal(moduleName));
 
  var runner = util.template("umd-runner-body", {
    AMD_ARGUMENTS: defineArgs,
    COMMON_TEST: commonTests,
    COMMON_ARGUMENTS: commonArgs
  });
 
  //
 
  var call = t.callExpression(runner, [factory]);
  program.body = [t.expressionStatement(call)];
};