Skip to content

Commit

Permalink
Update destructure-variable.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Oct 25, 2024
1 parent e743f24 commit 91f1693
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions packages/core/babel/core/destructure-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,31 +257,12 @@ export default function destructureVariable(
declarators.push(expr);

if (restIdentifier) {
const targetName = restIdentifier.name;
const rest = restIdentifier;
path.scope.path.traverse({
ObjectProperty(p) {
if (
!(p.scope !== path.scope && p.scope.hasOwnBinding(targetName)) &&
p.node.shorthand &&
t.isIdentifier(p.node.key) &&
p.node.key.name === targetName &&
t.isIdentifier(p.node.value) &&
p.node.value.name === targetName
) {
p.replaceWith(t.objectProperty(rest, otherIdentifier));
}
},
Expression(p) {
if (
t.isIdentifier(p.node) &&
p.node.name === targetName &&
!(p.scope !== path.scope && p.scope.hasOwnBinding(targetName))
) {
p.replaceWith(otherIdentifier);
}
},
});
const binding = path.scope.getBinding(restIdentifier.name);
if (binding) {
for (const ref of binding.referencePaths) {
ref.replaceWith(otherIdentifier);
}
}
}

return declarators;
Expand Down

0 comments on commit 91f1693

Please sign in to comment.