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

Statements: 100% (52 / 52)      Branches: 100% (12 / 12)      Functions: 100% (9 / 9)      Lines: 100% (51 / 51)      Ignored: none     

All files » 6to5/transformation/modules/ » amd.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 1051   1 1 1 1   1 38 38     1   1 25 25 24   25             1 13 13       13 13       13 13   13   13 13 13   13   13               1 26 2   24       1 90 90   90 54   36       1 18     1 48 48   48   42   8     34     48         1 48 48 24      
module.exports = AMDFormatter;
 
var DefaultFormatter = require("./_default");
var util             = require("../../util");
var t                = require("../../types");
var _                = require("lodash");
 
function AMDFormatter() {
  DefaultFormatter.apply(this, arguments);
  this.ids  = {};
}
 
util.inherits(AMDFormatter, DefaultFormatter);
 
AMDFormatter.prototype.buildDependencyLiterals = function () {
  var names = [];
  for (var name in this.ids) {
    names.push(t.literal(name));
  }
  return names;
};
 
/**
 * Wrap the entire body in a `define` wrapper.
 */
 
AMDFormatter.prototype.transform = function (ast) {
  var program = ast.program;
  var body    = program.body;
 
  // build an array of module names
 
  var names = [t.literal("exports")].concat(this.buildDependencyLiterals());
  names = t.arrayExpression(names);
 
  // build up define container
 
  var params = _.values(this.ids);
  params.unshift(t.identifier("exports"));
 
  var container = t.functionExpression(null, params, t.blockStatement(body));
 
  var defineArgs = [names, container];
  var moduleName = this.getModuleName();
  if (moduleName) defineArgs.unshift(t.literal(moduleName));
 
  var call = t.callExpression(t.identifier("define"), defineArgs);
 
  program.body = [t.expressionStatement(call)];
};
 
/**
 * Get the AMD module name that we'll prepend to the wrapper
 * to define this module
 */
 
AMDFormatter.prototype.getModuleName = function () {
  if (this.file.opts.amdModuleIds) {
    return DefaultFormatter.prototype.getModuleName.apply(this, arguments);
  } else {
    return null;
  }
};
 
AMDFormatter.prototype._push = function (node) {
  var id  = node.source.value;
  var ids = this.ids;
 
  if (ids[id]) {
    return ids[id];
  } else {
    return this.ids[id] = this.file.generateUidIdentifier(id);
  }
};
 
AMDFormatter.prototype.importDeclaration = function (node) {
  this._push(node);
};
 
AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  var key = t.getSpecifierName(specifier);
  var ref = this._push(node);
 
  if (t.isImportBatchSpecifier(specifier)) {
    // import * as bar from "foo";
  } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
    // import foo from "foo";
    ref = t.callExpression(this.file.addDeclaration("interop-require"), [ref]);
  } else {
    // import {foo} from "foo";
    ref = t.memberExpression(ref, specifier.id, false);
  }
 
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(key, ref)
  ]));
};
 
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
  var self = this;
  return this._exportSpecifier(function () {
    return self._push(node);
  }, specifier, node, nodes);
};