From 81e0b5e20f6e5f2397f0a0025de297ad0176cde2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 1 Aug 2024 21:42:58 -0400 Subject: [PATCH] chore: remove binding.mutation (#12697) * chore: remove binding.mutation * regenerate types --- .../phases/3-transform/client/transform-client.js | 2 ++ .../compiler/phases/3-transform/client/types.d.ts | 10 +++++++++- .../compiler/phases/3-transform/client/utils.js | 6 ++++-- .../3-transform/client/visitors/EachBlock.js | 15 ++++++++------- packages/svelte/src/compiler/phases/scope.js | 1 - packages/svelte/src/compiler/types/index.d.ts | 2 -- packages/svelte/types/index.d.ts | 3 --- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index fee42359ed..82d49f0ad7 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -137,6 +137,7 @@ export function client_component(analysis, options) { public_state: new Map(), private_state: new Map(), getters: {}, + setters: {}, in_constructor: false, // these are set inside the `Fragment` visitor, and cannot be used until then @@ -624,6 +625,7 @@ export function client_module(analysis, options) { public_state: new Map(), private_state: new Map(), getters: {}, + setters: {}, in_constructor: false }; diff --git a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts index 64b461e723..0284c82d1e 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts +++ b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts @@ -4,7 +4,8 @@ import type { LabeledStatement, Identifier, PrivateIdentifier, - Expression + Expression, + AssignmentExpression } from 'estree'; import type { Namespace, SvelteNode, ValidatedCompileOptions } from '#compiler'; import type { TransformState } from '../types.js'; @@ -28,6 +29,13 @@ export interface ClientTransformState extends TransformState { * will be replaced with `node` (e.g. `x` -> `$.get(x)`) */ readonly getters: Record Expression)>; + /** + * Counterpart to `getters` + */ + readonly setters: Record< + string, + (assignment: AssignmentExpression, context: Context) => Expression + >; } export interface ComponentClientTransformState extends ClientTransformState { diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js index de23d1f789..58fae325ef 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -265,8 +265,10 @@ export function build_setter(node, context, fallback, prefix, options) { if (!binding) return fallback(); - if (binding.mutation !== null) { - return binding.mutation(node, context); + if (Object.hasOwn(state.setters, left.name)) { + const setter = state.setters[left.name]; + // @ts-expect-error + return setter(node, context); } if (binding.kind === 'legacy_reactive_import') { diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js index 622c5b579f..a82677817a 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js @@ -1,6 +1,6 @@ -/** @import { BlockStatement, Expression, Identifier, MemberExpression, Pattern, Statement } from 'estree' */ +/** @import { AssignmentExpression, BlockStatement, Expression, Identifier, MemberExpression, Pattern, Statement } from 'estree' */ /** @import { Binding, EachBlock } from '#compiler' */ -/** @import { ComponentContext } from '../types' */ +/** @import { ComponentContext, Context } from '../types' */ import { EACH_INDEX_REACTIVE, EACH_IS_ANIMATED, @@ -110,7 +110,8 @@ export function EachBlock(node, context) { const child_state = { ...context.state, - getters: { ...context.state.getters } + getters: { ...context.state.getters }, + setters: { ...context.state.setters } }; /** The state used when generating the key function, if necessary */ @@ -121,7 +122,7 @@ export function EachBlock(node, context) { /** * @param {Pattern} expression_for_id - * @returns {Binding['mutation']} + * @returns {(assignment: AssignmentExpression, context: Context) => Expression} */ const create_mutation = (expression_for_id) => { return (assignment, context) => { @@ -151,7 +152,7 @@ export function EachBlock(node, context) { return b.sequence(sequence); } else { const original_left = /** @type {MemberExpression} */ (assignment.left); - const left = context.visit(original_left); + const left = /** @type {Pattern} */ (context.visit(original_left)); const assign = b.assignment(assignment.operator, left, value); sequence.unshift(assign); return b.sequence(sequence); @@ -184,7 +185,7 @@ export function EachBlock(node, context) { const declarations = []; if (node.context.type === 'Identifier') { - binding.mutation = create_mutation( + child_state.setters[node.context.name] = create_mutation( b.member( each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection, index, @@ -209,7 +210,7 @@ export function EachBlock(node, context) { const getter = needs_derived ? b.call('$.get', b.id(name)) : b.call(name); child_state.getters[name] = getter; - binding.mutation = create_mutation( + child_state.setters[name] = create_mutation( /** @type {Pattern} */ (path.update_expression(unwrapped)) ); diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 82f9591a82..5eba0d3cf2 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -118,7 +118,6 @@ export class Scope { declaration_kind, is_called: false, prop_alias: null, - mutation: null, reassigned: false, metadata: null }; diff --git a/packages/svelte/src/compiler/types/index.d.ts b/packages/svelte/src/compiler/types/index.d.ts index c7bf2ff3e1..47dc9d7eb6 100644 --- a/packages/svelte/src/compiler/types/index.d.ts +++ b/packages/svelte/src/compiler/types/index.d.ts @@ -307,8 +307,6 @@ export interface Binding { legacy_dependencies: Binding[]; /** Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props() */ prop_alias: string | null; - /** If this is set, all mutations should use this expression */ - mutation: ((assignment: AssignmentExpression, context: Context) => Expression) | null; /** Additional metadata, varies per binding type */ metadata: { /** `true` if is (inside) a rest parameter */ diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index b3230c9413..8ba68e1d2d 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -584,7 +584,6 @@ declare module 'svelte/animate' { declare module 'svelte/compiler' { import type { AssignmentExpression, ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, ArrayExpression, MemberExpression, ObjectExpression, Pattern, Node, VariableDeclarator, ArrowFunctionExpression, VariableDeclaration, FunctionExpression, Program, ChainExpression, SimpleCallExpression } from 'estree'; import type { SourceMap } from 'magic-string'; - import type { Context } from 'zimmerframe'; import type { Location } from 'locate-character'; /** * `compile` converts your `.svelte` source code into a JavaScript module that exports a component @@ -957,8 +956,6 @@ declare module 'svelte/compiler' { legacy_dependencies: Binding[]; /** Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props() */ prop_alias: string | null; - /** If this is set, all mutations should use this expression */ - mutation: ((assignment: AssignmentExpression, context: Context) => Expression) | null; /** Additional metadata, varies per binding type */ metadata: { /** `true` if is (inside) a rest parameter */