Skip to content

Commit 82d45a2

Browse files
committed
fix: create assignable_proxy utils to prevent declaring an external const
1 parent d7876af commit 82d45a2

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,11 @@ export function ClassBody(node, context) {
125125

126126
let proxied = should_proxy(init, context.state.scope);
127127

128-
if (field.kind === 'state' && proxied && options != null) {
129-
let generated = 'state_options';
130-
let i = 0;
131-
while (private_ids.includes(generated)) {
132-
generated = `state_options_${i++}`;
133-
}
134-
private_ids.push(generated);
135-
body.push(b.prop_def(b.private_id(generated), options));
136-
options = b.member(b.this, `#${generated}`);
137-
}
138-
139128
value =
140129
field.kind === 'state'
141-
? b.call(
142-
'$.state',
143-
should_proxy(init, context.state.scope) ? b.call('$.proxy', init, options) : init,
144-
options
145-
)
130+
? should_proxy(init, context.state.scope)
131+
? b.call('$.assignable_proxy', init, options)
132+
: b.call('$.state', init, options)
146133
: field.kind === 'raw_state'
147134
? b.call('$.state', init)
148135
: field.kind === 'derived_by'

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,12 @@ export function VariableDeclaration(node, context) {
127127
context.state.scope.get(id.name)
128128
);
129129
const proxied = rune === '$state' && should_proxy(value, context.state.scope);
130-
if (proxied) {
131-
if (options != null) {
132-
const generated = context.state.scope.generate('state_options');
133-
declarations.push(b.declarator(generated, options));
134-
options = b.id(generated);
135-
}
130+
const is_state = is_state_source(binding, context.state.analysis);
131+
if (proxied && is_state) {
132+
value = b.call('$.assignable_proxy', value, options);
133+
} else if (proxied) {
136134
value = b.call('$.proxy', value, options);
137-
}
138-
if (is_state_source(binding, context.state.analysis)) {
135+
} else if (is_state) {
139136
value = b.call('$.state', value, options);
140137
}
141138
return value;

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export {
152152
} from './runtime.js';
153153
export { validate_binding, validate_each_keys } from './validate.js';
154154
export { raf } from './timing.js';
155-
export { proxy } from './proxy.js';
155+
export { proxy, assignable_proxy } from './proxy.js';
156156
export { create_custom_element } from './dom/elements/custom-element.js';
157157
export {
158158
child,

packages/svelte/src/internal/client/proxy.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
object_prototype
1010
} from '../shared/utils.js';
1111
import { check_ownership, widen_ownership } from './dev/ownership.js';
12-
import { source, set } from './reactivity/sources.js';
12+
import { source, set, state } from './reactivity/sources.js';
1313
import { STATE_SYMBOL, STATE_SYMBOL_METADATA } from './constants.js';
1414
import { UNINITIALIZED } from '../../constants.js';
1515
import * as e from './errors.js';
@@ -318,6 +318,17 @@ export function proxy(value, options, parent = null, prev) {
318318
});
319319
}
320320

321+
/**
322+
* @template T
323+
* @param {T} value
324+
* @param {ValueOptions} [options]
325+
* @returns {Source<T>}
326+
*/
327+
328+
export function assignable_proxy(value, options) {
329+
return state(proxy(value, options), options);
330+
}
331+
321332
/**
322333
* @param {Source<number>} signal
323334
* @param {1 | -1} [d]

0 commit comments

Comments
 (0)