all files / lib/ traversal.js

91.55% Statements 65/71
76.67% Branches 46/60
87.5% Functions 7/8
91.55% Lines 65/71
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     110× 110× 110× 606× 606× 28× 30× 30× 28×     578× 78×   606×   110×     64663× 64663× 262132× 262132× 55831× 102759× 102759× 19158×     206301× 45028×     64638×     384× 384× 384× 352× 59459× 59459× 59459× 349696× 349696× 37021×   312675× 50128× 91812× 91812× 17797×     262547× 41310×     59459× 59459× 178377× 178377×   59459×   352× 352×     59459× 59459× 22452×   37007× 37007× 37019×   37007×                 37007× 861×   37007×        
(function() {
  var insertHelpers, morphAST, walkAST, walkASTCorrect, _, _ref, _ref1, _ref2;
 
  _ = (_ref = (_ref1 = (_ref2 = typeof window !== "undefined" && window !== null ? window._ : void 0) != null ? _ref2 : typeof self !== "undefined" && self !== null ? self._ : void 0) != null ? _ref1 : typeof global !== "undefined" && global !== null ? global._ : void 0) != null ? _ref : require('lodash');
 
  module.exports.walkAST = walkAST = function(node, fn) {
    var child, grandchild, key, _i, _len, _results;
    _results = [];
    for (key in node) {
      child = node[key];
      if (_.isArray(child)) {
        for (_i = 0, _len = child.length; _i < _len; _i++) {
          grandchild = child[_i];
          if (_.isString(grandchild != null ? grandchild.type : void 0)) {
            walkAST(grandchild, fn);
          }
        }
      } else if (_.isString(child != null ? child.type : void 0)) {
        walkAST(child, fn);
      }
      _results.push(fn(child));
    }
    return _results;
  };
 
  module.exports.walkASTCorrect = walkASTCorrect = function(node, fn) {
    var child, grandchild, key, _i, _len;
    for (key in node) {
      child = node[key];
      if (_.isArray(child)) {
        for (_i = 0, _len = child.length; _i < _len; _i++) {
          grandchild = child[_i];
          if (_.isString(grandchild != null ? grandchild.type : void 0)) {
            walkASTCorrect(grandchild, fn);
          }
        }
      } else if (_.isString(child != null ? child.type : void 0)) {
        walkASTCorrect(child, fn);
      }
    }
    return fn(node);
  };
 
  module.exports.morphAST = morphAST = function(source, transforms, parseFn, aether) {
    var ast, chunks, morphWalk;
    chunks = source.split('');
    ast = parseFn(source, aether);
    morphWalk = function(node, parent) {
      var child, grandchild, key, transform, _i, _j, _len, _len1, _results;
      insertHelpers(node, parent, chunks);
      for (key in node) {
        child = node[key];
        if (key === 'parent' || key === 'leadingComments') {
          continue;
        }
        if (_.isArray(child)) {
          for (_i = 0, _len = child.length; _i < _len; _i++) {
            grandchild = child[_i];
            if (_.isString(grandchild != null ? grandchild.type : void 0)) {
              morphWalk(grandchild, node);
            }
          }
        } else if (_.isString(child != null ? child.type : void 0)) {
          morphWalk(child, node);
        }
      }
      _results = [];
      for (_j = 0, _len1 = transforms.length; _j < _len1; _j++) {
        transform = transforms[_j];
        _results.push(transform(node, aether));
      }
      return _results;
    };
    morphWalk(ast, void 0);
    return chunks.join('');
  };
 
  insertHelpers = function(node, parent, chunks) {
    var update;
    if (!node.range) {
      return;
    }
    node.parent = parent;
    node.source = function() {
      return chunks.slice(node.range[0], node.range[1]).join('');
    };
    update = function(s) {
      var i, _i, _ref3, _ref4, _results;
      chunks[node.range[0]] = s;
      _results = [];
      for (i = _i = _ref3 = node.range[0] + 1, _ref4 = node.range[1]; _ref3 <= _ref4 ? _i < _ref4 : _i > _ref4; i = _ref3 <= _ref4 ? ++_i : --_i) {
        _results.push(chunks[i] = '');
      }
      return _results;
    };
    if (_.isObject(node.update)) {
      _.extend(update, node.update);
    }
    return node.update = update;
  };
 
}).call(this);