From 29a31762762b5815bdba2f905e2e5fae9b5167c2 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sat, 5 Dec 2015 14:27:42 -0800 Subject: [PATCH] Move destructuring statements off export statements before processing - fixes T6720 --- .../src/index.js | 19 +++++++++++++++++++ .../destructuring/export-variable/actual.js | 2 ++ .../destructuring/export-variable/expected.js | 8 ++++++++ 3 files changed, 29 insertions(+) create mode 100644 packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/actual.js create mode 100644 packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/expected.js diff --git a/packages/babel-plugin-transform-es2015-destructuring/src/index.js b/packages/babel-plugin-transform-es2015-destructuring/src/index.js index 4a334d1f24..235f27c3be 100644 --- a/packages/babel-plugin-transform-es2015-destructuring/src/index.js +++ b/packages/babel-plugin-transform-es2015-destructuring/src/index.js @@ -325,6 +325,25 @@ export default function ({ types: t }) { return { visitor: { + ExportNamedDeclaration(path){ + let declaration = path.get("declaration"); + if (!declaration.isVariableDeclaration()) return; + if (!variableDeclarationHasPattern(declaration.node)) return; + + let specifiers = []; + + for (let name in path.getOuterBindingIdentifiers(path)){ + let id = t.identifier(name); + specifiers.push(t.exportSpecifier(id, id)); + } + + // Split the declaration and export list into two declarations so that the variable + // declaration can be split up later without needing to worry about not being a + // top-level statement. + path.replaceWith(declaration.node); + path.insertAfter(t.exportNamedDeclaration(null, specifiers)); + }, + ForXStatement(path, file) { let { node, scope } = path; let left = node.left; diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/actual.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/actual.js new file mode 100644 index 0000000000..52033e2b3e --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/actual.js @@ -0,0 +1,2 @@ + +export let {a, b, c: {d, e: {f = 4}}} = {}; diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/expected.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/expected.js new file mode 100644 index 0000000000..01bd34a215 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/export-variable/expected.js @@ -0,0 +1,8 @@ +var _ref = {}; +var a = _ref.a; +var b = _ref.b; +var _ref$c = _ref.c; +var d = _ref$c.d; +var _ref$c$e$f = _ref$c.e.f; +var f = _ref$c$e$f === undefined ? 4 : _ref$c$e$f; +export { a, b, d, f };