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 83 84 85 86 871 1   1 885 885   885 6     885 7         885   24363 24302   489   23813         61   661 18     643   642   642 7 635 7   628     14       61       885   885 12 12         885 6     885 6       1 337 3       1   548 9 9      
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, {
    enter: 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, {
        enter: 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);
};