feat: better destructuring assignments (#12872)

* preserve existing destructuring assignments where appropriate

* add test

* changeset
pull/12856/head
Rich Harris 4 months ago committed by GitHub
parent 686b0865c5
commit 39c559a0d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: better destructuring assignments

@ -11,7 +11,13 @@ import { visit_assignment_expression } from '../../shared/assignments.js';
* @param {Context} context
*/
export function AssignmentExpression(node, context) {
return visit_assignment_expression(node, context, build_assignment);
const expression = /** @type {Expression} */ (
visit_assignment_expression(node, context, build_assignment) ?? context.next()
);
return is_ignored(node, 'ownership_invalid_mutation')
? b.call('$.skip_ownership_validation', b.thunk(expression))
: expression;
}
/**
@ -109,19 +115,17 @@ function build_assignment(operator, left, right, context) {
return transform.assign(object, value);
}
/** @type {Expression} */
let mutation = b.assignment(
operator,
/** @type {Pattern} */ (context.visit(left)),
/** @type {Expression} */ (context.visit(right))
);
// mutation
if (transform?.mutate) {
mutation = transform.mutate(object, mutation);
return transform.mutate(
object,
b.assignment(
operator,
/** @type {Pattern} */ (context.visit(left)),
/** @type {Expression} */ (context.visit(right))
)
);
}
return is_ignored(left, 'ownership_invalid_mutation')
? b.call('$.skip_ownership_validation', b.thunk(mutation))
: mutation;
return null;
}

@ -10,7 +10,7 @@ import { visit_assignment_expression } from '../../shared/assignments.js';
* @param {Context} context
*/
export function AssignmentExpression(node, context) {
return visit_assignment_expression(node, context, build_assignment);
return visit_assignment_expression(node, context, build_assignment) ?? context.next();
}
/**

@ -41,7 +41,7 @@ export function visit_assignment_expression(node, context, build_assignment) {
if (!changed) {
// No change to output -> nothing to transform -> we can keep the original assignment
return context.next();
return null;
}
const is_standalone = /** @type {Node} */ (context.path.at(-1)).type.endsWith('Statement');
@ -70,8 +70,5 @@ export function visit_assignment_expression(node, context, build_assignment) {
throw new Error(`Unexpected assignment type ${node.left.type}`);
}
return (
build_assignment(node.operator, node.left, node.right, context) ??
/** @type {Expression} */ (context.next())
);
return build_assignment(node.operator, node.left, node.right, context);
}

@ -3,10 +3,14 @@ import * as $ from "svelte/internal/client";
let a = $.source(1);
let b = $.source(2);
let c = 3;
let d = 4;
export function update(array) {
(
$.set(a, $.proxy(array[0])),
$.set(b, $.proxy(array[1]))
);
[c, d] = array;
}

@ -3,7 +3,10 @@ import * as $ from "svelte/internal/server";
let a = 1;
let b = 2;
let c = 3;
let d = 4;
export function update(array) {
[a, b] = array;
[c, d] = array;
}

@ -1,6 +1,9 @@
let a = $state(1);
let b = $state(2);
let c = 3;
let d = 4;
export function update(array) {
[a, b] = array;
[c, d] = array;
}

Loading…
Cancel
Save