From 4a1c393bdb055311e5bb20e1f8b66ad8e5924d27 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 20 Nov 2014 15:09:07 +1100 Subject: [PATCH] inherit generator comments and add block hoisting - fixes #196 --- .../transformers/generators/visit.js | 14 +++++++------- lib/6to5/types/index.js | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/6to5/transformation/transformers/generators/visit.js b/lib/6to5/transformation/transformers/generators/visit.js index 1c31a3e5a3..b26177103d 100644 --- a/lib/6to5/transformation/transformers/generators/visit.js +++ b/lib/6to5/transformation/transformers/generators/visit.js @@ -9,6 +9,7 @@ */ var runtimeProperty = require("./util").runtimeProperty; +var traverse = require("../../../traverse"); var Emitter = require("./emit").Emitter; var hoist = require("./hoist").hoist; var types = require("ast-types"); @@ -156,12 +157,12 @@ var visitor = types.PathVisitor.fromMethodsObject({ ) ]); - if (node.comments) { - // Copy any comments preceding the function declaration to the - // variable declaration, to avoid weird formatting consequences. - varDecl.comments = node.comments; - node.comments = null; - } + // 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; @@ -175,7 +176,6 @@ var visitor = types.PathVisitor.fromMethodsObject({ } bodyPath.push(varDecl); - } else { t.assertFunctionExpression(node); return t.callExpression(runtimeMarkMethod, [node]); diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 3e13d640e0..2699529fbd 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -250,6 +250,11 @@ t.inheritsComments = function (child, parent) { return child; }; +t.removeComments = function (node) { + delete node.leadingComments; + delete node.trailingComments; +}; + t.inherits = function (child, parent) { child.loc = parent.loc; child.end = parent.end;