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

Statements: 97.37% (37 / 38)      Branches: 95.24% (20 / 21)      Functions: 100% (8 / 8)      Lines: 97.22% (35 / 36)      Ignored: none     

All files » 6to5/transformers/ » 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 651 1 1   1         226   226 34 4       226 386 11 11 11   11 21   21 16 5 3 6   2 2 4             11 16 16     11   11       226   8 187   176 7 169 11        
var traverse = require("../traverse");
var util     = require("../util");
var _        = require("lodash");
 
exports.Program =
exports.BlockStatement =
exports.ForInStatement =
exports.ForOfStatement =
exports.ForStatement = function (node) {
  var constants = [];
 
  var check = function (node, name) {
    if (constants.indexOf(name) >= 0) {
      throw util.errorWithNode(node, name + " is read-only");
    }
  };
 
  _.each(node.body, function (child) {
    if (child && child.type === "VariableDeclaration" && child.kind === "const") {
      _.each(child.declarations, function (declar) {
        var search = [declar.id];
        var names  = [];
 
        while (search.length) {
          var id = search.shift();
 
          if (id.type === "Identifier") {
            names.push(id.name);
          } else if (id.type === "ArrayPattern") {
            _.each(id.elements, function (elem) {
              search.push(elem);
            });
          } else Eif (id.type === "ObjectPattern") {
            _.each(id.properties, function (prop) {
              search.push(prop.value);
            });
          } else {
            throw new Error("unknown node " + id.type);
          }
        }
 
        _.each(names, function (name) {
          check(declar, name);
          constants.push(name);
        });
 
        declar._ignoreConstant = true;
      });
      child.kind = "let";
    }
  });
 
  if (!constants.length) return;
 
  traverse(node, function (child) {
    if (child._ignoreConstant) return;
 
    if (child.type === "VariableDeclarator") {
      check(child, child.id.name);
    } else if (child.type === "AssignmentExpression") {
      check(child, child.left.name);
    }
  });
};