Skip to content

Commit 1b0918c

Browse files
committed
refactor logic to only use one loop
1 parent 91b7d1d commit 1b0918c

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

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

+14-27
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import { should_proxy } from '../utils.js';
99
* @param {Context} context
1010
*/
1111
export function ObjectExpression(node, context) {
12-
/**
13-
* @typedef {[string, NonNullable<ReturnType<typeof get_rune>>]} ReactiveProperty
14-
*/
1512
let has_runes = false;
1613
const valid_property_runes = ['$state', '$derived', '$state.raw', '$derived.by'];
1714
/** @type {Statement[]} */
1815
const body = [];
19-
/** @type {Map<Property, ReactiveProperty>} */
20-
const sources = new Map();
2116
let counter = 0;
17+
/** @type {(Property | SpreadElement)[]} */
18+
const properties = [];
2219
for (let property of node.properties) {
23-
if (property.type !== 'Property') continue;
20+
if (property.type !== 'Property') {
21+
properties.push(/** @type {SpreadElement} */ (context.visit(property)));
22+
continue;
23+
}
2424
const rune = get_rune(property.value, context.state.scope);
2525
if (rune && valid_property_runes.includes(rune)) {
2626
has_runes = true;
@@ -30,41 +30,23 @@ export function ObjectExpression(node, context) {
3030
let value = /** @type {Expression} */ (
3131
context.visit(/** @type {CallExpression} */ (property.value).arguments[0] ?? b.void0)
3232
);
33+
const key = /** @type {Expression} */ (context.visit(property.key));
3334
value =
3435
rune === '$derived'
3536
? b.thunk(value)
3637
: rune === '$state' && should_proxy(value, context.state.scope)
3738
? b.call('$.proxy', value)
3839
: value;
39-
/** @type {ReactiveProperty} */
40-
const source = [name, rune];
41-
sources.set(property, source);
42-
body.push(b.let(name, b.call(call, value)));
43-
}
44-
}
45-
if (!has_runes) {
46-
context.next();
47-
return;
48-
}
49-
/** @type {(Property | SpreadElement)[]} */
50-
const properties = [];
51-
for (let property of node.properties) {
52-
if (property.type === 'SpreadElement') {
53-
properties.push(/** @type {SpreadElement} */ (context.visit(property)));
54-
continue;
55-
}
56-
if (sources.has(property)) {
57-
const [name, rune] = /** @type {ReactiveProperty} */ (sources.get(property));
5840
properties.push(
5941
b.prop(
6042
'get',
61-
/** @type {Expression} */ (context.visit(/**@type {Expression} */ (property.key))),
43+
key,
6244
b.function(null, [], b.block([b.return(b.call('$.get', b.id(name)))])),
6345
property.computed
6446
),
6547
b.prop(
6648
'set',
67-
/** @type {Expression} */ (context.visit(property.key)),
49+
key,
6850
b.function(
6951
null,
7052
[b.id('$$value')],
@@ -77,10 +59,15 @@ export function ObjectExpression(node, context) {
7759
property.computed
7860
)
7961
);
62+
body.push(b.let(name, b.call(call, value)));
8063
} else {
8164
properties.push(/** @type {Property} */ (context.visit(property)));
8265
}
8366
}
67+
if (!has_runes) {
68+
context.next();
69+
return;
70+
}
8471
body.push(b.return(b.object(properties)));
8572
return b.call(b.arrow([], b.block(body)));
8673
}

0 commit comments

Comments
 (0)