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

Statements: 100% (25 / 25)      Branches: 100% (18 / 18)      Functions: 100% (6 / 6)      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 43 44 45 461 1 1   1         340   340 40 6       340 620 13 13 18 18     13   13       340   10 197   184     11 173 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) {
        _.each(util.getIds(declar.id), 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" ||
        child.type === "FunctionDeclaration" ||
        child.type === "ClassDeclaration") {
      check(child, child.id.name);
    } else if (child.type === "AssignmentExpression") {
      check(child, child.left.name);
    }
  });
};