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 1eb8e7b70f..ae88adc336 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/src/index.js +++ b/packages/babel-plugin-proposal-object-rest-spread/src/index.js @@ -76,6 +76,8 @@ export default declare((api, opts) => { if (t.isIdentifier(prop.key) && !prop.computed) { // since a key {a: 3} is equivalent to {"a": 3}, use the latter keys.push(t.stringLiteral(prop.key.name)); + } else if (t.isTemplateLiteral(prop.key)) { + keys.push(t.cloneNode(prop.key)); } else if (t.isLiteral(prop.key)) { keys.push(t.stringLiteral(String(prop.key.value))); } else { diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/input.js new file mode 100644 index 0000000000..66679d7f05 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/input.js @@ -0,0 +1,10 @@ +const input = {}; + +const { + given_name: givenName, + 'last_name': lastName, + [`country`]: country, + [prefix + 'state']: state, + [`${prefix}consents`]: consents, + ...rest +} = input; diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/output.js new file mode 100644 index 0000000000..17b2e9aa93 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-false/output.js @@ -0,0 +1,12 @@ +const input = {}; + +const _ref = prefix + 'state', + _ref2 = `${prefix}consents`, + { + given_name: givenName, + 'last_name': lastName, + [`country`]: country, + [_ref]: state, + [_ref2]: consents +} = input, + rest = babelHelpers.objectWithoutProperties(input, ["given_name", "last_name", `country`, _ref, _ref2].map(babelHelpers.toPropertyKey)); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/input.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/input.js new file mode 100644 index 0000000000..2a85ab9e66 --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/input.js @@ -0,0 +1,8 @@ +const input = {}; + +const { + given_name: givenName, + 'last_name': lastName, + [`country`]: country, + ...rest +} = input; diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/output.js b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/output.js new file mode 100644 index 0000000000..7dcb94f46a --- /dev/null +++ b/packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/template-literal-property-allLiterals-true/output.js @@ -0,0 +1,7 @@ +const input = {}; +const { + given_name: givenName, + 'last_name': lastName, + [`country`]: country +} = input, + rest = babelHelpers.objectWithoutProperties(input, ["given_name", "last_name", `country`]);