var assert = require("assert");
var recast = require("recast");
var types = recast.types;
var n = types.namedTypes;
var transform = require("..").transform;

describe("_blockHoist nodes", function() {
  it("should be hoisted to the outer body", function() {
    var ast = recast.parse([
      "function *foo(doNotHoistMe, hoistMe) {",
      "  yield doNotHoistMe();",
      "  hoistMe();",
      "  return 123;",
      "}"
    ].join("\n"));

    var hoistMeStmt = ast.program.body[0].body.body[1];
    n.ExpressionStatement.assert(hoistMeStmt);
    n.CallExpression.assert(hoistMeStmt.expression);
    n.Identifier.assert(hoistMeStmt.expression.callee);
    assert.strictEqual(hoistMeStmt.expression.callee.name, "hoistMe");

    hoistMeStmt._blockHoist = 1;

    transform(ast);

    var foo;
    eval(recast.print(ast).code);

    assert.strictEqual(typeof foo, "function");
    assert.ok(regeneratorRuntime.isGeneratorFunction(foo));

    var names = [];

    var g = foo(function doNotHoistMe() {
      names.push("doNotHoistMe");
      return "yielded";
    }, function hoistMe() {
      names.push("hoistMe");
    });

    assert.deepEqual(names, ["hoistMe"]);
    assert.deepEqual(g.next(), { value: "yielded", done: false });
    assert.deepEqual(names, ["hoistMe", "doNotHoistMe"]);
    assert.deepEqual(g.next(), { value: 123, done: true });
    assert.deepEqual(names, ["hoistMe", "doNotHoistMe"]);
  });
});

@@ -247,24 +247,57 @@
 unction() %7B%0A
+    var foo;%0A    var names = %5B%5D;%0A
     var ast 
@@ -369,16 +369,27 @@
       %22 
+ var sent =
  yield d
@@ -419,32 +419,61 @@
 %22  hoistMe();%22,%0A
+      %22  names.push(sent);%22,%0A
       %22  return 
@@ -831,42 +831,8 @@
 1;%0A%0A
-    transform(ast);%0A%0A    var foo;%0A
     
@@ -853,12 +853,23 @@
 int(
+transform(
 ast)
+)
 .cod
@@ -981,35 +981,55 @@
 (foo));%0A
-%0A
     
-var names = %5B%5D
+assert.strictEqual(names.length, 0)
 ;%0A%0A    v
@@ -1376,32 +1376,38 @@
 eepEqual(g.next(
+%22oyez%22
 ), %7B value: 123,
@@ -1472,24 +1472,32 @@
 oNotHoistMe%22
+, %22oyez%22
 %5D);%0A  %7D);%0A%7D)

Improve _blockHoist test slightly.