Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't hoist template strings from destructuring keys #13482

Merged
merged 1 commit into from Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/babel-plugin-transform-destructuring/src/index.js
Expand Up @@ -190,7 +190,7 @@ export default declare((api, options) => {

const keys = [];
let allLiteral = true;

let hasTemplateLiteral = false;
for (let i = 0; i < pattern.properties.length; i++) {
const prop = pattern.properties[i];

Expand All @@ -204,8 +204,9 @@ export default declare((api, options) => {
const key = prop.key;
if (t.isIdentifier(key) && !prop.computed) {
keys.push(t.stringLiteral(key.name));
} else if (t.isTemplateLiteral(prop.key)) {
keys.push(t.cloneNode(prop.key));
} else if (t.isTemplateLiteral(key)) {
keys.push(t.cloneNode(key));
hasTemplateLiteral = true;
} else if (t.isLiteral(key)) {
keys.push(t.stringLiteral(String(key.value)));
} else {
Expand All @@ -228,7 +229,7 @@ export default declare((api, options) => {
t.memberExpression(keyExpression, t.identifier("map")),
[this.addHelper("toPropertyKey")],
);
} else if (!t.isProgram(this.scope.block)) {
} else if (!hasTemplateLiteral && !t.isProgram(this.scope.block)) {
// Hoist definition of excluded keys, so that it's not created each time.
const program = this.scope.path.findParent(path => path.isProgram());
const id = this.scope.generateUidIdentifier("excluded");
Expand Down
@@ -0,0 +1,4 @@
const example = (obj) => {
const foo = 'foo';
const { [`prefix_${foo}`]: _, ...rest } = obj;
};
@@ -0,0 +1,5 @@
var example = obj => {
var foo = 'foo';
var _ = obj[`prefix_${foo}`],
rest = babelHelpers.objectWithoutProperties(obj, [`prefix_${foo}`]);
};