From b30bdf2294afc45c7d603dcbbdac21d13e027682 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 3 May 2015 17:22:51 +0100 Subject: [PATCH] turn inserted helper declarations into function declarations if possible --- src/babel/generation/index.js | 4 ++-- src/babel/transformation/file/index.js | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 51a0914488..790354d645 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -232,14 +232,14 @@ class CodeGenerator { this.map.mark(node, "end"); if (opts.after) opts.after(); + this.format.concise = oldConcise; + newline(false); this.printTrailingComments(node, parent); } else { throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`); } - - this.format.concise = oldConcise; } printJoin(print, nodes, opts = {}) { diff --git a/src/babel/transformation/file/index.js b/src/babel/transformation/file/index.js index 16763d0c2b..be33970d50 100644 --- a/src/babel/transformation/file/index.js +++ b/src/babel/transformation/file/index.js @@ -370,13 +370,23 @@ export default class File { } var ref = util.template("helper-" + name); - ref._compact = true; + var uid = this.declarations[name] = this.scope.generateUidIdentifier(name); - this.scope.push({ - id: uid, - init: ref, - unique: true - }); + + if (t.isFunctionExpression(ref) && !ref.id) { + ref.body._compact = true; + ref._generated = true; + ref.id = uid; + ref.type = "FunctionDeclaration"; + this.path.unshiftContainer("body", ref); + } else { + ref._compact = true; + this.scope.push({ + id: uid, + init: ref, + unique: true + }); + } return uid; }