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

Statements: 100% (35 / 35)      Branches: 100% (12 / 12)      Functions: 100% (7 / 7)      Lines: 100% (32 / 32)      Ignored: none     

All files » 6to5/transformation/modules/ » common.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 303   303 303   10728     303     1   1 16     16 4                   12     12   12               1   6         1 22 9     9     9   9         9   13       1 16 8      
module.exports = CommonJSFormatter;
 
var DefaultFormatter = require("./_default");
var traverse         = require("../../traverse");
var util             = require("../../util");
var t                = require("../../types");
 
function CommonJSFormatter(file) {
  DefaultFormatter.apply(this, arguments);
 
  var hasNonDefaultExports = false;
  traverse(file.ast, {
    enter: function (node) {
      if (t.isExportDeclaration(node) && !node.default) hasNonDefaultExports = true;
    }
  });
  this.hasNonDefaultExports = hasNonDefaultExports;
}
 
util.inherits(CommonJSFormatter, DefaultFormatter);
 
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  var variableName = t.getSpecifierName(specifier);
 
  // import foo from "foo";
  if (t.isSpecifierDefault(specifier)) {
    nodes.push(t.variableDeclaration("var", [
      t.variableDeclarator(variableName,
        t.callExpression(this.file.addDeclaration("interop-require"), [util.template("require", {
          MODULE_NAME: node.source
        })])
      )
    ]));
  } else {
    // import foo from "foo";
 
    var templateName = "require-assign";
 
    // import * as bar from "foo";
    if (specifier.type !== "ImportBatchSpecifier") templateName += "-key";
 
    nodes.push(util.template(templateName, {
      VARIABLE_NAME: variableName,
      MODULE_NAME:   node.source,
      KEY:           specifier.id
    }));
  }
};
 
CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
  // import "foo";
  nodes.push(util.template("require", {
    MODULE_NAME: node.source
  }, true));
};
 
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
  if (node.default) {
    var declar = node.declaration;
 
    // module.exports = VALUE;
    var templateName = "exports-default-module";
 
    // exports = module.exports = VALUE;
    if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
 
    var assign = util.template(templateName, {
      VALUE: this._pushStatement(declar, nodes)
    }, true);
 
    // hoist to the top if this default is a function
    nodes.push(this._hoistExport(declar, assign));
  } else {
    DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
  }
};
 
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
  this._exportSpecifier(function () {
    return t.callExpression(t.identifier("require"), [node.source]);
  }, specifier, node, nodes);
};