Skip to content

Commit

Permalink
remove unnecessary hosting when using template strings (#13482)
Browse files Browse the repository at this point in the history
  • Loading branch information
lala7573 committed Jun 18, 2021
1 parent dddb413 commit eb22659
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
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}`]);
};

0 comments on commit eb22659

Please sign in to comment.