From b6e533ec5d17ba10586a0396c8535ad7f9869c4a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 9 Oct 2014 20:19:22 +1100 Subject: [PATCH] handle ReturnStatements in block binding - closes #24 --- lib/6to5/templates/function-call-return.js | 1 + lib/6to5/templates/function-call-this-return.js | 1 + lib/6to5/transformers/block-binding.js | 3 ++- package.json | 2 +- test/fixtures/block-binding/return/actual.js | 16 ++++++++++++++++ test/fixtures/block-binding/return/expected.js | 14 ++++++++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 lib/6to5/templates/function-call-return.js create mode 100644 lib/6to5/templates/function-call-this-return.js create mode 100644 test/fixtures/block-binding/return/actual.js create mode 100644 test/fixtures/block-binding/return/expected.js diff --git a/lib/6to5/templates/function-call-return.js b/lib/6to5/templates/function-call-return.js new file mode 100644 index 0000000000..652e5de9d5 --- /dev/null +++ b/lib/6to5/templates/function-call-return.js @@ -0,0 +1 @@ +return FUNCTION(); diff --git a/lib/6to5/templates/function-call-this-return.js b/lib/6to5/templates/function-call-this-return.js new file mode 100644 index 0000000000..6d98cadc86 --- /dev/null +++ b/lib/6to5/templates/function-call-this-return.js @@ -0,0 +1 @@ +return FUNCTION.call(this); diff --git a/lib/6to5/transformers/block-binding.js b/lib/6to5/transformers/block-binding.js index 16e836dfdc..cf44b6e019 100644 --- a/lib/6to5/transformers/block-binding.js +++ b/lib/6to5/transformers/block-binding.js @@ -31,7 +31,7 @@ exports.BlockStatement = function (node, parent, opts, generateUid) { if (!hasLet(node.body)) return; // ignore if we're the body of a closure already - if (parent.type === "FunctionExpression") return; + if (parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration") return; var body = node.body; @@ -120,6 +120,7 @@ var buildNode = function (node) { var templateName = "function-call"; if (traverse.hasType(node, "ThisExpression")) templateName += "-this"; + if (traverse.hasType(node, "ReturnStatement", ["FunctionDeclaration", "FunctionExpression"])) templateName += "-return"; // diff --git a/package.json b/package.json index b37d4034a8..87f4347422 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into vanilla ES5 with source maps and no runtime", - "version": "1.5.2", + "version": "1.5.3", "author": "Sebastian McKenzie ", "homepage": "https://github.com/sebmck/6to5", "repository": { diff --git a/test/fixtures/block-binding/return/actual.js b/test/fixtures/block-binding/return/actual.js new file mode 100644 index 0000000000..1974a42dec --- /dev/null +++ b/test/fixtures/block-binding/return/actual.js @@ -0,0 +1,16 @@ +function student () { + let isStudent = true; + return Object.freeze({ + isStudent + }); +} + +function student () { + let isStudent = true; + while (true) { + let test = "foo"; + return Object.freeze({ + isStudent + }); + } +} diff --git a/test/fixtures/block-binding/return/expected.js b/test/fixtures/block-binding/return/expected.js new file mode 100644 index 0000000000..fb2e343ae4 --- /dev/null +++ b/test/fixtures/block-binding/return/expected.js @@ -0,0 +1,14 @@ +function student() { + var isStudent = true; + return Object.freeze({ isStudent: isStudent }); +} + +function student() { + var isStudent = true; + while (true) { + return function () { + var test = 'foo'; + return Object.freeze({ isStudent: isStudent }); + }(); + } +}