Skip to content

Commit 81e0b5e

Browse files
authored
chore: remove binding.mutation (#12697)
* chore: remove binding.mutation * regenerate types
1 parent 4fd7834 commit 81e0b5e

File tree

7 files changed

+23
-16
lines changed

7 files changed

+23
-16
lines changed

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export function client_component(analysis, options) {
137137
public_state: new Map(),
138138
private_state: new Map(),
139139
getters: {},
140+
setters: {},
140141
in_constructor: false,
141142

142143
// these are set inside the `Fragment` visitor, and cannot be used until then
@@ -624,6 +625,7 @@ export function client_module(analysis, options) {
624625
public_state: new Map(),
625626
private_state: new Map(),
626627
getters: {},
628+
setters: {},
627629
in_constructor: false
628630
};
629631

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type {
44
LabeledStatement,
55
Identifier,
66
PrivateIdentifier,
7-
Expression
7+
Expression,
8+
AssignmentExpression
89
} from 'estree';
910
import type { Namespace, SvelteNode, ValidatedCompileOptions } from '#compiler';
1011
import type { TransformState } from '../types.js';
@@ -28,6 +29,13 @@ export interface ClientTransformState extends TransformState {
2829
* will be replaced with `node` (e.g. `x` -> `$.get(x)`)
2930
*/
3031
readonly getters: Record<string, Expression | ((id: Identifier) => Expression)>;
32+
/**
33+
* Counterpart to `getters`
34+
*/
35+
readonly setters: Record<
36+
string,
37+
(assignment: AssignmentExpression, context: Context) => Expression
38+
>;
3139
}
3240

3341
export interface ComponentClientTransformState extends ClientTransformState {

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ export function build_setter(node, context, fallback, prefix, options) {
265265

266266
if (!binding) return fallback();
267267

268-
if (binding.mutation !== null) {
269-
return binding.mutation(node, context);
268+
if (Object.hasOwn(state.setters, left.name)) {
269+
const setter = state.setters[left.name];
270+
// @ts-expect-error
271+
return setter(node, context);
270272
}
271273

272274
if (binding.kind === 'legacy_reactive_import') {

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/** @import { BlockStatement, Expression, Identifier, MemberExpression, Pattern, Statement } from 'estree' */
1+
/** @import { AssignmentExpression, BlockStatement, Expression, Identifier, MemberExpression, Pattern, Statement } from 'estree' */
22
/** @import { Binding, EachBlock } from '#compiler' */
3-
/** @import { ComponentContext } from '../types' */
3+
/** @import { ComponentContext, Context } from '../types' */
44
import {
55
EACH_INDEX_REACTIVE,
66
EACH_IS_ANIMATED,
@@ -110,7 +110,8 @@ export function EachBlock(node, context) {
110110

111111
const child_state = {
112112
...context.state,
113-
getters: { ...context.state.getters }
113+
getters: { ...context.state.getters },
114+
setters: { ...context.state.setters }
114115
};
115116

116117
/** The state used when generating the key function, if necessary */
@@ -121,7 +122,7 @@ export function EachBlock(node, context) {
121122

122123
/**
123124
* @param {Pattern} expression_for_id
124-
* @returns {Binding['mutation']}
125+
* @returns {(assignment: AssignmentExpression, context: Context) => Expression}
125126
*/
126127
const create_mutation = (expression_for_id) => {
127128
return (assignment, context) => {
@@ -151,7 +152,7 @@ export function EachBlock(node, context) {
151152
return b.sequence(sequence);
152153
} else {
153154
const original_left = /** @type {MemberExpression} */ (assignment.left);
154-
const left = context.visit(original_left);
155+
const left = /** @type {Pattern} */ (context.visit(original_left));
155156
const assign = b.assignment(assignment.operator, left, value);
156157
sequence.unshift(assign);
157158
return b.sequence(sequence);
@@ -184,7 +185,7 @@ export function EachBlock(node, context) {
184185
const declarations = [];
185186

186187
if (node.context.type === 'Identifier') {
187-
binding.mutation = create_mutation(
188+
child_state.setters[node.context.name] = create_mutation(
188189
b.member(
189190
each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection,
190191
index,
@@ -209,7 +210,7 @@ export function EachBlock(node, context) {
209210

210211
const getter = needs_derived ? b.call('$.get', b.id(name)) : b.call(name);
211212
child_state.getters[name] = getter;
212-
binding.mutation = create_mutation(
213+
child_state.setters[name] = create_mutation(
213214
/** @type {Pattern} */ (path.update_expression(unwrapped))
214215
);
215216

packages/svelte/src/compiler/phases/scope.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ export class Scope {
118118
declaration_kind,
119119
is_called: false,
120120
prop_alias: null,
121-
mutation: null,
122121
reassigned: false,
123122
metadata: null
124123
};

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,6 @@ export interface Binding {
307307
legacy_dependencies: Binding[];
308308
/** Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props() */
309309
prop_alias: string | null;
310-
/** If this is set, all mutations should use this expression */
311-
mutation: ((assignment: AssignmentExpression, context: Context<any, any>) => Expression) | null;
312310
/** Additional metadata, varies per binding type */
313311
metadata: {
314312
/** `true` if is (inside) a rest parameter */

packages/svelte/types/index.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ declare module 'svelte/animate' {
584584
declare module 'svelte/compiler' {
585585
import type { AssignmentExpression, ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, ArrayExpression, MemberExpression, ObjectExpression, Pattern, Node, VariableDeclarator, ArrowFunctionExpression, VariableDeclaration, FunctionExpression, Program, ChainExpression, SimpleCallExpression } from 'estree';
586586
import type { SourceMap } from 'magic-string';
587-
import type { Context } from 'zimmerframe';
588587
import type { Location } from 'locate-character';
589588
/**
590589
* `compile` converts your `.svelte` source code into a JavaScript module that exports a component
@@ -957,8 +956,6 @@ declare module 'svelte/compiler' {
957956
legacy_dependencies: Binding[];
958957
/** Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props() */
959958
prop_alias: string | null;
960-
/** If this is set, all mutations should use this expression */
961-
mutation: ((assignment: AssignmentExpression, context: Context<any, any>) => Expression) | null;
962959
/** Additional metadata, varies per binding type */
963960
metadata: {
964961
/** `true` if is (inside) a rest parameter */

0 commit comments

Comments
 (0)