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

Statements: 98.32% (117 / 119)      Branches: 92.06% (58 / 63)      Functions: 100% (13 / 13)      Lines: 99.04% (103 / 104)      Ignored: none     

All files » 6to5/lib/6to5/traverse/ » index.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 1891   1 1 1   1 33061920 33061920 33061920 33061920     1 552     1 30 30     1 31236     1 4 4     1 60300216 60300216   2421     2421 2421 2421     2421       2421       2421 522     2421     1 33061920 33061920     33054342   33054333 33054333 33054333     33054333 33054333 33054312   33054312 30 30       33054312       33023079     33023060 27245904 27245904       1   33468511     33468499 522 633 522       33467977 33467977   33467976 33467976   33467976 32580792 32580792 32580792   30548482 7034221 9547659 9547659 9547641     7034201 189   189   137       23514261 23514261 23514239         1 49 1053 1053 1053 1053 1053 1053 1053 1053 1053   1053 1053     49 2106     49 49   49     1 22   22     22     21   20     223 2 2         20    
module.exports = traverse;
 
var Scope = require("./scope");
var t     = require("../types");
var _     = require("lodash");
 
function TraversalContext(previousContext) {
  this.didSkip = false;
  this.didRemove = false;
  this.didStop = false;
  this.didFlatten = previousContext ? previousContext.didFlatten : false;
}
 
TraversalContext.prototype.flatten = function () {
  this.didFlatten = true;
};
 
TraversalContext.prototype.remove = function () {
  this.didRemove = true;
  this.skip();
};
 
TraversalContext.prototype.skip = function () {
  this.didSkip = true;
};
 
TraversalContext.prototype.stop = function () {
  this.didStop = true;
  this.skip();
};
 
TraversalContext.prototype.maybeReplace = function (result, obj, key, node) {
  Iif (result === false) return node;
  if (result == null) return node;
 
  var isArray = Array.isArray(result);
 
  // inherit comments from original node to the first replacement node
  var inheritTo = result;
  if (isArray) inheritTo = result[0];
  if (inheritTo) t.inheritsComments(inheritTo, node);
 
  // replace the node
  node = obj[key] = result;
 
  // we're replacing a statement or block node with an array of statements so we better
  // ensure that it's a block
  Iif (isArray && _.contains(t.STATEMENT_OR_BLOCK_KEYS, key) && !t.isBlockStatement(obj)) {
    t.ensureBlock(obj, key);
  }
 
  if (isArray) {
    this.flatten();
  }
 
  return node;
};
 
TraversalContext.prototype.visit = function (obj, key, opts, scope, parent) {
  var node = obj[key];
  if (!node) return;
 
  // type is blacklisted
  if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) return;
 
  var result;
  var ourScope = scope;
  if (t.isScope(node)) ourScope = new Scope(node, scope);
 
  // enter
  Eif (opts.enter) {
    result = opts.enter.call(this, node, parent, ourScope);
    node = this.maybeReplace(result, obj, key, node);
 
    if (this.didRemove) {
      obj[key] = null;
      this.flatten();
    }
 
    // stop traversal
    if (this.didSkip) return;
  }
 
  // traverse node
  traverse(node, opts, ourScope);
 
  // exit
  if (opts.exit) {
    result = opts.exit.call(this, node, parent, ourScope);
    node = this.maybeReplace(result, obj, key, node);
  }
};
 
function traverse(parent, opts, scope) {
  // falsy node
  if (!parent) return;
 
  // array of nodes
  if (Array.isArray(parent)) {
    for (var i = 0; i < parent.length; i++)
      traverse(parent[i], opts, scope);
    return;
  }
 
  // unknown node type to traverse
  var keys = t.VISITOR_KEYS[parent.type];
  if (!keys) return;
 
  opts = opts || {};
  var context = null;
 
  for (var j = 0; j < keys.length; j++) {
    var key = keys[j];
    var nodes = parent[key];
    if (!nodes) continue;
 
    if (Array.isArray(nodes)) {
      for (var k = 0; k < nodes.length; k++) {
        context = new TraversalContext(context);
        context.visit(nodes, k, opts, scope, parent);
        if (context.didStop) return;
      }
 
      if (context && context.didFlatten) {
        parent[key] = _.flatten(parent[key]);
 
        if (key === "body") {
          // we can safely compact this
          parent[key] = _.compact(parent[key]);
        }
      }
    } else {
      context = new TraversalContext(context);
      context.visit(parent, key, opts, scope, parent);
      if (context.didStop) return;
    }
  }
}
 
traverse.removeProperties = function (tree) {
  var clear = function (node) {
    delete node._declarations;
    delete node.extendedRange;
    delete node._scopeInfo;
    delete node.tokens;
    delete node.range;
    delete node.start;
    delete node.end;
    delete node.loc;
    delete node.raw;
 
    clearComments(node.trailingComments);
    clearComments(node.leadingComments);
  };
 
  var clearComments = function (comments) {
    _.each(comments, clear);
  };
 
  clear(tree);
  traverse(tree, { enter: clear });
 
  return tree;
};
 
traverse.hasType = function (tree, type, blacklistTypes) {
  blacklistTypes = [].concat(blacklistTypes || []);
 
  var has = false;
 
  // the node we're searching in is blacklisted
  if (_.contains(blacklistTypes, tree.type)) return false;
 
  // the type we're looking for is the same as the passed node
  if (tree.type === type) return true;
 
  traverse(tree, {
    blacklist: blacklistTypes,
    enter: function (node) {
      if (node.type === type) {
        has = true;
        this.skip();
      }
    }
  });
 
  return has;
};