Skip to content

Commit 4bb30db

Browse files
committed
we should keep this logic out of , since it's very specific to accessors
1 parent 2f05191 commit 4bb30db

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,23 @@ export function client_component(source, analysis, options) {
255255

256256
const key = binding.prop_alias ?? name;
257257

258-
properties.push(
259-
b.get(key, [b.return(b.call(b.id(name)))]),
260-
b.set(
261-
key,
262-
[b.stmt(b.call(b.id(name), b.id('$$value'))), b.stmt(b.call('$.flushSync'))],
263-
analysis.runes && binding.initial
264-
? /** @type {import('estree').Expression} */ (binding.initial)
265-
: undefined
266-
)
267-
);
258+
const getter = b.get(key, [b.return(b.call(b.id(name)))]);
259+
260+
const setter = b.set(key, [
261+
b.stmt(b.call(b.id(name), b.id('$$value'))),
262+
b.stmt(b.call('$.flushSync'))
263+
]);
264+
265+
if (binding.initial) {
266+
// turn `set foo($$value)` into `set foo($$value = expression)`
267+
setter.value.params[0] = {
268+
type: 'AssignmentPattern',
269+
left: b.id('$$value'),
270+
right: /** @type {import('estree').Expression} */ (binding.initial)
271+
};
272+
}
273+
274+
properties.push(getter, setter);
268275
}
269276
}
270277

packages/svelte/src/compiler/utils/builders.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function function_declaration(id, params, body) {
210210
/**
211211
* @param {string} name
212212
* @param {import('estree').Statement[]} body
213-
* @returns {import('estree').Property}
213+
* @returns {import('estree').Property & { value: import('estree').FunctionExpression}}}
214214
*/
215215
export function get(name, body) {
216216
return prop('get', key(name), function_builder(null, [], block(body)));
@@ -305,11 +305,12 @@ export function object_pattern(properties) {
305305
}
306306

307307
/**
308+
* @template {import('estree').Expression} Value
308309
* @param {'init' | 'get' | 'set'} kind
309310
* @param {import('estree').Expression} key
310-
* @param {import('estree').Expression} value
311+
* @param {Value} value
311312
* @param {boolean} computed
312-
* @returns {import('estree').Property}
313+
* @returns {import('estree').Property & { value: Value }}
313314
*/
314315
export function prop(kind, key, value, computed = false) {
315316
return { type: 'Property', kind, key, value, method: false, shorthand: false, computed };
@@ -355,23 +356,10 @@ export function sequence(expressions) {
355356
/**
356357
* @param {string} name
357358
* @param {import('estree').Statement[]} body
358-
* @param {import('estree').Expression | null} [fallback]
359-
* @returns {import('estree').Property}
359+
* @returns {import('estree').Property & { value: import('estree').FunctionExpression}}
360360
*/
361-
export function set(name, body, fallback) {
362-
return prop(
363-
'set',
364-
key(name),
365-
function_builder(
366-
null,
367-
[
368-
fallback
369-
? { type: 'AssignmentPattern', left: id('$$value'), right: fallback }
370-
: id('$$value')
371-
],
372-
block(body)
373-
)
374-
);
361+
export function set(name, body) {
362+
return prop('set', key(name), function_builder(null, [id('$$value')], block(body)));
375363
}
376364

377365
/**

0 commit comments

Comments
 (0)