Don't hoist template literal keys in object-rest-spread (#13483)

* remove hoisting when using template strings in proposal-object-rest-spread

* remove const from test
This commit is contained in:
Lively 2021-06-22 05:48:12 +09:00 committed by GitHub
parent 5145c98a73
commit 8ae0efe98a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -95,6 +95,7 @@ export default declare((api, opts) => {
const props = path.node.properties; const props = path.node.properties;
const keys = []; const keys = [];
let allLiteral = true; let allLiteral = true;
let hasTemplateLiteral = false;
for (const prop of props) { for (const prop of props) {
if (t.isIdentifier(prop.key) && !prop.computed) { if (t.isIdentifier(prop.key) && !prop.computed) {
@ -102,6 +103,7 @@ export default declare((api, opts) => {
keys.push(t.stringLiteral(prop.key.name)); keys.push(t.stringLiteral(prop.key.name));
} else if (t.isTemplateLiteral(prop.key)) { } else if (t.isTemplateLiteral(prop.key)) {
keys.push(t.cloneNode(prop.key)); keys.push(t.cloneNode(prop.key));
hasTemplateLiteral = true;
} else if (t.isLiteral(prop.key)) { } else if (t.isLiteral(prop.key)) {
keys.push(t.stringLiteral(String(prop.key.value))); keys.push(t.stringLiteral(String(prop.key.value)));
} else { } else {
@ -110,7 +112,7 @@ export default declare((api, opts) => {
} }
} }
return { keys, allLiteral }; return { keys, allLiteral, hasTemplateLiteral };
} }
// replaces impure computed keys with new identifiers // replaces impure computed keys with new identifiers
@ -156,7 +158,8 @@ export default declare((api, opts) => {
path.get("properties"), path.get("properties"),
path.scope, path.scope,
); );
const { keys, allLiteral } = extractNormalizedKeys(path); const { keys, allLiteral, hasTemplateLiteral } =
extractNormalizedKeys(path);
if (keys.length === 0) { if (keys.length === 0) {
return [ return [
@ -179,7 +182,7 @@ export default declare((api, opts) => {
} else { } else {
keyExpression = t.arrayExpression(keys); 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. // Hoist definition of excluded keys, so that it's not created each time.
const program = path.findParent(path => path.isProgram()); const program = path.findParent(path => path.isProgram());
const id = path.scope.generateUidIdentifier("excluded"); const id = path.scope.generateUidIdentifier("excluded");

View File

@ -0,0 +1,9 @@
const example = () => {
const input = {};
const foo = 'foo';
({
[`${foo}_bar`]: country,
...rest
} = input);
}

View File

@ -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;
};