Code coverage report for 6to5/transformers/_alias-functions.js

Statements: 100% (44 / 44)      Branches: 100% (28 / 28)      Functions: 100% (10 / 10)      Lines: 100% (43 / 43)      Ignored: none     

All files » 6to5/transformers/ » _alias-functions.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 851 1 1   1 315 315   315 6     315 5         315 4991 4991 4955   136   4819         36 444 284 2       442   442 7 435 5   430     12     36     315   315 10 10         315 6     315 4       1 143 2       1   172 8 8      
var traverse = require("../traverse");
var util     = require("../util");
var b        = require("recast").types.builders;
 
var go = function (getBody, node, file) {
  var argumentsId;
  var thisId;
 
  var getArgumentsId = function () {
    return argumentsId = argumentsId || b.identifier(file.generateUid("arguments"));
  };
 
  var getThisId = function () {
    return thisId = thisId || b.identifier(file.generateUid("this"));
  };
 
  // traverse the function and find all alias functions so we can alias
  // arguments and this if neccesary
  traverse(node, function (node) {
    var _aliasFunction = node._aliasFunction;
    if (!_aliasFunction) {
      if (traverse.isFunction(node)) {
        // stop traversal of this node as it'll be hit again by this transformer
        return false;
      } else {
        return;
      }
    }
 
    // traverse all child nodes of this function and find arguments and this
    traverse(node, function (node, parent) {
      if (_aliasFunction === "arrows") {
        if (traverse.isFunction(node) && node._aliasFunction !== "arrows") {
          return false;
        }
      }
 
      var getId;
 
      if (node.type === "Identifier" && node.name === "arguments") {
        getId = getArgumentsId;
      } else if (node.type === "ThisExpression") {
        getId = getThisId;
      } else {
        return;
      }
 
      if (util.isReferenced(node, parent)) return getId();
    });
 
    return false;
  });
 
  var body;
 
  var pushDeclaration = function (id, init) {
    body = body || getBody();
    body.unshift(b.variableDeclaration("var", [
      b.variableDeclarator(id, init)
    ]));
  };
 
  if (argumentsId) {
    pushDeclaration(argumentsId, b.identifier("arguments"));
  }
 
  if (thisId) {
    pushDeclaration(thisId, b.identifier("this"));
  }
};
 
exports.Program = function (node, parent, file) {
  go(function () {
    return node.body;
  }, node, file);
};
 
exports.FunctionDeclaration =
exports.FunctionExpression = function (node, parent, file) {
  go(function () {
    util.ensureBlock(node);
    return node.body.body;
  }, node, file);
};