diff --git a/packages/babel-plugin-proposal-object-rest-spread/src/index.js b/packages/babel-plugin-proposal-object-rest-spread/src/index.js index 396ae6cfd3c5..016583d14fe0 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/src/index.js +++ b/packages/babel-plugin-proposal-object-rest-spread/src/index.js @@ -95,6 +95,7 @@ export default declare((api, opts) => { const props = path.node.properties; const keys = []; let allLiteral = true; + let hasTemplateLiteral = false; for (const prop of props) { if (t.isIdentifier(prop.key) && !prop.computed) { @@ -102,6 +103,7 @@ export default declare((api, opts) => { keys.push(t.stringLiteral(prop.key.name)); } else if (t.isTemplateLiteral(prop.key)) { keys.push(t.cloneNode(prop.key)); + hasTemplateLiteral = true; } else if (t.isLiteral(prop.key)) { keys.push(t.stringLiteral(String(prop.key.value))); } else { @@ -110,7 +112,7 @@ export default declare((api, opts) => { } } - return { keys, allLiteral }; + return { keys, allLiteral, hasTemplateLiteral }; } // replaces impure computed keys with new identifiers @@ -156,7 +158,8 @@ export default declare((api, opts) => { path.get("properties"), path.scope, ); - const { keys, allLiteral } = extractNormalizedKeys(path); + const { keys, allLiteral, hasTemplateLiteral } = + extractNormalizedKeys(path); if (keys.length === 0) { return [ @@ -179,7 +182,7 @@ export default declare((api, opts) => { } else { keyExpression = t.arrayExpression(keys); - if (!t.isProgram(path.scope.block)) { + if (!hasTemplateLiteral && !t.isProgram(path.scope.block)) { // Hoist definition of excluded keys, so that it's not created each time. const program = path.findParent(path => path.isProgram()); const id = path.scope.generateUidIdentifier("excluded"); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/input.js new file mode 100644 index 000000000000..d8a1ab6a721d --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/input.js @@ -0,0 +1,9 @@ +const example = () => { + const input = {}; + const foo = 'foo'; + + ({ + [`${foo}_bar`]: country, + ...rest + } = input); +} diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/output.js new file mode 100644 index 000000000000..29bfbc6619df --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-allLiterals-true-no-hoisting/output.js @@ -0,0 +1,10 @@ +const example = () => { + const input = {}; + const foo = 'foo'; + var _input = input; + ({ + [`${foo}_bar`]: country + } = _input); + rest = babelHelpers.objectWithoutProperties(_input, [`${foo}_bar`]); + _input; +};