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

Statements: 94.87% (74 / 78)      Branches: 92.86% (39 / 42)      Functions: 100% (10 / 10)      Lines: 94.67% (71 / 75)      Ignored: none     

All files » 6to5/transformation/modules/ » _default.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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641   1 1 1 1   1 303 303   303 303     1 303   303 10713 10713 13       303     1 303   303 10583   10549 97 97 97 1                           1 2 2 2   2       2       2             2   2   2     1 27 12 6 6       27     1 18 7     18     1 48   48 48   48 24   3         21               24             1 57   57 18       39   39 30 33   33         33 33 33     9         9 9   9        
module.exports = DefaultFormatter;
 
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
var _        = require("lodash");
 
function DefaultFormatter(file) {
  this.exports = [];
  this.file    = file;
 
  this.localExports = this.getLocalExports();
  this.remapAssignments();
}
 
DefaultFormatter.prototype.getLocalExports = function () {
  var localExports = {};
 
  traverse(this.file.ast, function (node) {
    var declar = node && node.declaration;
    if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) {
      _.extend(localExports, t.getIds(declar, true));
    }
  });
 
  return localExports;
};
 
DefaultFormatter.prototype.remapAssignments = function () {
  var localExports = this.localExports;
 
  traverse(this.file.ast, function (node, parent, scope) {
    if (t.isExportDeclaration(node)) return false;
 
    if (t.isAssignmentExpression(node)) {
      var left = node.left;
      var name = left.name;
      if (t.isIdentifier(left) && localExports[name] && localExports[name] === scope.get(name, true)) {
        return t.assignmentExpression(
          "=",
          left,
          t.assignmentExpression(
            node.operator,
            t.memberExpression(t.identifier("exports"), left),
            node.right
          )
        );
      }
    }
  });
};
 
DefaultFormatter.prototype.getModuleName = function () {
  var opts = this.file.opts;
  var filenameRelative = opts.filenameRelative;
  var moduleName = "";
 
  Iif (opts.moduleRoot) {
    moduleName = opts.moduleRoot + "/";
  }
 
  Iif (!opts.filenameRelative) {
    return moduleName + opts.filename.replace(/^\//, "");
  }
 
  Iif (opts.sourceRoot) {
    // remove sourceRoot from filename
    var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "\/?");
    filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
  }
 
  // remove extension
  filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
 
  moduleName += filenameRelative;
 
  return moduleName;
};
 
DefaultFormatter.prototype._pushStatement = function (ref, nodes) {
  if (t.isClass(ref) || t.isFunction(ref)) {
    if (ref.id) {
      nodes.push(t.toStatement(ref));
      ref = ref.id;
    }
  }
 
  return ref;
};
 
DefaultFormatter.prototype._hoistExport = function (declar, assign) {
  if (t.isFunctionDeclaration(declar)) {
    assign._blockHoist = true;
  }
 
  return assign;
};
 
DefaultFormatter.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", {
        OBJECT: getRef()
      }, true));
    } else {
      // export { foo } from "test";
      nodes.push(util.template("exports-assign-key", {
        VARIABLE_NAME: variableName,
        OBJECT:        getRef(),
        KEY:           specifier.id
      }, true));
    }
  } else {
    // export { foo };
    nodes.push(util.template("exports-assign", {
      VALUE: specifier.id,
      KEY:   variableName
    }, true));
  }
};
 
DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
  var declar = node.declaration;
 
  if (node.default) {
    nodes.push(util.template("exports-default", {
      VALUE: this._pushStatement(declar, nodes)
    }, true));
  } else {
    var assign;
 
    if (t.isVariableDeclaration(declar)) {
      for (var i in declar.declarations) {
        var decl = declar.declarations[i];
 
        decl.init = util.template("exports-assign", {
          VALUE: decl.init,
          KEY:   decl.id
        });
 
        var newDeclar = t.variableDeclaration(declar.kind, [decl]);
        if (i == 0) t.inherits(newDeclar, declar);
        nodes.push(newDeclar);
      }
    } else {
      assign = util.template("exports-assign", {
        VALUE: declar.id,
        KEY:   declar.id
      }, true);
 
      nodes.push(t.toStatement(declar));
      nodes.push(assign);
 
      this._hoistExport(declar, assign);
    }
  }
};