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

Statements: 100% (39 / 39)      Branches: 100% (14 / 14)      Functions: 100% (7 / 7)      Lines: 100% (38 / 38)      Ignored: none     

All files » 6to5/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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991   1 1 1   1 174     1   6         1 16     16 3     16     16   16             1 60   60 27   27       33 33 24     33         33   33 6     33       1 48   48 24   3         21               24             1 16 8      
module.exports = CommonJSFormatter;
 
var util = require("../util");
var t    = require("../types");
var b    = require("../builders");
 
function CommonJSFormatter(file) {
  this.file = file;
}
 
CommonJSFormatter.prototype.import = function (node, nodes) {
  // import "foo";
  nodes.push(util.template("require", {
    MODULE_NAME: node.source.raw
  }, true));
};
 
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  var variableName = t.getSpecifierName(specifier);
 
  // import foo from "foo";
  if (specifier.default) {
    specifier.id = b.identifier("default");
  }
 
  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.raw,
    KEY:           specifier.id
  }));
};
 
CommonJSFormatter.prototype.export = function (node, nodes) {
  var declar = node.declaration;
 
  if (node.default) {
    t.ensureExpressionType(declar);
 
    nodes.push(util.template("exports-default", {
      VALUE: declar
    }, true));
  } else {
    var id = declar.id;
    if (declar.type === "VariableDeclaration") {
      id = declar.declarations[0].id;
    }
 
    var assign = util.template("exports-assign", {
      VALUE: id,
      KEY:   id
    }, true);
 
    nodes.push(declar);
 
    if (declar.type === "FunctionDeclaration") {
      assign._blockHoist = true;
    }
 
    nodes.push(assign);
  }
};
 
CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
  var variableName = t.getSpecifierName(specifier);
 
  if (node.source) {
    if (specifier.type === "ExportBatchSpecifier") {
      // export * from "foo";
      nodes.push(util.template("exports-wildcard", {
        OBJECT: getRef()
      }, true));
    } else {
      // export { foo } from "test";
      nodes.push(util.template("exports-assign-key", {
        VARIABLE_NAME: variableName.name,
        OBJECT:        getRef(),
        KEY:           specifier.id
      }, true));
    }
  } else {
    // export { foo };
    nodes.push(util.template("exports-assign", {
      VALUE: specifier.id,
      KEY:   variableName
    }, true));
  }
};
 
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
  return this._exportSpecifier(function () {
    return b.callExpression(b.identifier("require"), [node.source]);
  }, specifier, node, nodes);
};