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

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

All files » 6to5/transformation/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 831 1   1 857 857   857 8     857 6         857 23507 23349   379   22970         158 9266 25     9241   9240   9240 11 9229 6   9223     17     158     857   857 12 12         857 7     857 5       1 322 2       1   535 10 10      
var traverse = require("../../traverse");
var t        = require("../../types");
 
var go = function (getBody, node, file, scope) {
  var argumentsId;
  var thisId;
 
  var getArgumentsId = function () {
    return argumentsId = argumentsId || file.generateUidIdentifier("arguments", scope);
  };
 
  var getThisId = function () {
    return thisId = thisId || file.generateUidIdentifier("this", scope);
  };
 
  // traverse the function and find all alias functions so we can alias
  // `arguments` and `this` if necessary
  traverse(node, function (node) {
    if (!node._aliasFunction) {
      if (t.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 (t.isFunction(node) && !node._aliasFunction) {
        return false;
      }
 
      if (node._ignoreAliasFunctions) return false;
 
      var getId;
 
      if (t.isIdentifier(node) && node.name === "arguments") {
        getId = getArgumentsId;
      } else if (t.isThisExpression(node)) {
        getId = getThisId;
      } else {
        return;
      }
 
      if (t.isReferenced(node, parent)) return getId();
    });
 
    return false;
  });
 
  var body;
 
  var pushDeclaration = function (id, init) {
    body = body || getBody();
    body.unshift(t.variableDeclaration("var", [
      t.variableDeclarator(id, init)
    ]));
  };
 
  if (argumentsId) {
    pushDeclaration(argumentsId, t.identifier("arguments"));
  }
 
  if (thisId) {
    pushDeclaration(thisId, t.identifier("this"));
  }
};
 
exports.Program = function (node, parent, file, scope) {
  go(function () {
    return node.body;
  }, node, file, scope);
};
 
exports.FunctionDeclaration =
exports.FunctionExpression = function (node, parent, file, scope) {
  go(function () {
    t.ensureBlock(node);
    return node.body.body;
  }, node, file, scope);
};