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

Statements: 95.8% (114 / 119)      Branches: 95.6% (87 / 91)      Functions: 95.24% (20 / 21)      Lines: 95.45% (105 / 110)      Ignored: none     

All files » 6to5/lib/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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 3101   1 1 1   1                   1 58146 58146   58146 58146 58146     1   1             1 9853 8201                   1 6 6       6                       1 110   110 1 109 17 92       110   110 222 44 44 178 129 49 8 41 24       110   110 110   110                     1 39 10     29 29       29     1 58146 58146   2653 2653 2653   2653 7209 7209         2653 130 260 260     130         2653 1804   3143           2653 7         2653 1240   22089 75 150 150           22089     21548   21327 4565         21327 1017               2653 842 842 662       2653                 1 220   220         220 220 220                               1                     1 176                   1 17276 17276 17276                   1 138                     1 17077                       1 17100                     1 16808    
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;
 
  var info = this.getInfo();
  this.references = info.references;
  this.declarations = info.declarations;
}
 
var vars = require("jshint/src/vars");
 
Scope.defaultDeclarations = _.flatten([
  vars.newEcmaIdentifiers,
  vars.node,
  vars.ecmaIdentifiers,
  vars.reservedVars
].map(_.keys));
 
Scope.add = function (node, references) {
  if (!node) return;
  _.defaults(references, t.getIds(node, true));
};
 
/**
 * Description
 *
 * @param {File} file
 * @param {String} [name="temp"]
 */
 
Scope.prototype.generateTemp = function (file, name) {
  var id = file.generateUidIdentifier(name || "temp", this);
  this.push({
    key: id.name,
    id: id
  });
  return id;
};
 
/*
 * Description
 *
 * @param {Object} parent
 * @param {File} file
 * @param {Scope} scope
 * @returns {Object}
 */
 
Scope.prototype.generateUidBasedOnNode = function (parent, file) {
  var node = parent;
 
  if (t.isAssignmentExpression(parent)) {
    node = parent.left;
  } else if (t.isVariableDeclarator(parent)) {
    node = parent.id;
  } else Iif (t.isProperty(node)) {
    node = node.key;
  }
 
  var parts = [];
 
  var add = function (node) {
    if (t.isMemberExpression(node)) {
      add(node.object);
      add(node.property);
    } else if (t.isIdentifier(node)) {
      parts.push(node.name);
    } else if (t.isLiteral(node)) {
      parts.push(node.value);
    } else if (t.isCallExpression(node)) {
      add(node.callee);
    }
  };
 
  add(node);
 
  var id = parts.join("$");
  id = id.replace(/^_/, "") || "ref";
 
  return file.generateUidIdentifier(id, this);
};
 
/**
 * Description
 *
 * @param {Object} node
 * @param {File} file
 * @returns {Object}
 */
 
Scope.prototype.generateTempBasedOnNode = function (node, file) {
  if (t.isIdentifier(node) && this.has(node.name, true)) {
    return null;
  }
 
  var id = this.generateUidBasedOnNode(node, file);
  this.push({
    key: id.name,
    id: id
  });
  return id;
};
 
Scope.prototype.getInfo = function () {
  var block = this.block;
  if (block._scopeInfo) return block._scopeInfo;
 
  var info = block._scopeInfo = {};
  var references = info.references = {};
  var declarations = info.declarations = {};
 
  var add = function (node, reference) {
    Scope.add(node, references);
    if (!reference) Scope.add(node, declarations);
  };
 
  // 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, {
      enter: 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 this.skip();
 
        // function identifier doesn't belong to this scope
        if (block.id && node === block.id) return;
 
        if (t.isIdentifier(node) && t.isReferenced(node, parent) && !scope.has(node.name)) {
          add(node, true);
        }
 
        // we've ran into a declaration!
        // we'll let the BlockStatement scope deal with `let` declarations unless
        if (t.isDeclaration(node) && !t.isLet(node)) {
          add(node);
        }
      }
    }, this);
  }
 
  // Function - params, rest
 
  if (t.isFunction(block)) {
    add(block.rest);
    _.each(block.params, function (param) {
      add(param);
    });
  }
 
  return info;
};
 
/**
 * Description
 *
 * @param {Object} opts
 */
 
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);
  }
};
 
/**
 * Description
 *
 * @param {Object} node
 */
 
Scope.prototype.add = function (node) {
  Scope.add(node, this.references);
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 */
 
Scope.prototype.get = function (id, decl) {
  return id && (this.getOwn(id, decl) || this.parentGet(id, decl));
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 */
 
Scope.prototype.getOwn = function (id, decl) {
  var refs = this.references;
  if (decl) refs = this.declarations;
  return _.has(refs, id) && refs[id];
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 */
 
Scope.prototype.parentGet = function (id, decl) {
  return this.parent && this.parent.get(id, decl);
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 * @returns {Boolean}
 */
 
Scope.prototype.has = function (id, decl) {
  return (id && (this.hasOwn(id, decl) || this.parentHas(id, decl))) ||
         _.contains(Scope.defaultDeclarations, id);
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 * @returns {Boolean}
 */
 
Scope.prototype.hasOwn = function (id, decl) {
  return !!this.getOwn(id, decl);
};
 
/**
 * Description
 *
 * @param {String} [id]
 * @param {Boolean} [decl]
 * @returns {Boolean}
 */
 
Scope.prototype.parentHas = function (id, decl) {
  return this.parent && this.parent.has(id, decl);
};