pull/12614/head
Rich Harris 2 years ago
parent 44076f736c
commit 20c9695e7b

@ -29,29 +29,21 @@ function serialize_assignment(node, context, is_standalone, fallback) {
node.left.type === 'RestElement' node.left.type === 'RestElement'
) { ) {
const value = /** @type {Expression} */ (context.visit(node.right)); const value = /** @type {Expression} */ (context.visit(node.right));
const should_cache = value.type !== 'Identifier'; const should_cache = value.type !== 'Identifier';
const rhs = should_cache ? b.id('$$value') : value; const rhs = should_cache ? b.id('$$value') : value;
/** @type {Expression[]} */ let unchanged = 0;
const assignments = [];
let should_transform = false;
for (const path of extract_paths(node.left)) { const assignments = extract_paths(node.left).map((path) => {
const assignment = b.assignment('=', path.node, path.expression?.(rhs)); const assignment = b.assignment('=', path.node, path.expression?.(rhs));
let changed = true;
assignments.push( return serialize_assignment(assignment, context, false, () => {
serialize_assignment(assignment, context, false, () => { unchanged += 1;
changed = false;
return assignment; return assignment;
}) });
); });
should_transform ||= changed;
}
if (!should_transform) { if (unchanged === assignments.length) {
// No change to output -> nothing to transform -> we can keep the original assignment // No change to output -> nothing to transform -> we can keep the original assignment
return fallback(); return fallback();
} }
@ -64,6 +56,7 @@ function serialize_assignment(node, context, is_standalone, fallback) {
} }
if (should_cache) { if (should_cache) {
// the right hand side is a complex expression, wrap in an IIFE to cache it
return b.call(b.arrow([rhs], sequence), value); return b.call(b.arrow([rhs], sequence), value);
} }

Loading…
Cancel
Save