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

Statements: 100% (46 / 46)      Branches: 100% (20 / 20)      Functions: 100% (7 / 7)      Lines: 100% (45 / 45)      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 1121 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   9       11 11 8     11         11   11 2     11       1 34   34 20   14     34    
var util = require("../util");
var b    = require("recast").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) {
    util.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);
  }
};
 
exports.ExportDeclaration = function (node, parent) {
  var nodes = [];
 
  if (node.declaration) {
    pushExportDeclaration(node, parent, nodes);
  } else {
    pushExportSpecifiers(node, nodes);
  }
 
  return nodes;
};