chore: remove binding.mutation (#12697)

* chore: remove binding.mutation

* regenerate types
pull/12682/head
Rich Harris 3 months ago committed by GitHub
parent 4fd7834169
commit 81e0b5e20f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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
};

@ -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<string, Expression | ((id: Identifier) => Expression)>;
/**
* Counterpart to `getters`
*/
readonly setters: Record<
string,
(assignment: AssignmentExpression, context: Context) => Expression
>;
}
export interface ComponentClientTransformState extends ClientTransformState {

@ -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') {

@ -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))
);

@ -118,7 +118,6 @@ export class Scope {
declaration_kind,
is_called: false,
prop_alias: null,
mutation: null,
reassigned: false,
metadata: null
};

@ -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<any, any>) => Expression) | null;
/** Additional metadata, varies per binding type */
metadata: {
/** `true` if is (inside) a rest parameter */

@ -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<any, any>) => Expression) | null;
/** Additional metadata, varies per binding type */
metadata: {
/** `true` if is (inside) a rest parameter */

Loading…
Cancel
Save