From 616ef8d8406e2f224372a8af32b7739f02603761 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 29 Apr 2015 12:03:15 +0100 Subject: [PATCH] make jscript transformer more trigger happy - #1382 --- .../transformers/other/jscript.js | 31 +++++++------------ .../var-named-function-expression/actual.js | 6 ++-- .../var-named-function-expression/expected.js | 8 +++-- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/babel/transformation/transformers/other/jscript.js b/src/babel/transformation/transformers/other/jscript.js index 973a9aca89..5da5355e95 100644 --- a/src/babel/transformation/transformers/other/jscript.js +++ b/src/babel/transformation/transformers/other/jscript.js @@ -4,24 +4,15 @@ export var metadata = { optional: true }; -export function VariableDeclarator(node, print) { - var varName = node.id.name; - if (node.init) { - if (t.isFunctionExpression(node.init)) { - if (node.init.id) { - var fnName = node.init.id.name; - if (varName === fnName) { - node.init._ignoreUserWhitespace = true; - var closureBody = [ - t.toStatement(node.init), - t.returnStatement(node.init.id) - ]; - var init = t.callExpression( - t.functionExpression(null, [], t.blockStatement(closureBody)), [] - ); - return t.variableDeclarator(node.id, init); - } - } - } - } +export function FunctionExpression(node, print) { + if (!node.id) return; + node._ignoreUserWhitespace = true; + + return t.callExpression( + t.functionExpression(null, [], t.blockStatement([ + t.toStatement(node), + t.returnStatement(node.id) + ])), + [] + ); } diff --git a/test/core/fixtures/transformation/jscript/var-named-function-expression/actual.js b/test/core/fixtures/transformation/jscript/var-named-function-expression/actual.js index 3f0eb85270..6bb77867dc 100644 --- a/test/core/fixtures/transformation/jscript/var-named-function-expression/actual.js +++ b/test/core/fixtures/transformation/jscript/var-named-function-expression/actual.js @@ -2,6 +2,6 @@ var IdenticalName = function IdenticalName(x) { return x; }; -var DifferentNameA = function DifferentNameB(x) { - return x; -}; +(function foo() { + +}); diff --git a/test/core/fixtures/transformation/jscript/var-named-function-expression/expected.js b/test/core/fixtures/transformation/jscript/var-named-function-expression/expected.js index 31a8fd30a2..a47668aa6f 100644 --- a/test/core/fixtures/transformation/jscript/var-named-function-expression/expected.js +++ b/test/core/fixtures/transformation/jscript/var-named-function-expression/expected.js @@ -8,6 +8,8 @@ var IdenticalName = (function () { return IdenticalName; })(); -var DifferentNameA = function DifferentNameB(x) { - return x; -}; +(function () { + function foo() {} + + return foo; +})();