Skip to content

feat: better destructuring assignments #12872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-elephants-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: better destructuring assignments
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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