Code coverage report for 6to5/lib/6to5/transformation/transformers/es6-constants.js

Statements: 95.65% (44 / 46)      Branches: 89.29% (25 / 28)      Functions: 100% (5 / 5)      Lines: 97.5% (39 / 40)      Ignored: none     

All files » 6to5/lib/6to5/transformation/transformers/ » es6-constants.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 831 1 1   1         68242 68242             68242 193 133 133 7   7 2 2     6       68242 193             68242 158247       158247 35 38   38 38 38   38 38 38   38 38     38     35 35       68242   23   925 852   772 155          
var traverse = require("../../traverse");
var t        = require("../../types");
var _        = require("lodash");
 
exports.Program =
exports.BlockStatement =
exports.ForInStatement =
exports.ForOfStatement =
exports.ForStatement = function (node, parent, file) {
  var hasConstants = false;
  var constants = {};
 
  /**
   * Check the results of `util.getIds` as `names` generated from a
   * node against it's parent.
   */
 
  var check = function (parent, names, scope) {
    for (var name in names) {
      var nameNode = names[name];
      if (!_.has(constants, name)) continue;
      Iif (parent && t.isBlockStatement(parent) && parent !== constants[name]) continue;
 
      if (scope) {
        var defined = scope.get(name);
        if (defined && defined === nameNode) continue;
      }
 
      throw file.errorWithNode(nameNode, name + " is read-only");
    }
  };
 
  var getIds = function (node) {
    return t.getIds(node, true, ["MemberExpression"]);
  };
 
  /**
   * Collect all constants in this scope.
   */
 
  _.each(node.body, function (child, parent) {
    Iif (t.isExportDeclaration(child)) {
      child = child.declaration;
    }
 
    if (t.isVariableDeclaration(child, { kind: "const" })) {
      for (var i in child.declarations) {
        var declar = child.declarations[i];
 
        var ids = getIds(declar);
        for (var name in ids) {
          var nameNode = ids[name];
 
          var names = {};
          names[name] = nameNode;
          check(parent, names);
 
          constants[name] = parent;
          hasConstants = true;
        }
 
        declar._ignoreConstant = true;
      }
 
      child._ignoreConstant = true;
      child.kind = "let";
    }
  });
 
  if (!hasConstants) return;
 
  traverse(node, {
    enter: function (child, parent, scope) {
      if (child._ignoreConstant) return;
      if (t.isVariableDeclaration(child)) return;
 
      if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
        check(parent, getIds(child), scope);
      }
    }
  });
};