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

Statements: 100% (47 / 47)      Branches: 94.87% (37 / 39)      Functions: 100% (12 / 12)      Lines: 100% (46 / 46)      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 801   1 1 1   1 53301 53301 53301   53301     1 106602 106602   17757 17757   17757 5349 5932 38     12408 12406 101240 67     101173 2884 98289 3953     2 2     17757 4080 3916       17757     1 6840     1 74     1 265     1 112     1 184     1 191     1 184    
module.exports = Scope;
 
var traverse = require("./index");
var t        = require("../types");
var _        = require("lodash");
 
function Scope(parent, block) {
  this.parent = parent;
  this.block  = block;
  this.ids    = this.getIds();
 
  this.getIds();
}
 
Scope.prototype.getIds = function () {
  var block = this.block;
  if (block._scopeIds) return block._scopeIds;
 
  var self = this;
  var ids  = block._scopeIds = {};
 
  if (t.isBlockStatement(block)) {
    _.each(block.body, function (node) {
      if (t.isVariableDeclaration(node) && node.kind !== "var") {
        self.add(node, ids);
      }
    });
  } else if (t.isProgram(block) || t.isFunction(block)) {
    traverse(block, function (node, parent) {
      if (parent !== block && t.isVariableDeclaration(node) && node.kind !== "var") {
        return;
      }
 
      if (t.isDeclaration(node)) {
        self.add(node, ids);
      } else if (t.isFunction(node)) {
        return false;
      }
    });
  } else Eif (t.isCatchClause(block)) {
    self.add(block.param, ids);
  }
 
  if (t.isFunction(block)) {
    _.each(block.params, function (param) {
      self.add(param, ids);
    });
  }
 
  return ids;
};
 
Scope.prototype.add = function (node, ids) {
  _.merge(ids || this.ids, t.getIds(node, true));
};
 
Scope.prototype.get = function (id) {
  return id && (this.getOwn(id) || this.parentGet(id));
};
 
Scope.prototype.getOwn = function (id) {
  return _.has(this.ids, id) && this.ids[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);
};