Code coverage report for 6to5/lib/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/lib/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 1104 1104   1104 9     1104 9         1104   25066 24998   658   24340         68   1181 12     1169   1161   1161 12 1149 9   1140     21       68       1104   1104 15 15         1104 8     1104 7       1 379 2       1   725 13 13      
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 this.skip();
        } 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 this.skip();
          }
 
          if (node._ignoreAliasFunctions) return this.skip();
 
          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 this.skip();
    }
  });
 
  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.thisExpression());
  }
};
 
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);
};