Code coverage report for 6to5/transformation/transformers/es6-generators/visit.js

Statements: 92.31% (72 / 78)      Branches: 90.7% (39 / 43)      Functions: 100% (6 / 6)      Lines: 92.31% (72 / 78)      Ignored: none     

All files » 6to5/transformation/transformers/es6-generators/ » visit.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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                    1 1 1 1 1   1 1 1   1 321   376         1   376   376 376   376 276     100   100               100 11     100       100 100 100   100 100   100   100 25     100               100 100 25     100         100 100   100 11 11     89 84   84       84                                                                                           84       84   84                 84 84   84   84 84   84 114 114 84 84           5 5       1 114 114       114           114 61 62 62 30         84     1   2         12      
/**
 * Copyright (c) 2014, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * https://raw.githut.com/facebook/regenerator/master/LICENSE file. An
 * additional grant of patent rights can be found in the PATENTS file in
 * the same directory.
 */
 
var runtimeProperty = require("./util").runtimeProperty;
var Emitter         = require("./emit").Emitter;
var hoist           = require("./hoist").hoist;
var types           = require("ast-types");
var t               = require("../../../types");
 
var runtimeAsyncMethod = runtimeProperty("async");
var runtimeWrapMethod  = runtimeProperty("wrap");
var runtimeMarkMethod  = runtimeProperty("mark");
 
exports.transform = function transform(node, file) {
  return types.visit(node, {
    visitFunction: function (path) {
      return visitor.call(this, path, file);
    }
  });
};
 
var visitor = function (path, file) {
  // Calling this.traverse(path) first makes for a post-order traversal.
  this.traverse(path);
 
  var node = path.value;
  var scope; // we need to actually get the current scope
 
  if (!node.generator && !node.async) {
    return;
  }
 
  node.generator = false;
 
  Iif (node.expression) {
    // Transform expression lambdas into normal functions.
    node.expression = false;
    node.body = t.blockStatement([
      t.returnStatement(node.body)
    ]);
  }
 
  if (node.async) {
    awaitVisitor.visit(path.get("body"));
  }
 
  var outerFnId = node.id || (
    node.id = file.generateUidIdentifier("callee", scope)
  );
 
  var innerFnId = t.identifier(node.id.name + "$");
  var contextId = file.generateUidIdentifier("context", scope);
  var vars = hoist(path);
 
  var emitter = new Emitter(contextId);
  emitter.explode(path.get("body"));
 
  var outerBody = [];
 
  if (vars && vars.declarations.length > 0) {
    outerBody.push(vars);
  }
 
  var wrapArgs = [
    emitter.getContextFunction(innerFnId),
    // Async functions don't care about the outer function because they
    // don't need it to be marked and don't inherit from its .prototype.
    node.async ? t.literal(null) : outerFnId,
    t.thisExpression()
  ];
 
  var tryEntryList = emitter.getTryEntryList();
  if (tryEntryList) {
    wrapArgs.push(tryEntryList);
  }
 
  var wrapCall = t.callExpression(
    node.async ? runtimeAsyncMethod : runtimeWrapMethod,
    wrapArgs
  );
 
  outerBody.push(t.returnStatement(wrapCall));
  node.body = t.blockStatement(outerBody);
 
  if (node.async) {
    node.async = false;
    return;
  }
 
  if (t.isFunctionDeclaration(node)) {
    var pp = path.parent;
 
    while (pp && !(t.isBlockStatement(pp.value) || t.isProgram(pp.value))) {
      pp = pp.parent;
    }
 
    Iif (!pp) {
      return;
    }
 
    // Here we turn the FunctionDeclaration into a named
    // FunctionExpression that will be assigned to a variable of the
    // same name at the top of the enclosing block. This is important
    // for a very subtle reason: named function expressions can refer to
    // themselves by name without fear that the binding may change due
    // to code executing outside the function, whereas function
    // declarations are vulnerable to the following rebinding:
    //
    //   function f() { return f }
    //   var g = f;
    //   f = "asdf";
    //   g(); // "asdf"
    //
    // One way to prevent the problem illustrated above is to transform
    // the function declaration thus:
    //
    //   var f = function f() { return f };
    //   var g = f;
    //   f = "asdf";
    //   g(); // f
    //   g()()()()(); // f
    //
    // In the code below, we transform generator function declarations
    // in the following way:
    //
    //   gen().next(); // { value: gen, done: true }
    //   function *gen() {
    //     return gen;
    //   }
    //
    // becomes something like
    //
    //   var gen = runtime.mark(function *gen() {
    //     return gen;
    //   });
    //   gen().next(); // { value: gen, done: true }
    //
    // which ensures that the generator body can always reliably refer
    // to gen by name.
 
    // Remove the FunctionDeclaration so that we can add it back as a
    // FunctionExpression passed to runtime.mark.
    path.replace();
 
    // Change the type of the function to be an expression instead of a
    // declaration. Note that all the other fields are the same.
    node.type = "FunctionExpression";
 
    var varDecl = t.variableDeclaration("var", [
      t.variableDeclarator(
        node.id,
        t.callExpression(runtimeMarkMethod, [node])
      )
    ]);
 
    // Copy any comments preceding the function declaration to the
    // variable declaration, to avoid weird formatting consequences.
    t.inheritsComments(varDecl, node);
    t.removeComments(node);
 
    varDecl._blockHoist = true;
 
    var bodyPath = pp.get("body");
    var bodyLen = bodyPath.value.length;
 
    for (var i = 0; i < bodyLen; ++i) {
      var firstStmtPath = bodyPath.get(i);
      if (!shouldNotHoistAbove(firstStmtPath)) {
        firstStmtPath.insertBefore(varDecl);
        return;
      }
    }
 
    bodyPath.push(varDecl);
  } else {
    t.assertFunctionExpression(node);
    return t.callExpression(runtimeMarkMethod, [node]);
  }
};
 
function shouldNotHoistAbove(stmtPath) {
  var value = stmtPath.value;
  t.assertStatement(value);
 
  // If the first statement is a "use strict" declaration, make sure to
  // insert hoisted declarations afterwards.
  Iif (t.isExpressionStatement(value) &&
      t.isLiteral(value.expression) &&
      value.expression.value === "use strict") {
    return true;
  }
 
  if (t.isVariableDeclaration(value)) {
    for (var i = 0; i < value.declarations.length; ++i) {
      var decl = value.declarations[i];
      if (t.isCallExpression(decl.init) && types.astNodesAreEquivalent(decl.init.callee, runtimeMarkMethod)) {
        return true;
      }
    }
  }
 
  return false;
}
 
var awaitVisitor = types.PathVisitor.fromMethodsObject({
  visitFunction: function () {
    return false; // Don't descend into nested function scopes.
  },
 
  visitAwaitExpression: function (path) {
    // Convert await expressions to yield expressions.
    return t.yieldExpression(path.value.argument, false);
  }
});