Skip to content

chore: remove binding.mutation #12697

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 2 commits into from
Aug 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -624,6 +625,7 @@ export function client_module(analysis, options) {
public_state: new Map(),
private_state: new Map(),
getters: {},
setters: {},
in_constructor: false
};

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

Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export class Scope {
declaration_kind,
is_called: false,
prop_alias: null,
mutation: null,
reassigned: false,
metadata: null
};
Expand Down
2 changes: 0 additions & 2 deletions packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 0 additions & 3 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
Loading