From c9b798bb4cae38a392f0c9d3bbea0d7f7c50c3dd Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Tue, 7 Jun 2016 21:04:51 -0400 Subject: [PATCH] Ensure JSX elements under Program are transpiled --- src/index.js | 7 +++++++ test/fixtures/program/element/actual.js | 1 + test/fixtures/program/element/expected.js | 12 ++++++++++++ .../program/expression-children/actual.js | 1 + .../program/expression-children/expected.js | 12 ++++++++++++ test/fixtures/program/hoist/actual.js | 1 + test/fixtures/program/hoist/expected.js | 15 +++++++++++++++ test/fixtures/program/hoist/options.json | 5 +++++ 8 files changed, 54 insertions(+) create mode 100644 test/fixtures/program/element/actual.js create mode 100644 test/fixtures/program/element/expected.js create mode 100644 test/fixtures/program/expression-children/actual.js create mode 100644 test/fixtures/program/expression-children/expected.js create mode 100644 test/fixtures/program/hoist/actual.js create mode 100644 test/fixtures/program/hoist/expected.js create mode 100644 test/fixtures/program/hoist/options.json diff --git a/src/index.js b/src/index.js index ccf50c07..e7a31b0e 100644 --- a/src/index.js +++ b/src/index.js @@ -171,6 +171,13 @@ export default function ({ types: t, traverse: _traverse }) { }, exit(path) { + path.traverse(elementVisitor, Object.assign({}, this, { + secondaryTree: true, + root: null, + replacedElements: new Set(), + closureVarsStack: [], + })); + hoist(path, this); injectHelpers(this); } diff --git a/test/fixtures/program/element/actual.js b/test/fixtures/program/element/actual.js new file mode 100644 index 00000000..4c2f3a3d --- /dev/null +++ b/test/fixtures/program/element/actual.js @@ -0,0 +1 @@ +var div =
; diff --git a/test/fixtures/program/element/expected.js b/test/fixtures/program/element/expected.js new file mode 100644 index 00000000..aff61209 --- /dev/null +++ b/test/fixtures/program/element/expected.js @@ -0,0 +1,12 @@ +var _jsxWrapper = function _jsxWrapper(func, args) { + var wrapper = args ? function wrapper() { + return func.apply(this, args); + } : func; + wrapper.__jsxDOMWrapper = true; + return wrapper; +}; + +var div = _jsxWrapper(function () { + elementOpen("div"); + return elementClose("div"); +}); \ No newline at end of file diff --git a/test/fixtures/program/expression-children/actual.js b/test/fixtures/program/expression-children/actual.js new file mode 100644 index 00000000..740c3038 --- /dev/null +++ b/test/fixtures/program/expression-children/actual.js @@ -0,0 +1 @@ +var div =
; diff --git a/test/fixtures/program/expression-children/expected.js b/test/fixtures/program/expression-children/expected.js new file mode 100644 index 00000000..aef97c35 --- /dev/null +++ b/test/fixtures/program/expression-children/expected.js @@ -0,0 +1,12 @@ +var _jsxWrapper = function _jsxWrapper(func, args) { + var wrapper = args ? function wrapper() { + return func.apply(this, args); + } : func; + wrapper.__jsxDOMWrapper = true; + return wrapper; +}; + +var div = _jsxWrapper(function (_ref) { + elementOpen("div", _ref, ["key", _ref]); + return elementClose("div"); +}, [i++]); \ No newline at end of file diff --git a/test/fixtures/program/hoist/actual.js b/test/fixtures/program/hoist/actual.js new file mode 100644 index 00000000..740c3038 --- /dev/null +++ b/test/fixtures/program/hoist/actual.js @@ -0,0 +1 @@ +var div =
; diff --git a/test/fixtures/program/hoist/expected.js b/test/fixtures/program/hoist/expected.js new file mode 100644 index 00000000..f0af9361 --- /dev/null +++ b/test/fixtures/program/hoist/expected.js @@ -0,0 +1,15 @@ +var _jsxWrapper = function _jsxWrapper(func, args) { + var wrapper = args ? function wrapper() { + return func.apply(this, args); + } : func; + wrapper.__jsxDOMWrapper = true; + return wrapper; +}; + +var _statics = ["key", ""], + _wrapper = function _wrapper(_ref) { + elementOpen("div", _ref, (_statics[1] = _ref, _statics)); + return elementClose("div"); +}; + +var div = _jsxWrapper(_wrapper, [i++]); \ No newline at end of file diff --git a/test/fixtures/program/hoist/options.json b/test/fixtures/program/hoist/options.json new file mode 100644 index 00000000..e773c7e2 --- /dev/null +++ b/test/fixtures/program/hoist/options.json @@ -0,0 +1,5 @@ +{ + "options": { + "hoist": true + } +}