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

Statements: 100% (48 / 48)      Branches: 100% (24 / 24)      Functions: 100% (7 / 7)      Lines: 100% (46 / 46)      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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331   1 1   1 198     1   6             1 16     16 3     16     16   16                 1 60   60 27   27 12 6 6       27           33   33 24   24 18               24   9             9 9   9 6           1 48   48 48   48 24   3             21                   24                 1 16 8      
module.exports = CommonJSFormatter;
 
var util = require("../../util");
var t    = require("../../types");
 
function CommonJSFormatter(file) {
  this.file = file;
}
 
CommonJSFormatter.prototype.import = function (node, nodes) {
  // import "foo";
  nodes.push(util.template("require", {
    //inherits: node,
 
    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 = t.identifier("default");
  }
 
  var templateName = "require-assign";
 
  // import * as bar from "foo";
  if (specifier.type !== "ImportBatchSpecifier") templateName += "-key";
 
  nodes.push(util.template(templateName, {
    //inherits: node.specifiers.length === 1 && node,
 
    VARIABLE_NAME: variableName,
    MODULE_NAME:   node.source.raw,
    KEY:           specifier.id
  }));
};
 
CommonJSFormatter.prototype.export = function (node, nodes) {
  var declar = node.declaration;
 
  if (node.default) {
    var ref = declar;
 
    if (t.isClass(ref) || t.isFunction(ref)) {
      if (ref.id) {
        nodes.push(t.toStatement(ref));
        ref = ref.id;
      }
    }
 
    nodes.push(util.template("exports-default", {
      //inherits: node,
 
      VALUE: ref
    }, true));
  } else {
    var assign;
 
    if (t.isVariableDeclaration(declar)) {
      var decl = declar.declarations[0];
 
      if (decl.init) {
        decl.init = util.template("exports-assign", {
          //inherits: node,
 
          VALUE: decl.init,
          KEY:   decl.id
        });
      }
 
      nodes.push(declar);
    } else {
      assign = util.template("exports-assign", {
        //inherits: node,
 
        VALUE: declar.id,
        KEY:   declar.id
      }, true);
 
      nodes.push(t.toStatement(declar));
      nodes.push(assign);
 
      if (t.isFunctionDeclaration(declar)) {
        assign._blockHoist = true;
      }
    }
  }
};
 
CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
  var variableName = t.getSpecifierName(specifier);
 
  var inherits = false;
  if (node.specifiers.length === 1) inherits = node;
 
  if (node.source) {
    if (t.isExportBatchSpecifier(specifier)) {
      // export * from "foo";
      nodes.push(util.template("exports-wildcard", {
        //inherits: inherits,
 
        OBJECT: getRef()
      }, true));
    } else {
      // export { foo } from "test";
      nodes.push(util.template("exports-assign-key", {
        //inherits: inherits,
 
        VARIABLE_NAME: variableName.name,
        OBJECT:        getRef(),
        KEY:           specifier.id
      }, true));
    }
  } else {
    // export { foo };
    nodes.push(util.template("exports-assign", {
      //inherits: inherits,
 
      VALUE: specifier.id,
      KEY:   variableName
    }, true));
  }
};
 
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
  return this._exportSpecifier(function () {
    return t.callExpression(t.identifier("require"), [node.source]);
  }, specifier, node, nodes);
};