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

Statements: 100% (25 / 25)      Branches: 100% (15 / 15)      Functions: 100% (5 / 5)      Lines: 100% (23 / 23)      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 431 1 1   1         205   205 23 3       205 339 6 6 6   6 6   6       205   6 129   123 7 116 10        
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 name = declar.id.name;
        check(declar, name);
 
        declar._ignoreConstant = true;
        constants.push(name);
      });
      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);
    }
  });
};