From c82b084927c5c15372a51e54158216052a3eddc0 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sun, 19 Mar 2017 00:32:27 +0200 Subject: [PATCH] Fix object rest params for exports. --- .../src/index.js | 18 +++++++++++------- .../actual.js | 1 + .../expected.js | 5 +++++ 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/actual.js create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/expected.js diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 4344f2cc71..f780fe72bf 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -129,6 +129,10 @@ export default function () { return { inherits: require("babel-plugin-transform-strict-mode"), + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push("objectRestSpread"); + }, + visitor: { ThisExpression(path, state) { // If other plugins run after this plugin's Program#exit handler, we allow them to @@ -311,14 +315,14 @@ export default function () { for (let i = 0; i < id.node.properties.length; i++) { const prop = id.node.properties[i]; let propValue = prop.value; - if (!t.isRestProperty(prop)) { - if (t.isAssignmentPattern(propValue)) { - propValue = propValue.left; - } - addTo(exports, propValue.name, propValue); - exportsToInsert.push(buildExportsAssignment(propValue, propValue)); - nonHoistedExportNames[propValue.name] = true; + if (t.isAssignmentPattern(propValue)) { + propValue = propValue.left; + } else if (t.isRestProperty(prop)) { + propValue = prop.argument; } + addTo(exports, propValue.name, propValue); + exportsToInsert.push(buildExportsAssignment(propValue, propValue)); + nonHoistedExportNames[propValue.name] = true; } } else if (id.isArrayPattern() && id.node.elements) { for (let i = 0; i < id.node.elements.length; i++) { diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/actual.js new file mode 100644 index 0000000000..d4d52568ce --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/actual.js @@ -0,0 +1 @@ +export const { foo, ...bar } = {}; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/expected.js new file mode 100644 index 0000000000..4e27e34014 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object-rest/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +const { foo, ...bar } = {}; +exports.foo = foo; +exports.bar = bar;