From 26395a86fa008ebb5bcdff84bdcaf37d74655929 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:03 +1100 Subject: [PATCH] add block scoped functions - fixes #514 --- lib/6to5/transformation/transform.js | 1 + .../spec-block-scoped-functions.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/6to5/transformation/transformers/spec-block-scoped-functions.js diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 485ed26959..838ee5a90d 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -44,6 +44,7 @@ transform.moduleFormatters = { _.each({ specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), specSetters: require("./transformers/spec-setters"), + specBlockScopedFunctions: require("./transformers/spec-block-scoped-functions"), // playground malletOperator: require("./transformers/playground-mallet-operator"), diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js new file mode 100644 index 0000000000..814937fd31 --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -0,0 +1,19 @@ +var t = require("../../types"); + +exports.FunctionDeclaration = function (node, parent) { + if (t.isProgram(parent) || t.isExportDeclaration(parent)) { + return; + } + + var declar = t.variableDeclaration("let", [ + t.variableDeclarator(node.id, t.toExpression(node)) + ]); + + // hoist it up above everything else + declar._blockHoist = 2; + + // todo: name this + node.id = null; + + return declar; +};