refactor logic to only use one loop

pull/15593/head
ComputerGuy 6 months ago
parent 91b7d1df65
commit 1b0918c31c

@ -9,18 +9,18 @@ import { should_proxy } from '../utils.js';
* @param {Context} context * @param {Context} context
*/ */
export function ObjectExpression(node, context) { export function ObjectExpression(node, context) {
/**
* @typedef {[string, NonNullable<ReturnType<typeof get_rune>>]} ReactiveProperty
*/
let has_runes = false; let has_runes = false;
const valid_property_runes = ['$state', '$derived', '$state.raw', '$derived.by']; const valid_property_runes = ['$state', '$derived', '$state.raw', '$derived.by'];
/** @type {Statement[]} */ /** @type {Statement[]} */
const body = []; const body = [];
/** @type {Map<Property, ReactiveProperty>} */
const sources = new Map();
let counter = 0; let counter = 0;
/** @type {(Property | SpreadElement)[]} */
const properties = [];
for (let property of node.properties) { for (let property of node.properties) {
if (property.type !== 'Property') continue; if (property.type !== 'Property') {
properties.push(/** @type {SpreadElement} */ (context.visit(property)));
continue;
}
const rune = get_rune(property.value, context.state.scope); const rune = get_rune(property.value, context.state.scope);
if (rune && valid_property_runes.includes(rune)) { if (rune && valid_property_runes.includes(rune)) {
has_runes = true; has_runes = true;
@ -30,41 +30,23 @@ export function ObjectExpression(node, context) {
let value = /** @type {Expression} */ ( let value = /** @type {Expression} */ (
context.visit(/** @type {CallExpression} */ (property.value).arguments[0] ?? b.void0) context.visit(/** @type {CallExpression} */ (property.value).arguments[0] ?? b.void0)
); );
const key = /** @type {Expression} */ (context.visit(property.key));
value = value =
rune === '$derived' rune === '$derived'
? b.thunk(value) ? b.thunk(value)
: rune === '$state' && should_proxy(value, context.state.scope) : rune === '$state' && should_proxy(value, context.state.scope)
? b.call('$.proxy', value) ? b.call('$.proxy', value)
: value; : value;
/** @type {ReactiveProperty} */
const source = [name, rune];
sources.set(property, source);
body.push(b.let(name, b.call(call, value)));
}
}
if (!has_runes) {
context.next();
return;
}
/** @type {(Property | SpreadElement)[]} */
const properties = [];
for (let property of node.properties) {
if (property.type === 'SpreadElement') {
properties.push(/** @type {SpreadElement} */ (context.visit(property)));
continue;
}
if (sources.has(property)) {
const [name, rune] = /** @type {ReactiveProperty} */ (sources.get(property));
properties.push( properties.push(
b.prop( b.prop(
'get', 'get',
/** @type {Expression} */ (context.visit(/**@type {Expression} */ (property.key))), key,
b.function(null, [], b.block([b.return(b.call('$.get', b.id(name)))])), b.function(null, [], b.block([b.return(b.call('$.get', b.id(name)))])),
property.computed property.computed
), ),
b.prop( b.prop(
'set', 'set',
/** @type {Expression} */ (context.visit(property.key)), key,
b.function( b.function(
null, null,
[b.id('$$value')], [b.id('$$value')],
@ -77,10 +59,15 @@ export function ObjectExpression(node, context) {
property.computed property.computed
) )
); );
body.push(b.let(name, b.call(call, value)));
} else { } else {
properties.push(/** @type {Property} */ (context.visit(property))); properties.push(/** @type {Property} */ (context.visit(property)));
} }
} }
if (!has_runes) {
context.next();
return;
}
body.push(b.return(b.object(properties))); body.push(b.return(b.object(properties)));
return b.call(b.arrow([], b.block(body))); return b.call(b.arrow([], b.block(body)));
} }

Loading…
Cancel
Save