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

Statements: 100% (27 / 27)      Branches: 100% (10 / 10)      Functions: 100% (5 / 5)      Lines: 100% (26 / 26)      Ignored: none     

All files » 6to5/transformation/modules/ » common-interop.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 571   1 1 1 1   1 11   11 11 51   11     1   1 16     16 3               13       1 20 8     8         8 8     12     1 16    
module.exports = CommonJSInteropFormatter;
 
var CommonJSFormatter = require("./common");
var util              = require("../../util");
var t                 = require("../../types");
var _                 = require("lodash");
 
function CommonJSInteropFormatter(file) {
  CommonJSFormatter.apply(this, arguments);
 
  var has = false;
  _.each(file.ast.program.body, function (node) {
    if (t.isExportDeclaration(node) && !node.default) has = true;
  });
  this.has = has;
}
 
util.inherits(CommonJSInteropFormatter, CommonJSFormatter);
 
CommonJSInteropFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  var variableName = t.getSpecifierName(specifier);
 
  // import foo from "foo";
   if (specifier.default) {
    nodes.push(t.variableDeclaration("var", [
      t.variableDeclarator(variableName,
        t.callExpression(this.file.addDeclaration("interop-require"), [util.template("require", {
          MODULE_NAME: node.source.raw
        })])
      )
    ]));
  } else {
    CommonJSFormatter.prototype.importSpecifier.apply(this, arguments);
  }
};
 
CommonJSInteropFormatter.prototype.export = function (node, nodes) {
  if (node.default && !this.has) {
    var declar = node.declaration;
 
    // module.exports = VALUE;
    var assign = util.template("exports-default-module", {
      VALUE: this._pushStatement(declar, nodes)
    }, true);
 
    // hoist to the top if this default is a function
    nodes.push(this._hoistExport(declar, assign));
    return;
  }
 
  CommonJSFormatter.prototype.export.apply(this, arguments);
};
 
CommonJSInteropFormatter.prototype.exportSpecifier = function () {
  CommonJSFormatter.prototype.exportSpecifier.apply(this, arguments);
};