Code coverage report for 6to5/traverse/scope.js

Statements: 94.52% (69 / 73)      Branches: 96.67% (58 / 60)      Functions: 94.12% (16 / 17)      Lines: 94.03% (63 / 67)      Ignored: none     

All files » 6to5/traverse/ » scope.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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511   1 1 1   1                   1 49948 49948   49948     1 6797 6089     1 49948 49948   2527   2527 6797         2527 119 238 238     119         2527 1753   2893           2527 40         2527 1188 24549 100 200 200           24549   24056 4624         24056 776             2527 719 719 450       2527     1 68   68         68 68 68                   1       1 156     1 15757     1 213     1 15581     1 15601     1 15002    
module.exports = Scope;
 
var traverse = require("./index");
var t        = require("../types");
var _        = require("lodash");
 
var FOR_KEYS = ["left", "init"];
 
/**
 * This searches the current "scope" and collects all references/declarations
 * within.
 *
 * @param {Node} block
 * @param {Scope} [parent]
 */
 
function Scope(block, parent) {
  this.parent = parent;
  this.block  = block;
 
  this.references = this.getReferences();
}
 
Scope.add = function (node, references) {
  if (!node) return;
  _.merge(references, t.getIds(node, true));
};
 
Scope.prototype.getReferences = function () {
  var block = this.block;
  if (block._scopeReferences) return block._scopeReferences;
 
  var references = block._scopeReferences = {};
 
  var add = function (node) {
    Scope.add(node, references);
  };
 
  // ForStatement - left, init
 
  if (t.isFor(block)) {
    _.each(FOR_KEYS, function (key) {
      var node = block[key];
      if (t.isLet(node)) add(node);
    });
 
    block = block.body;
  }
 
  // Program, BlockStatement - let variables
 
  if (t.isBlockStatement(block) || t.isProgram(block)) {
    _.each(block.body, function (node) {
      // check for non-var `VariableDeclaration`s
      if (t.isLet(node)) add(node);
    });
  }
 
  // CatchClause - param
 
  if (t.isCatchClause(block)) {
    add(block.param);
  }
 
  // Program, Function - var variables
 
  if (t.isProgram(block) || t.isFunction(block)) {
    traverse(block, function (node, parent, scope) {
      if (t.isFor(node)) {
        _.each(FOR_KEYS, function (key) {
          var declar = node[key];
          if (t.isVar(declar)) add(declar);
        });
      }
 
      // this block is a function so we'll stop since none of the variables
      // declared within are accessible
      if (t.isFunction(node)) return false;
 
      if (t.isIdentifier(node) && t.isReferenced(node, parent) && !scope.has(node.name)) {
        add(node);
      }
 
      // we've ran into a declaration!
      // we'll let the BlockStatement scope deal with `let` declarations
      if (t.isDeclaration(node) && !t.isLet(node)) {
        add(node);
      }
    }, { scope: this });
  }
 
  // Function - params, rest
 
  if (t.isFunction(block)) {
    add(block.rest);
    _.each(block.params, function (param) {
      add(param);
    });
  }
 
  return references;
};
 
Scope.prototype.push = function (opts) {
  var block = this.block;
 
  Iif (t.isFor(block) || t.isCatchClause(block) || t.isFunction(block)) {
    t.ensureBlock(block);
    block = block.body;
  }
 
  Eif (t.isBlockStatement(block) || t.isProgram(block)) {
    block._declarations = block._declarations || {};
    block._declarations[opts.key] = {
      kind: opts.kind,
      id: opts.id,
      init: opts.init
    };
  } else {
    throw new TypeError("cannot add a declaration here in node type " + block.type);
  }
};
 
Scope.prototype.add = function (node) {
  Scope.add(node, this.references);
};
 
Scope.prototype.get = function (id) {
  return id && (this.getOwn(id) || this.parentGet(id));
};
 
Scope.prototype.getOwn = function (id) {
  return _.has(this.references, id) && this.references[id];
};
 
Scope.prototype.parentGet = function (id) {
  return this.parent && this.parent.get(id);
};
 
Scope.prototype.has = function (id) {
  return id && (this.hasOwn(id) || this.parentHas(id));
};
 
Scope.prototype.hasOwn = function (id) {
  return !!this.getOwn(id);
};
 
Scope.prototype.parentHas = function (id) {
  return this.parent && this.parent.has(id);
};