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

Statements: 100% (41 / 41)      Branches: 100% (24 / 24)      Functions: 100% (10 / 10)      Lines: 100% (40 / 40)      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 791 1 1   1 293 293   293 6     293 3         293 4414 4414 4380   129   4251         34 390   390 7 383 3   380     10     34     293   293 9 9         293 6     293 3       1 130 2       1   163 7 7      
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) {
      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();
    }, _aliasFunction === "arrows" && ["FunctionExpression", "FunctionDeclaration"]);
 
    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);
};