diff --git a/src/babel/transformation/transformers/index.js b/src/babel/transformation/transformers/index.js index a6153cd1ba..648e6cee3f 100644 --- a/src/babel/transformation/transformers/index.js +++ b/src/babel/transformation/transformers/index.js @@ -115,5 +115,6 @@ export default { "utility.inlineExpressions": require("./utility/inline-expressions"), "utility.deadCodeElimination": require("./utility/dead-code-elimination"), + jscript: require("./other/jscript"), flow: require("./other/flow") }; diff --git a/src/babel/transformation/transformers/other/jscript.js b/src/babel/transformation/transformers/other/jscript.js new file mode 100644 index 0000000000..973a9aca89 --- /dev/null +++ b/src/babel/transformation/transformers/other/jscript.js @@ -0,0 +1,27 @@ +import * as t from "../../../types"; + +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); + } + } + } + } +} diff --git a/test/core/fixtures/transformation/jscript/options.json b/test/core/fixtures/transformation/jscript/options.json new file mode 100644 index 0000000000..7cd5899040 --- /dev/null +++ b/test/core/fixtures/transformation/jscript/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["jscript"] +} diff --git a/test/core/fixtures/transformation/jscript/simple-class/actual.js b/test/core/fixtures/transformation/jscript/simple-class/actual.js new file mode 100644 index 0000000000..2fc18b1217 --- /dev/null +++ b/test/core/fixtures/transformation/jscript/simple-class/actual.js @@ -0,0 +1,2 @@ +class Test { +} diff --git a/test/core/fixtures/transformation/jscript/simple-class/expected.js b/test/core/fixtures/transformation/jscript/simple-class/expected.js new file mode 100644 index 0000000000..7d044480a4 --- /dev/null +++ b/test/core/fixtures/transformation/jscript/simple-class/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var Test = (function () { + function Test() { + babelHelpers.classCallCheck(this, Test); + } + + return Test; +})(); diff --git a/test/core/fixtures/transformation/jscript/simple-class/options.json b/test/core/fixtures/transformation/jscript/simple-class/options.json new file mode 100644 index 0000000000..7d6e6cda1c --- /dev/null +++ b/test/core/fixtures/transformation/jscript/simple-class/options.json @@ -0,0 +1,3 @@ +{ + "externalHelpers": true +} 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 new file mode 100644 index 0000000000..3f0eb85270 --- /dev/null +++ b/test/core/fixtures/transformation/jscript/var-named-function-expression/actual.js @@ -0,0 +1,7 @@ +var IdenticalName = function IdenticalName(x) { + return x; +}; + +var DifferentNameA = function DifferentNameB(x) { + return x; +}; 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 new file mode 100644 index 0000000000..31a8fd30a2 --- /dev/null +++ b/test/core/fixtures/transformation/jscript/var-named-function-expression/expected.js @@ -0,0 +1,13 @@ +"use strict"; + +var IdenticalName = (function () { + function IdenticalName(x) { + return x; + } + + return IdenticalName; +})(); + +var DifferentNameA = function DifferentNameB(x) { + return x; +};