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

Statements: 100% (43 / 43)      Branches: 100% (6 / 6)      Functions: 100% (8 / 8)      Lines: 100% (43 / 43)      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 831   1 1 1 1   1 11 11     1   1 11 11       11 11 12   11       11 11   11 11   11     1 60 60   60 36   24       1 12     1 32 32     32 6     32   32   4     28     32         1 32 32 16      
module.exports = AMDFormatter;
 
var CommonJSFormatter = require("./common");
var util              = require("../../util");
var t                 = require("../../types");
var _                 = require("lodash");
 
function AMDFormatter(file) {
  this.file = file;
  this.ids  = {};
}
 
util.inherits(AMDFormatter, CommonJSFormatter);
 
AMDFormatter.prototype.transform = function (ast) {
  var program = ast.program;
  var body    = program.body;
 
  // build an array of module names
 
  var names = [t.literal("exports")];
  _.each(this.ids, function (id, name) {
    names.push(t.literal(name));
  });
  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 call = t.callExpression(t.identifier("define"), [names, container]);
 
  program.body = [t.expressionStatement(call)];
};
 
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] = t.identifier(this.file.generateUid(id));
  }
};
 
AMDFormatter.prototype.import = function (node) {
  this._push(node);
};
 
AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  var key = t.getSpecifierName(specifier);
  var id  = specifier.id;
 
  // import foo from "foo";
  if (specifier.default) {
    id = t.identifier("default");
  }
 
  var ref;
 
  if (t.isImportBatchSpecifier(specifier)) {
    // import * as bar from "foo";
    ref = this._push(node);
  } else {
    // import foo from "foo";
    ref = t.memberExpression(this._push(node), 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);
};