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

Statements: 100% (58 / 58)      Branches: 100% (26 / 26)      Functions: 100% (9 / 9)      Lines: 100% (57 / 57)      Ignored: none     

All files » 6to5/transformers/ » modules.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 1341 1 1 1   1 20   20 14 18 18     18 4     18     18   18               6         20     1 14 18   18 10   1         9               8               1 36     1 20   20 9 1 8 1     9       11 11 8     11         11   11 2     11       1   110   110 197 2 2   195       110       1 34   34 20   14     34    
var path = require("path");
var util = require("../util");
var b    = require("ast-types").builders;
var _    = require("lodash");
 
exports.ImportDeclaration = function (node) {
  var nodes = [];
 
  if (node.specifiers.length) {
    _.each(node.specifiers, function (specifier) {
      var variableName = getSpecifierName(specifier);
      var key          = specifier.id.name;
 
      // import foo from "foo";
      if (specifier.type === "ImportDefaultSpecifier") {
        key = b.identifier("default");
      }
 
      var templateName = "require-assign";
 
      // import * as bar from "foo";
      if (specifier.type !== "ImportNamespaceSpecifier") templateName += "-key";
 
      nodes.push(util.template(templateName, {
        VARIABLE_NAME: variableName.name,
        MODULE_NAME:   node.source.raw,
        KEY:           key
      }));
    });
  } else {
    // import "foo";
    nodes.push(util.template("require", {
      MODULE_NAME: node.source.raw
    }, true));
  }
 
  return nodes;
};
 
var pushExportSpecifiers = function (node, nodes) {
  _.each(node.specifiers, function (specifier) {
    var variableName = getSpecifierName(specifier);
 
    if (node.source) {
      if (specifier.type === "ExportBatchSpecifier") {
        // export * from "foo";
        nodes.push(util.template("exports-wildcard", {
          MODULE_NAME: node.source.raw
        }, true));
      } else {
        // export { foo } from "test";
        nodes.push(util.template("exports-require-assign-key", {
          VARIABLE_NAME: variableName.name,
          MODULE_NAME:   node.source.raw,
          KEY:           specifier.id
        }, true));
      }
    } else {
      // export { foo };
      nodes.push(util.template("exports-assign", {
        VALUE: specifier.id,
        KEY:   variableName
      }, true));
    }
  });
};
 
var getSpecifierName = function (specifier) {
  return specifier.name || specifier.id;
};
 
var pushExportDeclaration = function (node, parent, nodes) {
  var declar = node.declaration;
 
  if (node.default) {
    if (declar.type === "FunctionDeclaration") {
      declar.type = "FunctionExpression";
    } else if (declar.type === "ClassDeclaration") {
      declar.type = "ClassExpression";
    }
 
    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._modulesHoist = true;
    }
 
    nodes.push(assign);
  }
};
 
exports.Program = {
  exit: function (node) {
    var unshift = [];
 
    node.body = node.body.filter(function (bodyNode) {
      if (bodyNode._modulesHoist) {
        unshift.push(bodyNode);
        return false;
      } else {
        return true;
      }
    });
 
    node.body = unshift.concat(node.body);
  }
};
 
exports.ExportDeclaration = function (node, parent) {
  var nodes = [];
 
  if (node.declaration) {
    pushExportDeclaration(node, parent, nodes);
  } else {
    pushExportSpecifiers(node, nodes);
  }
 
  return nodes;
};